你只需要为
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)



