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

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

问题描述

我如何知道 Web 应用程序的部署环境,例如无论是本地、开发、qa 还是生产等.有什么办法可以在运行时在 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 目录或其他地方,它将出现在您的应用程序的类路径中.例如.对于 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 将在运行时设置您的端点属性时替换值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天全站免登陆