本文介绍了如何通过属性文件而不是通过环境变量或系统属性设置活动的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应用上下文的环境,因此部署不依赖于on服务器.

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:

我可以使用它来以编程方式设置配置文件,但是我仍然不知道在哪里以及何时执行此代码. 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?

更新:我刚刚在文档中找到我可能可以实现设置活动配置文件吗?

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

推荐答案

托马斯的答案是有效的,只要可以在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中,如下所示::

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环境配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 11:23