在web.xml 配置spring(ContextLoaderListener)

本文详细介绍了如何在Spring框架中配置applicationContext.xml文件,并利用ContextLoaderListener进行自动装配。此外,还探讨了BeanFactory的工作原理及其实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

一、在web.xml配置SpringapplicationContext .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.xmlapplicationContext-action.xmlapplicationContext-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都由这个类来创建

四、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

154100_gNAA_2725355.png

 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 }

154100_AFmt_2725355.png

  然后配置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 }

154100_tk9X_2725355.png

1 package com.spring;

2

3 public class Car implements Moveable{

4    

5     public void run(){

6         System.out.println("拖着四个轮子满街跑car·····");

7     }

8 }

154100_mDii_2725355.png

154100_jqj2_2725355.png

1 package com.spring;

2

3 public class Plane implements Moveable{

4

5     public void run() {

6         System.out.println("拖着翅膀天空飞plane......");

7     }

8    

9 }

154100_2eFA_2725355.png

 

现在来看一看效果吧,写一个类测试一下:

154101_lCpr_2725355.png

 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 }

154100_WTYz_2725355.png

由于Map容器里面保存的是Object类型,所以通过getBean()方法取出来的对象要强制类型转换。

 

 

 

 

 

 

转载于:https://my.oschina.net/lsl1991/blog/679927

下面是一个在web.xml配置ContextLoaderListener的示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 定义Spring上下文文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 配置ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置DispatcherServlet的映射 --> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` 在这个例子中,我们通过`context-param`元素来指定Spring上下文文件的位置,然后通过`listener`元素来配置ContextLoaderListener。在`listener-class`元素中,我们指定了ContextLoaderListener的类名。这样,当应用程序启动时,ContextLoaderListener会加载Spring上下文,并将其保存在ServletContext中,以便其他组件可以访问。 这个例子还配置了一个DispatcherServlet,来处理所有的HTTP请求。在`servlet`元素中,我们指定了DispatcherServlet的servlet-name和servlet-class,并通过`init-param`元素来指定DispatcherServlet的上下文文件位置。在`servlet-mapping`元素中,我们指定了DispatcherServlet的映射路径,这里我们使用了根路径`/`,表示DispatcherServlet将处理所有的HTTP请求。 在实际应用中,我们可以根据需要配置多个ContextLoaderListener和DispatcherServlet,来分别加载和处理不同的Spring上下文。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值