从Spring自动装配中排除子包? [英] Exclude subpackages from Spring autowiring?
问题描述
在Spring 3.1中,有没有一种简单的方法可以将程序包/子程序包排除在自动装配之外?
Is there a simple way to exclude a package / sub-package from autowiring in Spring 3.1?
例如,如果我想使用基本包com.example
包括组件扫描,是否有排除com.example.ignore
的简单方法?
E.g., if I wanted to include a component scan with a base package of com.example
is there a simple way to exclude com.example.ignore
?
(为什么?我想从集成测试中排除一些组件)
(Why? I'd like to exclude some components from my integration tests)
推荐答案
我不确定您可以使用< exclude-filter>明确排除软件包,但我敢打赌,使用正则表达式过滤器可以有效地带您到达此处:
I'm not sure you can exclude packages explicitly with an <exclude-filter>, but I bet using a regex filter would effectively get you there:
<context:component-scan base-package="com.example">
<context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/>
</context:component-scan>
要使其基于注释,请使用@ com.example.annotation.ExcludedFromITests注释要为集成测试排除的每个类.然后,组件扫描将如下所示:
To make it annotation-based, you'd annotate each class you wanted excluded for integration tests with something like @com.example.annotation.ExcludedFromITests. Then the component-scan would look like:
<context:component-scan base-package="com.example">
<context:exclude-filter type="annotation" expression="com.example.annotation.ExcludedFromITests"/>
</context:component-scan>
这很清楚,因为现在您已经在源代码本身中记录了该类不打算包含在集成测试的应用程序上下文中.
That's clearer because now you've documented in the source code itself that the class is not intended to be included in an application context for integration tests.
这篇关于从Spring自动装配中排除子包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!