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

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

问题描述

我有好几次碰到了同样的问题,我想有一些输入什么其他人思考的问题:
假设我们有打包为的.war 文件的J2EE应用程序,我们要在若干环境运行。 (开发/测试/ preprod /生产/等)

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 J2EE application packaged as a .war file and we want to run it on several environments. (development/test/preprod/prod/etc)

有关访问应用程序,(数据库/ web服务等),我们将访问信息在配置文件中所需的基础设施,也有一些业务配置中的那些文件。
比方说,我们使用的.properties 为此文件(因为我们有战争内部的Spring应用程序,我们喜欢具有由一个班轮在appcontext读属性)
 并且也假设在不同的环境中,我们不具有相同的应用服务器/ servlet容器。 (例如:开发,测试:码头,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.

现在最近我面临着运行的操作一个人的问题:
所以我们真的必须生成一个新的构建与上buildserver适当的配置文件,如果DB是preprod环境的变化?
我回答:'不,你其实可以去... / webapps /下currentApp / WEB-INF / classes中/配置/ 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 Assembly插件,我们嵌入一个码头的的,这使得它可作为一个可执行的战争,也让我们有一个全球性的XML配置的可能性战,
从嵌入式码头的启动创建/修改在战争爆发目录中适当的.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.

但同样,这并不如果你想使用码头别的其他解决问题。

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

如何处理同样的情况?

推荐答案

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

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目录的属性/ 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的环境。想到这情景:质量保证和放大器;内置PROD WAR文件,QA测试,QA战争的文件,部署在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配置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>

请参阅春天配置的所有变量和往常一样。

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队部署与相应的环境文件相同的神器。如果有什么打击了,我们知道它只是ENV。被搞砸的属性。
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

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

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