1、在web.xml配置Spring的applicationContext .xml和监听器ContextLoaderListenerhtml
<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>
2、applicationContext.xml的配置java
1、实现方式一:web
不用在web.xml配置xml信息,xml(也是默认路径)路径必须是/WEB-INF/applicationContext.xml,名称必须是applicationContext.xmlspring
2、实现方式二:api
xml文件名、路径能够自定义,要在web.xml配置自定义的xml,指明你的xml的位置,以供web容器来加载。若是有多个xml文件,能够写在一块儿并用 “,”号分隔。上面的applicationContext-*.xml采用通配符,好比这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。app
3、监听器ContextLoaderListenerdom
做用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息,初始化bean。测试
public class ContextLoaderListener extends ContextLoader implements ServletContextListener
由于ContextLoaderListener实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。this
在ContextLoaderListener中关联了ContextLoader这个类,因此整个加载配置过程由ContextLoader来完成。ContextLoader建立的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->spa
BeanFactory这样一来spring中的全部bean都由这个类来建立
4、spring的Beanfactory(工厂模式)
spring使用BeanFactory来实例化、配置和管理对象,可是它只是一个接口,里面有一个getBean()方法。咱们通常都不直接用BeanFactory,而是用它的实现类ApplicationContext,这个类会自动解析咱们配置的applicationContext.xml,而后根据咱们配置的bean来new对象,将new好的对象放进一个Map中,键就是咱们bean的id,值就是new的对象。
首先咱们创建一个BeanFactory接口
1 package com.spring;
2
3 public interface BeanFactory {
4 Object getBean(String id);
5 }
而后创建一个BeanFactory的实现类ClassPathXmlApplicationContext.java
1 package com.spring;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.dom4j.Document;
8 import org.dom4j.DocumentException;
9 import org.dom4j.Element;
10 import org.dom4j.io.SAXReader;
11
12
13 public class ClassPathXmlApplicationContext implements BeanFactory {
14 private Map<String, Object> beans = new HashMap<String, Object>();
15 public ClassPathXmlApplicationContext(String fileName) throws Exception{
16 SAXReader reader = new SAXReader();
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 public Object getBean(String id) {
28 return beans.get(id);
29 }
30
31 }
而后配置applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans>
3 <bean id="c" class="com.spring.Car"></bean>
4 <bean id="p" class="com.spring.Plane"></bean>
5 </beans>
建立类的时候顺便演示一下工厂模式,其实BeanFactory它也是一种工厂模式的。
1 package com.spring;
2
3 public interface Moveable {
4 void run();
5 }
1 package com.spring;
2
3 public class Car implements Moveable{
4
5 public void run(){
6 System.out.println("拖着四个轮子满街跑car·····");
7 }
8 }
1 package com.spring;
2
3 public class Plane implements Moveable{
4
5 public void run() {
6 System.out.println("拖着翅膀天空飞plane......");
7 }
8
9 }
如今来看一看效果吧,写一个类测试一下:
1 package com.spring;
2
3 import org.dom4j.DocumentException;
4
5 public class Test {
6
7 /**
8 * @param args
9 * @throws DocumentException
10 */
11 public static void main(String[] args) throws Exception {
12 BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
13 Object o = factory.getBean("c");
14 Moveable m = (Moveable)o;
15 m.run();
16 }
17
18 }
因为Map容器里面保存的是Object类型,因此经过getBean()方法取出来的对象要强制类型转换。