上下文相关的扫描组件过滤器 [英] context depended scan-component filter

查看:64
本文介绍了上下文相关的扫描组件过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于SpringMVC的Web应用程序通常使用2个上下文:MVC调度程序Servlet的Web应用程序上下文和父/根应用程序上下文.

My SpringMVC based webapp uses typically 2 contexts: the webapplication context for the MVC dispatcher servlet and the parent/root application context.

<!-- 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等)命名的

Within these contexts, I use component scanning for loading all beans. My packages are named according their usecase (e.g. com.abc.registration, com.abc.login etc.) rather then based on the technological tier (e.g. com.abc.dao, com.abc.services etc.)

现在我的问题是:为了避免重复扫描某些类,为两种情况过滤候选组件类是一种好习惯吗,例如仅包含用于Web上下文扫描的MVC控制器,并在根应用程序上下文中包含所有其他组件(服务,Dao/存储库)?

Now my question: in order to avoid duplicate scanning of some classes, is it a good practice, to filter the candidate component classes for both contexts, e.g. include only the MVC Controller for web context scan and include all other components (services, dao/repositorie) in the root application context ?

<!-- 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>

或者对于组件扫描避免这种重复既不重要,也没有必要吗?

Or is it neither important nor necessary to avoid such duplication for the component scan ?

推荐答案

我在两个方面喜欢您的解决方案:

I like your solution in two areas:

  1. 您根据用例而不是层来划分类.如果您的所有控制器都只有一个web软件包,那么您就不会有问题.但我仍然发现这种方法要好得多.

  1. You divide classes based on uses cases rather than layers. If you would have a single web package with all controllers then you wouldn't have problems. But still I found such an approach much better.

是的,您应该过滤类.显然,增加内存占用量不是问题,因为这是微不足道的(但增加的启动时间可能很重要).

Yes, you should filter classes. Obviously it's not a problem with increased memory footprint, as this is marginal (but increased startup time might be significant).

但是,具有重复的bean(控制器和服务bean)可能会引入细微的错误和不一致之处.一些连接池已初始化两次,一些启动挂钩运行两次,从而导致意外行为.如果使用singleton范围,请保持原样.也许您不会立即遇到一些问题,但是遵守合同是很高兴的.

However having duplicated beans (both controllers and service beans) might introduce subtle bugs and inconsistencies. Some connection pool has been initialized two times, some startup hook runs two times causing unexpected behavior. If you use singleton scope, keep that it way. Maybe you won't hit some problems immediately, but it's nice to obey the contracts.

顺便说一句,还有一个<mvc:annotation-driven/>标签.

BTW note that there is an <mvc:annotation-driven/> tag as well.

这篇关于上下文相关的扫描组件过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆