5

Spring | xx-servlet.xml 和 applicationContext.xml 的区别

 2 years ago
source link: https://segmentfault.com/a/1190000040848506
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

applicationContext.xmlxx-servlet.xml 是两个具有父子关系的上下文配置。

1 定义不同

1.1 applicationContext.xml

  • 顾名思义,应用上下文配置,定义了应用整体的上下文配置,这些上下文贯穿于整个应用,应用级别的配置。
  • 多个 servlet 可以共享此配置

web.xml 中配置如下:

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

ContextLoaderListenerSpring 的监听器,它的作用就是启动 Web 容器时,自动装配 ApplicationContext 的配置信息。因为它实现了 ServletContextListener 这个接口,在 web.xml 配置这个监听器,启动容器时,就会默认执行它实现的方法。

1.2 xx-servlet.xml

  • servlet 级别的配置,可以独立多个配置文件,一个应用中可以配置多个 servlet
  • 通常用于加载 controller 层需要的类,比如拦截器, mvc 标签加载的类

web.xml 中配置如下:

    <servlet>
        <servlet-name>spring3</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring3-servlet.xml</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>

通过配置即可了解,spring3-servlet.xml 其实就是指定的 org.springframework.web.servlet.DispatcherServlet 对应的配置。

2 关系等级

Spring lets you define multiple contexts in a parent-child hierarchy.  
  The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.  
  The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. 
There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml 
for servlet spring2).  
  Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.  
  All Spring MVC controllers must go in the spring-servlet.xml context.  
  In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared 
between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.

通过官方文档的说明,即可以了解,这两者是具有父子关系

配置关系applicationContext.xml父亲xx-servlet.xml儿子
  • 一个 bean 如果在两个文件中都被定义了(比如两个文件中都定义了 component scan 扫描相同的 package ), spring 会在 application contextservlet context 中都生成一个实例,他们处于不同的上下文空间中,他们的行为方式是有可能不一样的。
  • 如果 application contextservlet context 中都存在同一个 @Service 的实例,controller(在 servlet context 中) 通过 @Resource 引用时, 会优先选择 servlet context 中的实例。
  • servlet context 可以引用 application context 里的实例,反之不可以
  • 多个 servlet 共享 application context 里的实例
  • applicationContextxx-servlet 定义的 bean 最好不要重复,xx-servlet t最好只扫描 @controlerapplicationContext 扫描其它。

applicationContext.xml

<context:component-scan base-package="com.test" use-default-filters="true">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

xx-servlet.xml

<context:component-scan base-package="com.test.controller" use-default-filters="false">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
  • use-default-filters 用来指示是否自动扫描带有 @Component@Repository@Service@Controller 的类。默认为 true,即默认扫描
  • <context:include-filter/> 子标签是用来添加扫描注解
  • <context:exclude-filter/> 子标签是用来排除扫描注解

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK