【发布时间】:2011-09-30 20:02:59
【问题描述】:
我基于 SpringMVC 的 web 应用程序通常使用 2 个上下文:MVC 调度程序 servlet 的 web 应用程序上下文和父/根应用程序上下文。
<!-- the context for the dispatcher servlet -->
<servlet>
<servlet-name>webApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
....
<!-- the context for the root/parent application context -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:root-context.xml</param-value>
</context-param>
在这些上下文中,我使用组件扫描来加载所有 bean。 我的包是根据它们的用例命名的(例如 com.abc.registration、com.abc.login 等),而不是基于技术层(例如 com.abc.dao、com.abc.services 等)
现在我的问题是:为了避免重复扫描某些类,过滤两个上下文的候选组件类是否是一种好习惯,例如仅包含用于 Web 上下文扫描的 MVC 控制器,并在根应用程序上下文中包含所有其他组件(服务、dao/repositerie)?
<!-- servlet-context.xml -->
<context:component-scan base-package="com.abc.myapp" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- root-context.xml -->
<context:component-scan base-package="de.efinia.webapp">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
或者避免组件扫描的这种重复既不重要也没有必要?
【问题讨论】:
标签: spring spring-mvc