Java 在spring mvc中从环境变量设置活动配置文件
我需要在我的postconstruction钩子中访问应用程序上下文。这可能吗?如何通过编程实现Java 在spring mvc中从环境变量设置活动配置文件,java,spring,Java,Spring,我需要在我的postconstruction钩子中访问应用程序上下文。这可能吗?如何通过编程实现 public final void systemSetup() throws Exception { // I would like to set active profile here so I need the context. context.getEnvironment().setActiveProfiles(APP_ENV.toLowerCase()); PrintVaria
public final void systemSetup() throws Exception {
// I would like to set active profile here so I need the context.
context.getEnvironment().setActiveProfiles(APP_ENV.toLowerCase());
PrintVariables();
InitializeLimeService();
InitializeSGCSClient();
}
请注意。我最终创建了一个ApplicationContextInitializer,如下所示
package com.realitylabs.spa.tool;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author Pablo Karlsson
*
* ConfigurableApplicationContextInitializer sets the spring profile
* before application initialization so we can use dependency injection
* based on profile. This allows us to mock services like SGCSClientService
* In development mode.
*/
public class ConfigurableApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext context) {
String APP_ENV = System.getenv("APP_ENV");
if(APP_ENV == null) {
throw new RuntimeException("Please set APP_ENV to production or development");
}
APP_ENV = APP_ENV.toLowerCase();
if(!APP_ENV.equals("development") && !APP_ENV.equals("production")) {
throw new RuntimeException("Please set APP_ENV to production or development");
}
context.getEnvironment().setActiveProfiles(APP_ENV);
}
}
package com.realitylabs.spa.tool;
导入org.springframework.context.ApplicationContextInitializer;
导入org.springframework.context.ConfigurableApplicationContext;
/**
*@作者Pablo Karlsson
*
*ConfigurableApplicationContextInitializer设置弹簧配置文件
*在应用程序初始化之前,我们可以使用依赖项注入
*基于个人资料。这允许我们模拟像SGCSClientService这样的服务
*在开发模式中。
*/
公共类ConfigurableApplicationContextInitializer实现ApplicationContextInitializer{
@凌驾
public void初始化(ConfigurableApplicationContext上下文){
字符串APP_ENV=System.getenv(“APP_ENV”);
如果(APP_ENV==null){
抛出新的运行时异常(“请将APP_ENV设置为production或development”);
}
APP_ENV=APP_ENV.toLowerCase();
如果(!APP_ENV.equals(“开发”)和&!APP_ENV.equals(“生产”)){
抛出新的运行时异常(“请将APP_ENV设置为production或development”);
}
context.getEnvironment().setActiveProfiles(APP_ENV);
}
}
我在web.xml中添加了以下行
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>com.realitylabs.spa.tool.ConfigurableApplicationContextInitializer</param-value>
</context-param>
上下文初始化类
com.realitylabs.spa.tool.ConfigurableApplicationContextInitializer
这真的很有效。谢谢你的帮助 虽然不应该这样设置活动概要文件,但应该在初始化上下文之前进行设置。您只需添加一个
ApplicationContext
类型的字段并在其上放置@Autowired
即可获得访问权限。我尝试了这一点,但它似乎给了我访问权限。不幸的是,在依赖项注入发生之前,我确实需要访问权限。我如何才能做到这一点?我使用web.xml配置,但这不允许我使用env vars设置活动配置文件。谢谢你的评论。是的。设置一个系统属性spring.profiles.active
,它将被使用。否则,创建一个ApplicationCOntextInitializer
,它根据您想要的内容设置活动配置文件(但从您的代码判断,它基本上是一个常量,所以不确定您为什么需要它)。系统属性,如system wide或java env?它确实有效!非常感谢。