Spring Web 应用程序中配置文件的处理 [英] Handling of configuration files in Spring web applications

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

问题描述

我有好几次遇到同样的问题,我想就其他人对这个问题的看法提供一些意见:假设我们将 Spring 应用程序打包为 .war 文件,并且我们希望在 多个环境 上运行它.(开发/测试/预生产/生产/等)

I have several times ran into the same problem, and I would like to have some input on what other people think about the issue: Suppose we have Spring application packaged as a .war file and we want to run it on several environments. (development/test/preprod/prod/etc)

为了访问应用程序所需的基础设施(数据库/网络服务等),我们将访问信息存储在配置文件中,一些业务配置也在这些文件中.假设我们为此目的使用 .properties 文件(因为我们在 war 中有一个 spring 应用程序,我们喜欢在 appcontext 中由单行读取属性)并假设在不同的环境中我们没有相同的 appserver/servlet 容器.(例如:dev、test:jetty、preprod:tomcat、prod:glassfish)

For accessing the infrastructure needed by the application, (databases/webservices etc) we store the access info in configuration files, also some business configuration is in those files. Let's say we use .properties files for this purpose (because we have a spring application inside the war and we like having the properties read by a one-liner in the appcontext) and also suppose that in the different environments we don't have the same appserver/servlet container. (eg: dev, test: jetty, preprod: tomcat, prod: glassfish)

我通常所做的是创建多个 Maven 配置文件,每个环境一个,每个环境所需的配置文件.

What I usually have done is creating multiple Maven profiles, one for each environment, the needed configuration in the appropriate files for each.

最近我遇到了一个运行操作人员的问题:那么,如果在预生产环境中更改数据库,我们真的必须在构建服务器上使用适当的配置文件生成新构建吗?"我回答'不,你实际上可以去 .../webapps/currentApp/WEB-INF/classes/config/application.properties 并更改那里的值,然后重新启动容器'

Now recently I have faced a question from a guy running operations: 'So really we have to generate a new build with the appropriate profile on the buildserver if the DB is changed in preprod environment?' I answered 'No, you can actually go to .../webapps/currentApp/WEB-INF/classes/config/application.properties and change the values there, then restart the container'

我们提出了一个解决方案来解决这个问题的某些方面:使用 Maven 程序集插件,我们在 war 中嵌入了一个 Jetty inside,这使它可以用作可执行"war,也使我们有可能拥有全局配置 XML,嵌入式 Jetty 的启动器从中创建/修改展开的 war 目录中相应的 .properties 文件,然后才启动应用程序.

We have come up with a solution which solves some aspects of this issue: using Maven assembly plugin we embed a Jetty inside the war which makes it usable as an 'executable' war, also giving us the possibility to have a global configuration XML, from which the starter of the embedded Jetty creates/modifies the appropriate .properties files in the exploded war directory and only then starts the application.

但是,如果您想使用除 Jetty 以外的任何其他东西,这同样不能解决问题.

But again this doesn't solve the issue if you want to use anything else other than Jetty.

大家是如何处理同样情况的?

How is everyone dealing with the same situation?

推荐答案

环境变量,外部配置文件

我们有类似的东西,一个在 Tomcat/Weblogic 和 Spring 中运行的 Web 应用程序.我们所做的是定义一个 环境属性 CONFIG_PATH 并放置所有 XML(包括 spring 配置)和该目录中的属性文件.

Environment variable, external config files

We have something similar, a Web Application running in Tomcat/Weblogic with Spring. What we do is define a environment property CONFIG_PATH and put all the XMLs (including spring config) and properties files in that directory.

我们有多个属性文件(每个环境),我们将其作为 tar 文件发送.Web 应用程序从 CONFIG_PATH 目录加载所有 Properties/Spring 配置文件.这个变量在各自的环境中被定义为环境变量

We have multiple properties files (per environment) that we send it as a tar file. The Web app loads all the Properties/Spring config files from the CONFIG_PATH directory. This variable is defined as Environment variable in the respective environment

这样我们就不会接触 WAR 文件,也不会为环境构建单独的 WAR.想想这个场景:QA &构建了 PROD WAR 文件,QA 测试了 QA war 文件,在 PROD 中部署了 PROD WAR 但有些事情发生了 :(

This way we are not touching the WAR file nor building separate WAR for environment. Think of this scenario: QA & PROD WAR files built, QA tested QA war file, PROD WAR deployed in PROD but something blows up :(

我们做如下事情:

在spring config xml中,我们定义:

In spring config xml, we define:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="0"></property>
    <property name="locations">
        <list>
            <value>file:${CONFIG_PATH}/App.properties</value>
        </list>
    </property>
</bean>

在 spring 配置中照常引用所有变量.

Refer all variables as usual in spring config.

在 web.xml 中我们定义 spring 配置如下:

In web.xml we define spring config as below:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>file:${CONFIG_PATH}/**/spring-config.xml
    </param-value>
</context-param>

QA/PROD 团队使用相应的环境文件部署相同的工件.如果有什么事情发生了,我们只知道它的环境.乱七八糟的属性.HTH

The QA/PROD team deploys the same artifact with their corresponding environment files. If something blows up we know its only the env. properties that are messed up. HTH

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

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