在Spring3.1中,有没有一种简单的方法可以从自动装配中排除一个包/子包?
例如,如果我想在com.example的基础包中包含一个组件扫描,有没有一种简单的方法可以排除com.example.ignore
(为什么?我想从我的集成测试中排除一些组件)
发布于 2012-05-24 02:11:02
我不确定您是否可以使用显式排除包,但我打赌使用正则表达式过滤器将有效地将您带到那里:
<context:component-scan base-package="com.example">
<context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/>
</context:component-scan>要使其基于注释,您可以使用诸如@com.example.annotation.ExcludedFromITests之类的内容来注释您希望在集成测试中排除的每个类。然后,组件扫描将如下所示:
<context:component-scan base-package="com.example">
<context:exclude-filter type="annotation" expression="com.example.annotation.ExcludedFromITests"/>
</context:component-scan>这一点更清楚了,因为现在您已经在源代码本身中记录了该类并不打算包含在集成测试的应用程序上下文中。
发布于 2015-05-13 03:27:30
对于相同的用例,我使用@ComponentScan的方法如下所示。这与BenSchro10's XML answer相同,但使用了注释。两者都使用带有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);
}
}发布于 2016-08-11 02:47:26
看起来您已经通过XML实现了这一点,但是如果您使用的是新的Spring最佳实践,那么您的配置应该是Java的,并且您可以将它们排除在外:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.example.tool",
excludeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
value = {JPAConfiguration.class, SecurityConfig.class})
})https://stackoverflow.com/questions/10725192
复制相似问题