开发者

Spring中配置ContextLoaderListener方式

目录
  • Spring中配置ContextLoaderLishttp://www.devze.comtener
    • ContextLoaderListener的作用是什么?
    • web.XML 中 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>
        &jslt;/context-param>
    
        &landroidt;!-- 配置spring核心监听器 Bootstraps the root web application context before servlet initialization -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

    ContextLoaderListener的作用是js什么?

    ContextLoaderListener的作用就是启动Web容器时,读取在contextConfigLocation中定义的xml文件,自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。

    简单来说,就是上面这段配置为项目提供了spring支持,初始化了Ioc容器。

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

    • 如果只有 Spring mvc 的一个 Servlet,listener 可以不用。
    • 但是如果用了Shiro 等,Shiro 用到的 Spring 的配置必须在 listener 里加载。
    • 一般 Dao, Service 的 Spring 配置都会在 listener 里加载,因为可能会在多个 Servlet 里用到,而且父子 Context 间存在可见性问题,所以,为了防止重复加载需要在 listener 中进行加载。

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜