Spring配置加载ContextLoaderListener和DispatcherServlet的区别和关系

栏目: Java · 发布时间: 5年前

内容简介:在Spring应用的web.xml里面可以配置ContextLoaderListener和DispatcherServlet:这种配置,Spring会加载两个ApplicationContext(应用上下文),启动关键日志如下:如果把Application打印出来,结果如下:

在Spring应用的web.xml里面可以配置ContextLoaderListener和DispatcherServlet:

< context-param >

  < param-name >contextConfigLocation</ param-name >

  < param-value >

   classpath:spring-main.xml

  </ param-value >

</ context-param >

< listener >

  < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >

</ listener >

< servlet >

  < servlet-name >dispatcher</ servlet-name >

  < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >

  < init-param >

   < param-name >contextConfigLocation</ param-name >

   < param-value >classpath:spring-servlet.xml</ param-value >

  </ init-param >

  < load-on-startup >1</ load-on-startup >

</ servlet >

这种配置,Spring会加载两个ApplicationContext(应用上下文),启动关键日志如下:

13:44:34.759 [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader:304 - Root WebApplicationContext: initialization started

......

13:44:35.290 [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader:344 - Root WebApplicationContext: initialization completed in 531 ms

......

13:44:35.327 [localhost-startStop-1] INFO  o.springframework.web.servlet.DispatcherServlet:489 - FrameworkServlet 'dispatcher': initialization started

......

13:44:35.717 [localhost-startStop-1] INFO  o.springframework.web.servlet.DispatcherServlet:508 - FrameworkServlet 'dispatcher': initialization completed in 390 ms

......

如果把Application打印出来,结果如下:

org.springframework.web.context.support.XmlWebApplicationContext:

Root WebApplicationContext: startup date [Wed Jan 24 13:59:44 CST 2018]; root of context hierarchy

......

org.springframework.web.context.support.XmlWebApplicationContext:

WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Jan 24 13:59:45 CST 2018]; parent: Root WebApplicationContext

可以看到,Spring启动时,加载了两次 XmlWebApplicationContext,Context是有继承关系的,其中第二次的Context的parent为第一次的Context,获取Context的父级Context的方法是: applicationContext.getParent();

像上面那样,将xml配置分成两个Context加载,会引起一些意外的问题,比如:

1、在spring-main.xml里面配置了 properties文件,但是在第二个Context(spring-servlet.xml)加载时,是使用不了的(比如@Value("${timeout}"))。

2、在spring-main.xml里面 扫描(component-scan)了Controller类,但是在第二个Context(spring-servlet.xml)加载时,是处理不了的。

原因是,每个 ApplicationContext 处理时,它会new一个新的Envirement和PropertyResolver,所以说它用不了其他ApplicationContext 的Properties。其二,它只会处理 自己生成的那些bean,别的bean,它可以使用,但是不会处理,所以说 如果之前的ApplicationContext 已经处理过Controller类了,那么它就不会再进一步处理Controller类了。所以,所有Controller类必须在spring-servlet.xml里面处理,可以再spring-main.xml里面像下面那样配置:

< context:component-scan base-package = "com.zollty" >  

   < context:exclude-filter type = "annotation"

     expression = "org.springframework.stereotype.Controller" />  

</ context:component-scan >

总结:将Spring配置拆分成两份ApplicationContext 配置,会带来一些意想不到的副作用,除非对Spring源码非常熟悉,否则不建议这么配置。

最简单的配置方法如下(web.xml):

< context-param >

  < param-name >contextConfigLocation</ param-name >

  < param-value >

   classpath:spring-main.xml,classpath:spring-servlet.xml

  </ param-value >

</ context-param >

< listener >

  < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >

</ listener >

< servlet >

  < servlet-name >dispatcher</ servlet-name >

  < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >

  < load-on-startup >1</ load-on-startup >

</ servlet >

即,将所有配置集中在一个ApplicationContext内,这样就避免了一些奇怪的问题。


以上所述就是小编给大家介绍的《Spring配置加载ContextLoaderListener和DispatcherServlet的区别和关系》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Computer Age Statistical Inference

Computer Age Statistical Inference

Bradley Efron、Trevor Hastie / Cambridge University Press / 2016-7-21 / USD 74.99

The twenty-first century has seen a breathtaking expansion of statistical methodology, both in scope and in influence. 'Big data', 'data science', and 'machine learning' have become familiar terms in ......一起来看看 《Computer Age Statistical Inference》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码