@ComponentScan( //CS1
basePackages = {"com.package.A", "com.package.B"},
excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
value = {com.Package.A.SomeClass.class
})
)
@ComponentScan( //CS2
basePackages = { "com.package.A"}
)
@EnableAutoConfiguration
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
以上是我SpringBootApplication
的主要课程。如您所见,我必须使用注释,而不是 xml。有两个@ComponentScan
注解。当然,Spring 是不允许的。对我来说,这两种不同@ComponentScan
意味着启动我的应用程序的两种不同方式。如果我选择使用 CS1(意思是@ComponentScan1),我不得不评论 CS2,反之亦然。
它既不优雅也不优雅。特别是对于那些春天的新手。所以我想知道如何根据我的 .properties 文件动态配置它。比如我的 .properties 文件中名为“isScanA”的参数是真的,那么我可以使用 CS1。或任何其他优雅的方式。
我已经尝试了很多。
使用占位符。比如
@ComponentScan(basePackage="${scan.basePackage}")
.and 在需要的时候改变 .properties 文件中的值。但是这种方式无法修复excludeFilters
。因为如果我FilterType.ASSIGNABLE_TYPE
用来分配需要排除的类,则value
应该是一个Class
类型而不是一个String
,如果value = {"${scan.excludeClass}"}
使用的话。程序化方式。
/** * Create a new AnnotationConfigApplicationContext, scanning for bean definitions * in the given packages and automatically refreshing the context. * @param basePackages the packages to check for annotated classes */ public AnnotationConfigApplicationContext(String... basePackages) { this(); scan(basePackages); refresh(); }
我在我的主函数中调用了这个方法。但它也无法解决excludeFilters
问题,原因在这里:Doing context:component-scan programatic way?
...
我真的尝试了很多,但仍然无法修复。所以我需要你的帮助。
请原谅我糟糕的英语。
非常感谢,至少你有一点时间阅读。