一、在web.xml配置Spring的applicationContext .xml和监听器ContextLoaderListener
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
二、applicationContext.xml的配置
1、实现方式一:
不用在web.xml配置xml信息,xml(也是默认路径)路径必须是/WEB-INF/applicationContext.xml,名称必须是applicationContext.xml
2、实现方式二:
xml文件名、路径可以自定义,要在web.xml配置自定义的xml,指明你的xml的位置,以供web容器来加载。如果有多个xml文件,可以写在一起并用“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。
三、监听器ContextLoaderListener
作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息,初始化bean。
public class ContextLoaderListener extends ContextLoader implements ServletContextListener
因为ContextLoaderListener实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->
BeanFactory这样一来spring中的所有bean都由这个类来创建
四、spring的Beanfactory(工厂模式)
spring使用BeanFactory来实例化、配置和管理对象,但是它只是一个接口,里面有一个getBean()方法。我们一般都不直接用BeanFactory,而是用它的实现类ApplicationContext,这个类会自动解析我们配置的applicationContext.xml,然后根据我们配置的bean来new对象,将new好的对象放进一个Map中,键就是我们bean的id,值就是new的对象。
首先我们建立一个BeanFactory接口
1packagecom.spring;
2
3publicinterfaceBeanFactory {
4 Object getBean(String id);
5}
然后建立一个BeanFactory的实现类ClassPathXmlApplicationContext.java
1packagecom.spring;
2
3importjava.util.HashMap;
4importjava.util.List;
5importjava.util.Map;
6
7importorg.dom4j.Document;
8importorg.dom4j.DocumentException;
9importorg.dom4j.Element;
10importorg.dom4j.io.SAXReader;
11
12
13publicclassClassPathXmlApplicationContextimplementsBeanFactory {
14 privateMap<String, Object> beans =newHashMap<String, Object>();
15 publicClassPathXmlApplicationContext(String fileName)throwsException{
16 SAXReader reader =newSAXReader();
17 Document document = reader.read(this.getClass().getClassLoader().getResourceAsStream(fileName));
18 List<Element> elements = document.selectNodes("/beans/bean");
19 for(Element e : elements) {
20 String id = e.attributeValue("id");
21 String value = e.attributeValue("class");
22 Object o = Class.forName(value).newInstance();
23 beans.put(id, o);
24 }
25 }
26
27 publicObject getBean(String id) {
28 returnbeans.get(id);
29 }
30
31}
然后配置applicationContext.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<beans>
3 <beanid="c"class="com.spring.Car"></bean>
4 <beanid="p"class="com.spring.Plane"></bean>
5</beans>
创建类的时候顺便演示一下工厂模式,其实BeanFactory它也是一种工厂模式的。
1packagecom.spring;
2
3publicinterfaceMoveable {
4 voidrun();
5}
1packagecom.spring;
2
3publicclassCarimplementsMoveable{
4
5 publicvoidrun(){
6 System.out.println("拖着四个轮子满街跑car·····");
7 }
8}
1packagecom.spring;
2
3publicclassPlaneimplementsMoveable{
4
5 publicvoidrun() {
6 System.out.println("拖着翅膀天空飞plane......");
7 }
8
9}
现在来看一看效果吧,写一个类测试一下:
1packagecom.spring;
2
3importorg.dom4j.DocumentException;
4
5publicclassTest {
6
7 /**
8 *@paramargs
9 *@throwsDocumentException
10 */
11 publicstaticvoidmain(String[] args)throwsException {
12 BeanFactory factory =newClassPathXmlApplicationContext("applicationContext.xml");
13 Object o = factory.getBean("c");
14 Moveable m = (Moveable)o;
15 m.run();
16 }
17
18}
由于Map容器里面保存的是Object类型,所以通过getBean()方法取出来的对象要强制类型转换。
转载于:https://my.oschina.net/lsl1991/blog/679927