如何上传不同的上下文配置类?

时间:2016-11-30 11:27:41

标签: java spring config

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`)? 如何在交换机中为不同的情况使用不同的上下文配置文件? 谢谢。

1 个答案:

答案 0 :(得分:1)

由于@ComponentScan中的AppConfigBasic - 应该包含您的AppConfig(不要忘记@ComponentScan也在扫描@Configuration bean)。

@ComponentScan中更具体,不要包含AppConfig或替代方案(也可能更喜欢)使用@ComponentScan excludeFilters functionality