Spring batch 弹簧批中的型材

Spring batch 弹簧批中的型材,spring-batch,profiles,Spring Batch,Profiles,我试图理解如何在SpringBatch中使用@profiles。 我创建了一个java文件,其中包含两个类: @Configuration @Profile("nonlocal") class NonLocalConfiguration { } and @Configuration @Profile("local") class LocalConfiguration { } 在主java类中,我尝试如下设置配置文件: AbstractApplicationContext context = n

我试图理解如何在SpringBatch中使用@profiles。 我创建了一个java文件,其中包含两个类:

@Configuration
@Profile("nonlocal")
class NonLocalConfiguration {
}
and
@Configuration
@Profile("local")
class LocalConfiguration {
}
在主java类中,我尝试如下设置配置文件:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
String run_env = System.getenv("profile");
System.out.println("run_env is: " + run_env);
context.getEnvironment().setActiveProfiles(run_env);
context.refresh();
我使用环境变量profile=local设置概要文件


当程序被执行时,我得到一个空指针异常,我认为它没有正确地获取概要文件。如何在Spring批处理中使用profiles概念?在普通弹簧和弹簧批次中会有所不同吗

据我所知,您必须在创建应用程序上下文之前设置配置文件。创建应用程序contenxt后,更改配置文件不会产生任何影响

来自SpringBoot文档,第25.2章

以编程方式设置配置文件
您可以通过编程方式将其设置为活动 通过调用SpringApplication.setAdditionalProfiles(…​) 之前 您的应用程序正在运行。还可以使用激活配置文件 Spring的可配置环境界面