spring-batch - Spring 批处理中的型材

标签 spring-batch profiles

我试图了解如何在 spring-batch 中使用@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 Batch 中使用配置文件概念?普通 Spring 与 Spring 批处理会有不同吗?

最佳答案

据我了解,您必须创建 ApplicationContext 之前设置配置文件。一旦创建了应用程序上下文,更改配置文件将不会产生任何影响。

摘自 SpringBoot 文档,第 25.2 章
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

Programmatically setting profiles
You can programmatically set active profiles by calling SpringApplication.setAdditionalProfiles(…​) before your application runs. It is also possible to activate profiles using Spring’s ConfigurableEnvironment interface.

关于spring-batch - Spring 批处理中的型材,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33787824/

相关文章:

java - 在基于 block 的步骤期间中止批处理作业

java - Spring 批处理 : Use of ExcecutionContext to pass step attributes

javascript - 如何声明配置文件变量并将其集成到JS文件中?如何排除/包含构建功能?

maven-2 - Maven Profile - 根据打包激活 Profile

entity-framework - 如何在 mvc4 中分离调试和释放连接等

spring-batch - 如何使用单个读取器配置 Spring 批处理作业并根据返回的对象调用多个写入器

java - 启动后如何启动Spring Batch作业?

maven - 当文件存在时使用通配符激活配置文件

ios - 阻止 iOS 设备

java - Spring Batch 异步处理器配置以获得最佳性能