我正在使用Spring Web MVC作为事务休眠的前端(所有注释驱动).我将web.xml设置如下:
<?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>
我不明白< tx:annotation-driven />从根上下文(在本例中为一个已定义的applicationContext.xml)继承到* -servlet.xml上下文.它似乎不起作用.我在这两个文件中都进行了组件扫描,以使应用程序正常工作.这可以吗?组件不重复吗?
您能帮我指出一些简洁的文档,描述这些上下文如何合并吗?
最佳答案
I don’t understand how are options like
<tx:annotation-driven/>
inherited from root context (in this case the one definedapplicationContext.xml
) to*-servlet.xml
context
他们不是.这些设置是上下文本地的.
I have
component-scan
in both of these files in order to get application working. Is that ok? Aren’t the components duplicated?
如果在每个上下文中都有相同的component-scan配置,则可以,这些组件将被复制.由您决定指定扫描仅实例化每个上下文所需的组件.
但是,applicationContext.xml中定义的bean对子上下文是可见的,因此您应该能够在父上下文中保持组件扫描,并将其保留在子上下文之外.根据经验,只有特定于MVC的东西才需要在servlet上下文中声明,并且那些bean可以引用父对象中定义的bean.