I am using Spring Web MVC as frontend on hibernate with transactions (all annotation driven). I setup web.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>wdman</display-name>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>wdman</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>wdman</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- Disables Servlet Container welcome file handling.
Needed for compatibility with Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>
I don’t understand how are options like <tx:annotation-driven/> inherited from root context (in this case the one defined applicationContext.xml) to *-servlet.xml context. It doesn’t seem to function. I have component-scan in both of these files in order to get application working. Is that ok? Aren’t the components duplicated?
Could you help or point me out to some concise documentation describing how these context are merged?
They aren’t. These settings are local to the context.
If you have identical
component-scanconfig in each context, then yes, the components will be duplicated. It’s up to you to specify the scan to only instantiate the components that are necessary for each context.However, the beans defined in
applicationContext.xmlare visible to the child context, so you should be able to keep your component-scan in the parent context, and leave it out of the child context. As a rule of thumb, only MVC-specific stuff needs declaring in the servlet context, and those beans can refer to the beans defined in the parent.