Search code examples
javaspringjava-ee-6spring-ioc

spring configuration files relationship?


I am using spring3.x. i have below configuration files in my application.

applicationContext.xml
spring-ws-servlet.xml

web.xml

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


<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>
    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

My question is what kind of beans do we need to keep in applicationContext.xml and what kind of beans do we need to keep in spring-ws-servlet.xml ? why do we need both? Shall i keep everything in applicationContext.xml and delete spring-ws-servlet.xml ?

If i have both then should i import one in another ? Please help me.

Thanks!


Solution

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