同事,我有主班,看起来像:

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`中加载)?
两个人如何针对不同情况在切换中使用不同的上下文配置文件?
谢谢。

最佳答案

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

在您的@ComponentScan中更具体地讲,不要包含AppConfig的软件包或其他替代品(可能最好是使用@ComponentScan excludeFilters functionality的软件包)。

10-08 03:00