为AbstractAnnotationConfigDispatcherServletInitializer设置可与@PropertySource一起使用的活动配置文件? [英] Setting an active profile for AbstractAnnotationConfigDispatcherServletInitializer that can be use with @PropertySource?
问题描述
我正在使用AbstractAnnotationConfigDispatcherServletInitializer
来配置我的Web应用程序.我还具有用于创建一些bean的@Configuration
类.在此类中,我使用@PropertySource
批注为各种设置(例如数据库连接详细信息)加载属性文件.
I'm using the AbstractAnnotationConfigDispatcherServletInitializer
to configure my web application. I also have an @Configuration
class I use for creating a few beans. In this class, I use the @PropertySource
annotation to load a properties file for various settings (e.g. database connection details).
当前,我将Maven配置文件与Ant任务结合使用,以为我的运行时环境创建正确的属性文件.也就是说,在构建时,我得到Maven将"prod.properties"或"dev.properties"移动到"application.properties"(该类使用的).我想做的是使用Spring配置文件来消除这种情况.我希望能够执行以下操作:
Currently, I use Maven profiles with Ant tasks to create the correct properties file(s) for my runtime environment. That is, I get Maven to move a "prod.properties" or "dev.properties" to "application.properties" (which the class uses) at build time. What I would like to do is use Spring profiles to eliminate this. I would like to be able to do the following:
@PropertySource( value = "classpath:/application-${spring.profiles.active}.properties")
我也想在不使用任何XML的情况下设置配置文件.因此,我需要根据系统属性的存在来设置配置文件.例如,
I also want to set the profile without using any XML. So I would need to set the profile based on the presence of a system property. For example,
String currentEnvironment = systemProperties.getProperty("current.environment");
if (currentEnvironment == null) {
((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles("production");
} else {
((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles(currentEnvironment);
}
不过,我不确定在哪里可以做到这一点.根据一个相关问题的答案,可以通过覆盖初始化类中的createRootApplicationContext
方法来实现.但是,该答案还取决于设置配置文件之前要加载的配置类.
I am not sure where I could do this, though. According to an answer to a related question, this could be done in an override of the createRootApplicationContext
method in my initializer class. But, that answer also relies on the configuration classes being loaded before setting the profile.
我想做些什么吗?如果可以,怎么办?
Is what I want to do possible? If so, how?
推荐答案
覆盖createRootApplicationContext
或createServletApplicationContext
不适用于我.我遇到各种错误,例如非法状态异常和"$ {spring.profiles.active}"无法解决.挖掘AbstractAnnotationConfigDispatcherServletInitializer
的继承树,我设计了以下解决方案:
Overriding createRootApplicationContext
or createServletApplicationContext
was not working for me. I was getting various errors like illegal state exceptions and "${spring.profiles.active}" not being resolvable. Digging through the inheritance tree for AbstractAnnotationConfigDispatcherServletInitializer
I devised the following solution:
public class ApplicationInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
public void onStartup(ServletContext context) throws ServletException {
super.onStartup(context);
String activeProfile = System.getProperty("your.profile.property");
if (activeProfile == null) {
activeProfile = "prod"; // or whatever you want the default to be
}
context.setInitParameter("spring.profiles.active", activeProfile);
}
}
现在,您可以创建如下配置类,它将正常工作:
Now you can create a configuration class like the following and it will work just fine:
@Configuration
@PropertySource( value = "classpath:application-${spring.profiles.active}.properties" )
public class MyAppBeans {
@Autowired
private Environment env;
@Bean
public Object coolBean() {
String initParam = this.env.getProperty("cool.bean.initParam");
...
return coolBean;
}
}
当然,您可以通过VM选项(-Dyour.profile.property=dev
)或容器属性(例如Tomcat容器属性)设置"your.profile.property".
Of course, you would set the "your.profile.property" via VM options (-Dyour.profile.property=dev
) or container properties (e.g. Tomcat container properties).
这篇关于为AbstractAnnotationConfigDispatcherServletInitializer设置可与@PropertySource一起使用的活动配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!