Spring中配置ContextLoaderListener

Spring中配置ContextLoaderListener

web.xml:

  <!-- contextConfigLocation参数用来指定Spring的配置文件 needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-persist-*.xml</param-value>
    </context-param>

    <!-- 配置spring核心监听器 Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

ContextLoaderListener的作用是什么?
ContextLoaderListener的作用就是启动Web容器时,读取在contextConfigLocation中定义的xml文件,自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。简单来说,就是上面这段配置为项目提供了spring支持,初始化了Ioc容器。

web.xml 中 ContextLoaderListener 是否可以不配置?

  • 如果只有 Spring mvc 的一个 Servlet,listener 可以不用。
  • 但是如果用了Shiro 等,Shiro 用到的 Spring 的配置必须在 listener 里加载。
  • 一般 Dao, Service 的 Spring 配置都会在 listener 里加载,因为可能会在多个 Servlet 里用到,而且父子 Context 间存在可见性问题,所以,为了防止重复加载需要在 listener 中进行加载。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个在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上下文。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值