有没有一种简单的方法可以在Spring 3.1中从自动装配中排除包/子包?
例如,如果我想在基础包中包含组件扫描,com.example
是否有一种简单的排除方法com.example.ignore
?
(为什么?我想从集成测试中排除一些组件)
Jon*_*n W 81
我不确定您是否可以使用<exclude-filter>明确排除软件包,但我打赌使用正则表达式过滤器可以有效地将您带到那里:
<context:component-scan base-package="com.example">
<context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/>
</context:component-scan>
Run Code Online (Sandbox Code Playgroud)
要使其基于注释,您需要使用@ com.example.annotation.ExcludedFromITests等注释要为集成测试排除的每个类.然后组件扫描看起来像:
<context:component-scan base-package="com.example">
<context:exclude-filter type="annotation" expression="com.example.annotation.ExcludedFromITests"/>
</context:component-scan>
Run Code Online (Sandbox Code Playgroud)
这更清楚,因为现在您已经在源代码本身中记录了该类不打算包含在集成测试的应用程序上下文中.
Kir*_*rby 52
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
Wit*_*rba 27
对于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 {
// ...
}
Run Code Online (Sandbox Code Playgroud)
Ars*_*lid 12
看起来你已经通过XML完成了这项工作,但是如果你正在使用新的Spring最佳实践,那么你的配置将使用Java,你可以将它们排除在外:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.example.tool",
excludeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = {JPAConfiguration.class, SecurityConfig.class})
})
Run Code Online (Sandbox Code Playgroud)
小智 11
这适用于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>
Run Code Online (Sandbox Code Playgroud)
我认为你应该在更方便的层次结构中重构你的包,所以它们不在基础包中.
但如果你不能这样做,试试:
<context:component-scan base-package="com.example">
...
<context:exclude-filter type="regex" expression="com\.example\.ignore.*"/>
</context:component-scan>
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到更多示例:使用过滤器自定义扫描
归档时间: |
|
查看次数: |
96631 次 |
最近记录: |