spring configuration files relationship?

百般思念 提交于 2019-12-04 19:06:54
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/applicationContext.xml</param-value>
</context-param>

You must have a listener defined as this:

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

This ContextLoaderListener listener is responsible for starting and stopping the Spring root ApplicationContext interface, which loads the beans defined in applicationContext.xml. It finds the configurations to be used by looking at the <context-param> tag for contextConfigLocation.

<servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
            <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/spring-ws-servlet.xml</param-value>
        </init-param>
</servlet>

The ApplicationContext created by DispatcherServlet is a child of the root ApplicationContext interface. Typically, Spring MVC-specific components are initialized in the ApplicationContext interface of DispatcherServlet, while the rest are loaded by ContextLoaderListener.

The beans in a child ApplicationContext (such as those created by DispatcherServlet) can refer to beans of its parent ApplicationContext (such as those created by ContextLoaderListener). However, the parent ApplicationContext cannot refer to beans of the child ApplicationContext.

There are two types of contexts we are dealing with:

1: root context (parent context. Typically include all jdbc(ORM, Hibernate) initialisation and other spring security related configuration)

2: individual servlet context (child context.Typically Dispatcher Servlet Context and initialise all beans related to spring-mvc (controllers , URL Mapping etc)).

Here is an example of web.xml which includes multiple application context file

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>Spring Web Application example</display-name>

<!-- Configurations for the root application context (parent context) -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/jdbc/spring-jdbc.xml <!-- JDBC related context -->
        /WEB-INF/spring/security/spring-security-context.xml <!-- Spring Security related context -->
    </param-value>
</context-param>

<!-- Configurations for the DispatcherServlet application context (child context) -->
<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/mvc/spring-mvc-servlet.xml
        </param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/admin/*</url-pattern>
</servlet-mapping>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!