如何通过属性文件而不是通过环境变量或系统属性设置活动的 spring 3.1 环境配置文件 [英] How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property

查看:48
本文介绍了如何通过属性文件而不是通过环境变量或系统属性设置活动的 spring 3.1 环境配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We use the new environment profiles feature of spring 3.1. We currently set the active profile by setting the environment variable spring.profiles.active=xxxxx on the server to which we deploy the application.

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

We think this is a suboptimal solution as the war file we want to deploy should just have an additional properties file which sets the environment in which the spring app context should load so the deployment is not dependent on some env var set on the server.

我试图弄清楚如何做到这一点并发现:

I tried to figure out how to do that and found:

ConfigurableEnvironment.setActiveProfiles()

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

which I can use to programmatically set the profile but then I still don't know where and when to execute this code. Somewhere where the spring context loads up? Can I load the parameter I want to pass to the method from a properties file?

更新:我刚刚在 docs 找到我可以实现它来设置活动配置文件吗?

UPDATE: I just found at docs which I might be able to implement to set the active profile?

推荐答案

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

The answer from Thomasz is valid as long as the profile name can be provided statically in the web.xml or one uses the new XML-less configuration type where one could programmatically load the profile to set from a properties file.

由于我们仍在使用 XML 版本,我进一步调查并找到了以下不错的解决方案,您可以在其中实现自己的 ApplicationContextInitializer,您只需将带有属性文件的新 PropertySource 添加到要搜索的源列表中用于环境特定的配置设置.在下面的示例中,可以在 env.properties 文件中设置 spring.profiles.active 属性.

As we still use the XML version I investigated further and found the following nice solution where you implement your own ApplicationContextInitializer where you just add a new PropertySource with a properties file to the list of sources to search for environment specific configuration settings. in the example below one could set the spring.profiles.active property in the env.properties file.

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 中:

You then need to add that initializer as a parameter to the ContextLoaderListener of spring as follows to your 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>

这篇关于如何通过属性文件而不是通过环境变量或系统属性设置活动的 spring 3.1 环境配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆