在web.xml 配置spring(ContextLoaderListener)

 

 

1、在web.xml配置SpringapplicationContext .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.xmlapplicationContext-action.xmlapplicationContext-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、springBeanfactory(工廠模式)

spring使用BeanFactory來實例化、配置和管理對象,可是它只是一個接口,裏面有一個getBean()方法。咱們通常都不直接用BeanFactory,而是用它的實現類ApplicationContext,這個類會自動解析咱們配置的applicationContext.xml,而後根據咱們配置的beannew對象,將new好的對象放進一個Map中,鍵就是咱們beanid,值就是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()方法取出來的對象要強制類型轉換。

相關文章
相關標籤/搜索