java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring中配置ContextLoaderListener

Spring中配置ContextLoaderListener方式

作者:长不大的大灰狼

这篇文章主要介绍了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 是否可以不配置?

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文