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!
<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>
来源:https://stackoverflow.com/questions/19543078/spring-configuration-files-relationship