关于spring:Spring-xxservletxml-和-applicationContextxml-的区别

4次阅读

共计 2919 个字符,预计需要花费 8 分钟才能阅读完成。

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/> 子标签是用来排除扫描注解
正文完
 0