筛选@ComponentScan中的特定软件包

筛选@ComponentScan中的特定软件包,第1张

筛选@ComponentScan中的特定软件包

你只需要为

Config
所需的两个
@ComponentScan
注释创建两个类。

因此,例如

Config
,你的
foo.bar
包装将有一个类:

@Configuration@ComponentScan(basePackages = {"foo.bar"},     excludeFilters = @ComponentScan.Filter(value = Service.class, type = FilterType.ANNOTATION))public class FooBarConfig {}

然后是

Config
你的
foo.baz
包裹的二等舱:

@Configuration@ComponentScan(basePackages = {"foo.baz"})public class FooBazConfig {}

然后在实例化Spring上下文时,你将执行以下 *** 作:

new AnnotationConfigApplicationContext(FooBarConfig.class, FooBazConfig.class);

另一种选择是,你可以@org.springframework.context.annotation.import在第一Config类上使用批注来导入第二Config类。因此,例如,你可以更改FooBarConfig为:

@Configuration@ComponentScan(basePackages = {"foo.bar"},     excludeFilters = @ComponentScan.Filter(value = Service.class, type = FilterType.ANNOTATION))@import(FooBazConfig.class)public class FooBarConfig {}

然后,你只需从以下内容开始上下文:

new AnnotationConfigApplicationContext(FooBarConfig.class)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5014335.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-15

发表评论

登录后才能评论

评论列表(0条)

    保存