基于Spring的Web应用程序的环境特定配置? [英] Environment-specific configuration for a Spring-based web application?

查看:127
本文介绍了基于Spring的Web应用程序的环境特定配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道Web应用程序的部署环境,例如无论是本地的,dev,qa还是prod等。有没有办法可以在运行时在spring应用程序上下文文件中确定?

How can I know the deployment environment of a web application, e.g. whether it is local, dev, qa or prod, etc. Is there any way I can determine this in spring application context file at runtime?

推荐答案

不要在代码中添加逻辑来测试你正在运行的环境 - 这是一个灾难的秘诀(或至少燃烧很多午夜的油)。

Don't add logic to your code to test which environment you're running in - that is a recipe for disaster (or at least burning a lot of midnight oil down the road).

你使用Spring,所以利用它。使用依赖注入为您的代码提供环境特定的参数。例如。如果您需要在测试和生产中调用具有不同端点的Web服务,请执行以下操作:

You use Spring, so take advantage of it. Use dependency injection to provide environment-specific parameters to your code. E.g. if you need to call a web service with different endpoints in test and production, do something like this:

public class ServiceFacade {
    private String endpoint;

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public void doStuffWithWebService() {
        // use the value of endpoint to construct client
    }
}

接下来,使用Spring的PropertyPlaceholderConfigurer (或者PropertyOverrideConfigurer)从.properties文件或JVM系统属性中填充此属性,如下所示:

Next, use Spring's PropertyPlaceholderConfigurer (or alternatively PropertyOverrideConfigurer) to populate this property from either a .properties file, or from a JVM system property like so:

<bean id="serviceFacade" class="ServiceFacade">
    <property name="endpoint" value="${env.endpoint}"/>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:environment.properties</value>
    </property>
</bean>

现在创建两个(或三个或四个)这样的文件 - 每个不同的环境一个。

Now create two (or three, or four) files like so - one for each of the different environments.

在environment-dev.properties中:

In environment-dev.properties:

env.endpoint=http://dev-server:8080/

在environment-test.properties:

In environment-test.properties:

env.endpoint=http://test-server:8080/

现在为每个环境采取适当的属性文件,将其重命名为environment.properties,并将其复制到您的应用服务器的lib目录或其他地方将出现在您的应用程式的classpath。例如。对于Tomcat:

Now take the appropriate properties file for each environment, rename it to just environment.properties, and copy it to your app server's lib directory or somewhere else where it will appear on your app's classpath. E.g. for Tomcat:

cp environment-dev.properties $CATALINA_HOME/lib/environment.properties

现在部署您的应用程序 - 当Spring设置您的端点属性时,Spring将替换值http:// dev-server:8080 /运行时。

Now deploy your app - Spring will substitute the value "http://dev-server:8080/" when it sets up your endpoint property at runtime.

有关如何加载属性值的更多详细信息,请参阅Spring文档。

See the Spring docs for more details on how to load the property values.

这篇关于基于Spring的Web应用程序的环境特定配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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