我们使用Spring3.1的新环境配置文件功能。我们当前通过在部署应用程序的服务器上设置环境变量spring.profiles.active=xxxxx来设置活动配置文件。
我们认为这是一个次优的解决方案,因为我们想要部署的war文件应该只有一个额外的属性文件,该文件设置了spring应用程序上下文加载的环境,因此部署不依赖于服务器上设置的某个env var。
我试着找出如何做到这一点,并发现:
ConfigurableEnvironment.setActiveProfiles()
我可以使用它来以编程方式设置配置文件,但是我仍然不知道何时何地执行这段代码。在什么地方加载了spring上下文?我可以加载我想要从属性文件传递给方法的参数吗?
更新:我刚刚在docs上找到了一个可以用来设置活动配置文件的工具?
发布于 2011-12-22 16:49:22
只要配置文件名称可以在web.xml中静态提供,或者使用新的无XML配置类型,可以通过编程方式从属性文件加载要设置的配置文件,来自Thomasz的答案就是有效的。
因为我们仍然使用XML版本,所以我进一步研究并发现了以下很好的解决方案,您可以在其中实现自己的ApplicationContextInitializer,您只需将一个带有属性文件的新PropertySource添加到源代码列表中,以搜索特定于环境的配置设置。在下面的示例中,可以在env.properties文件中设置spring.profiles.active属性。
public class P13nApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static Logger LOG = LoggerFactory.getLogger(P13nApplicationContextInitializer.class);
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
try {
environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env.properties"));
LOG.info("env.properties loaded");
} catch (IOException e) {
// it's ok if the file is not there. we will just log that info.
LOG.info("didn't find env.properties in classpath so not loading it in the AppContextInitialized");
}
}
}然后,您需要将该初始化器作为参数添加到spring的ContextLoaderListener中,如下所示添加到web.xml中
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>somepackage.P13nApplicationContextInitializer</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>您还可以将其应用于DispatcherServlet
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextInitializerClasses</param-name>
<param-value>somepackage.P13nApplicationContextInitializer</param-value>
</init-param>
</servlet>发布于 2011-12-21 17:40:03
在web.xml中
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>profileName</param-value>
</context-param>使用WebApplicationInitializer
当您在Servlet3.0环境中没有web.xml文件,并且完全从Java引导Spring时,可以使用这种方法:
class SpringInitializer extends WebApplicationInitializer {
void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.getEnvironment().setActiveProfiles("profileName");
rootContext.register(SpringConfiguration.class);
container.addListener(new ContextLoaderListener(rootContext));
}
}其中使用@Configuration注释SpringConfiguration类。
发布于 2012-12-26 17:50:00
出于某种原因,只有一种方法对我有效
public class ActiveProfileConfiguration implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.setProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "dev");
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
}……
<listener>
<listener-class>somepackahe.ActiveProfileConfiguration</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>https://stackoverflow.com/questions/8587489
复制相似问题