首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过属性文件而不是环境变量或系统属性来设置活动的spring 3.1环境配置文件

问如何通过属性文件而不是环境变量或系统属性来设置活动的spring 3.1环境配置文件
EN

Stack Overflow用户
提问于 2011-12-21 17:21:32
回答 4查看 66.6K关注 0票数 49

我们使用Spring3.1的新环境配置文件功能。我们当前通过在部署应用程序的服务器上设置环境变量spring.profiles.active=xxxxx来设置活动配置文件。

我们认为这是一个次优的解决方案,因为我们想要部署的war文件应该只有一个额外的属性文件,该文件设置了spring应用程序上下文加载的环境,因此部署不依赖于服务器上设置的某个env var。

我试着找出如何做到这一点,并发现:

ConfigurableEnvironment.setActiveProfiles()

我可以使用它来以编程方式设置配置文件,但是我仍然不知道何时何地执行这段代码。在什么地方加载了spring上下文?我可以加载我想要从属性文件传递给方法的参数吗?

更新:我刚刚在docs上找到了一个可以用来设置活动配置文件的工具?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-12-22 16:49:22

只要配置文件名称可以在web.xml中静态提供,或者使用新的无XML配置类型,可以通过编程方式从属性文件加载要设置的配置文件,来自Thomasz的答案就是有效的。

因为我们仍然使用XML版本,所以我进一步研究并发现了以下很好的解决方案,您可以在其中实现自己的ApplicationContextInitializer,您只需将一个带有属性文件的新PropertySource添加到源代码列表中,以搜索特定于环境的配置设置。在下面的示例中,可以在env.properties文件中设置spring.profiles.active属性。

代码语言:javascript
复制
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中

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>
票数 40
EN

Stack Overflow用户

发布于 2011-12-21 17:40:03

在web.xml中

代码语言:javascript
复制
<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>profileName</param-value>
</context-param>

使用WebApplicationInitializer

当您在Servlet3.0环境中没有web.xml文件,并且完全从Java引导Spring时,可以使用这种方法:

代码语言:javascript
复制
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类。

票数 51
EN

Stack Overflow用户

发布于 2012-12-26 17:50:00

出于某种原因,只有一种方法对我有效

代码语言:javascript
复制
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");
    }

……

代码语言:javascript
复制
 <listener>
     <listener-class>somepackahe.ActiveProfileConfiguration</listener-class>
 </listener>
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8587489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档