Spring | The difference between xx-servlet.xml and applicationContext.xml

kycool
中文

applicationContext.xml and xx-servlet.xml are two context configurations with a parent-child relationship.

1 Different definitions

1.1 applicationContext.xml

  • As the name implies, the application context configuration defines the overall context configuration of the application. These contexts run through the entire application and application-level configuration.
  • Multiple servlet can share this configuration

The configuration in web.xml is as follows:

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

ContextLoaderListener is Spring listener, its role is to start Web container, automatic assembly ApplicationContext configuration information. Because it implements ServletContextListener this interface, in web.xml configure the listener, when you start a container, it will default execution method it implements.

1.2 xx-servlet.xml

  • servlet level configuration, multiple configuration files can be independent, multiple servlet
  • Usually used to load controller required by the 061aed71835917 layer, such as interceptors, mvc loaded by the 061aed71835918 label

The configuration in web.xml is as follows:

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

Through the configuration, you can understand that spring3-servlet.xml is actually the configuration corresponding to the org.springframework.web.servlet.DispatcherServlet

注意:
servlet 下 
<param-name>contextConfigLocation</param-name> 

中对应的 contextConfigLocation 的配置和顶部配置的 

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

contextConfigLocation 的业务属性不同。一个是定义全局的上下文配置,一个是
定义单独的 servlet 对应的上下文配置。

2 Relationship level

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.

Through the description of the official document, you can understand that the two have a parent-child relationship

Configurationrelation
applicationContext.xmlFather
xx-servlet.xmlson

Key points:

  • A bean If both are defined (for example, two files are defined in two documents component scan same scanning package ), spring will application context and servlet context generates one example, they are in a different context spaces, their The behavior may be different.
  • If application context and servlet context same are present @Service instance, controller (in servlet context in) by @Resource referencing, prefers servlet context instance.
  • servlet context can refer application context , but not vice versa
  • Multiple servlet share the instance in application context
  • In applicationContext and xx-servlet defined bean best not repeated, xx-servlet T preferably only scan @controler , applicationContext scan other.

E.g:

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>

Notice:

  • use-default-filters used to indicate whether to automatically scan @Component with 061aed71835bae, @Repository , @Service and @Controller . The default is true , which is the default scan
  • <context:include-filter/> is used to add scan annotations
  • <context:exclude-filter/> is used to exclude scan annotations
阅读 543

黑月亮
点滴记录,步步成长

现实与完美之间

1.6k 声望
24 粉丝
0 条评论

现实与完美之间

1.6k 声望
24 粉丝
文章目录
宣传栏