Collegues,我的主要课程看起来像:
public class App {
static final Logger LOG = Logger.getLogger(App.class.getName());
public static void main(String[] args) {
for(int i = 0; i < args.length; i++) {
switch (args[i]) {
case "OrderUpload":
ApplicationContext ctxOrderUpload = new AnnotationConfigApplicationContext(AppConfigBasic.class);
OrderUpload OrderUpload = ctxOrderUpload.getBean(OrderUpload.class);
OrderUpload.process();
break;
case "TrdUpload":
ApplicationContext ctxTrdUpload = new AnnotationConfigApplicationContext(AppConfig.class);
TrdUpload TrdUpload = ctxTrdUpload.getBean(TrdUpload.class);
TrdUpload.process();
break;
default:
LOG.info("Process is not defined - > " + args[i]);
System.exit(0);
}
}
}
我有两个配置类:
@Configuration
@EnableTransactionManagement
@ComponentScan
public class AppConfigBasic {}
和扩展配置
@Configuration
@EnableTransactionManagement
@ComponentScan
public class AppConfig extends AppConfigBasic {}
为什么在执行java -jar app.jar OrderUpload
时,Spring也会从AppConfig加载bean(不仅仅是来自AppConfigBasic`)?
如何在交换机中为不同的情况使用不同的上下文配置文件?
谢谢。
答案 0 :(得分:1)
由于@ComponentScan
中的AppConfigBasic
- 应该包含您的AppConfig
(不要忘记@ComponentScan
也在扫描@Configuration
bean)。
在@ComponentScan
中更具体,不要包含AppConfig
或替代方案(也可能更喜欢)使用@ComponentScan
excludeFilters functionality。