从 Spring 自动装配中排除子包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10725192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exclude subpackages from Spring autowiring?
提问by HolySamosa
Is there a simple way to exclude a package / sub-package from autowiring in Spring 3.1?
有没有一种简单的方法可以从 Spring 3.1 中的自动装配中排除包/子包?
E.g., if I wanted to include a component scan with a base package of com.exampleis there a simple way to exclude com.example.ignore?
例如,如果我想在基本包中包含组件扫描,com.example是否有一种简单的排除方法com.example.ignore?
(Why? I'd like to exclude some components from my integration tests)
(为什么?我想从我的集成测试中排除一些组件)
回答by Jonathan W
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:
我不确定您是否可以使用 <exclude-filter> 明确排除包,但我打赌使用正则表达式过滤器会有效地让您到达那里:
<context:component-scan base-package="com.example">
<context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/>
</context:component-scan>
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:
为了使其基于注释,您可以使用@com.example.annotation.ExcludedFromITests 之类的内容注释要排除在集成测试中的每个类。然后组件扫描看起来像:
<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.
这更清楚,因为现在您已经在源代码中记录了该类不打算包含在集成测试的应用程序上下文中。
回答by Kirby
I am using @ComponentScanas follows for the same use case. This is the same as BenSchro10'sXML answer but this uses annotations. Both use a filter with type=AspectJ
@ComponentScan对于相同的用例,我使用如下。这与BenSchro10 的XML 答案相同,但它使用注释。两者都使用过滤器type=AspectJ
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.example" },
excludeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "com.example.ignore.*"))
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
回答by Witold Kaczurba
For Spring 4I use the following
(I am posting it as the question is 4 years old and more people use Spring 4 than Spring 3.1):
对于Spring 4,我使用以下内容
(我发布它是因为问题是 4 岁,而且使用 Spring 4 的人比使用 Spring 3.1 的人多):
@Configuration
@ComponentScan(basePackages = "com.example",
excludeFilters = @Filter(type=FilterType.REGEX,pattern="com\.example\.ignore\..*"))
public class RootConfig {
// ...
}
回答by Arsalan Khalid
It seems you've done this through XML, but if you were working in new Spring best practice, your config would be in Java, and you could exclude them as so:
似乎您已经通过 XML 完成了此操作,但是如果您正在使用新的 Spring 最佳实践,那么您的配置将使用 Java,您可以将它们排除在外:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.example.tool",
excludeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = {JPAConfiguration.class, SecurityConfig.class})
})
回答by BenSchro10
This works in Spring 3.0.5. So, I would think it would work in 3.1
这适用于 Spring 3.0.5。所以,我认为它会在 3.1 中工作
<context:component-scan base-package="com.example">
<context:exclude-filter type="aspectj" expression="com.example.dontscanme.*" />
</context:component-scan>
回答by richarbernal
I think you should refactor your packages in more convenient hierarchy, so they are out of the base package.
我认为你应该在更方便的层次结构中重构你的包,所以它们不在基础包中。
But if you can't do this, try:
但是,如果您不能这样做,请尝试:
<context:component-scan base-package="com.example">
...
<context:exclude-filter type="regex" expression="com\.example\.ignore.*"/>
</context:component-scan>
Here you could find more examples: Using filters to customize scanning
在这里您可以找到更多示例:使用过滤器自定义扫描
回答by leeor
One thing that seems to work for me is this:
似乎对我有用的一件事是:
@ComponentScan(basePackageClasses = {SomeTypeInYourPackage.class}, resourcePattern = "*.class")
Or in XML:
或者在 XML 中:
<context:component-scan base-package="com.example" resource-pattern="*.class"/>
This overrides the default resourcePatternwhich is "**/*.class".
这将覆盖默认resourcePattern是"**/*.class"。
This would seem like the most type-safe way to ONLY include your base package since that resourcePattern would always be the same and relative to your base package.
这似乎是仅包含您的基本包的最类型安全的方式,因为该 resourcePattern 将始终相同并且相对于您的基本包。
回答by dorony
You can also use @SpringBootApplication, which according to Spring documentation does the same functionality as the following three annotations: @Configuration, @EnableAutoConfiguration@ComponentScanin one annotation.
您还可以使用@SpringBootApplication,根据 Spring 文档,它与以下三个注释具有相同的功能: @Configuration,@ EnableAutoConfiguration @ComponentScan在一个注释中。
@SpringBootApplication(exclude= {Foo.class})
public class MySpringConfiguration {}
回答by Rishabh Agarwal
You can also include specific package and excludes them like :
您还可以包含特定的包并排除它们,例如:
Include and exclude (both)
包括和排除(两者)
@SpringBootApplication
(
scanBasePackages = {
"com.package1",
"com.package2"
},
exclude = {org.springframework.boot.sample.class}
)
JUST Exclude
只排除
@SpringBootApplication(exclude= {com.package1.class})
public class MySpringConfiguration {}

