简体   繁体   English

动态更改jdbc.properties和log4j.properties的文件目录

[英]Dynamic Change of files directory for jdbc.properties and log4j.properties

I'm working in a Java Web Project. 我正在一个Java Web项目中工作。 I need to change the folder of the files "jdbc.properties" and "log4j.properties" depending of the environment, because testing, demo and release have diferent values for those files. 我需要根据环境更改文件“ jdbc.properties”和“ log4j.properties”的文件夹,因为测试,演示和发行版对这些文件具有不同的值。

I have this folders and subfolders: 我有以下文件夹和子文件夹:

c:\\myProject\\conf\\dev c:\\myProject\\conf\\test c:\\myProject\\conf\\demo c:\\ myProject \\ conf \\ dev c:\\ myProject \\ conf \\ test c:\\ myProject \\ conf \\ demo

I need to put diferent jdbc.properties and log4j.properties files in each of those folders 我需要将不同的jdbc.properties和log4j.properties文件放在每个文件夹中

c:\\myProject\\conf\\dev\\log4j.properties c:\\myProject\\conf\\dev\\jdbc.properties c:\\ myProject \\ conf \\ dev \\ log4j.properties c:\\ myProject \\ conf \\ dev \\ jdbc.properties

c:\\myProject\\conf\\test\\log4j.properties c:\\myProject\\conf\\test\\jdbc.properties c:\\ myProject \\ conf \\ test \\ log4j.properties c:\\ myProject \\ conf \\ test \\ jdbc.properties

c:\\myProject\\conf\\demo\\log4j.properties c:\\myProject\\conf\\demo\\jdbc.properties c:\\ myProject \\ conf \\ demo \\ log4j.properties c:\\ myProject \\ conf \\ demo \\ jdbc.properties

The three project are in the same Server and in the same Apache (It is a Web Project) 这三个项目位于同一服务器和同一Apache中(这是一个Web项目)

First i made some changes to use a windows system variable to get the parent folder (c:\\myProject). 首先,我进行了一些更改,以使用Windows系统变量来获取父文件夹(c:\\ myProject)。 To do that, i made this on Spring appContext file: 为此,我在Spring appContext文件上进行了此操作:

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:${PARENT_FOLDER}/conf/dev/jdbc.properties</value> 
</property>
</bean>

"PARENT_FOLDER" is defined on Windows environment variables/system variable 在Windows环境变量/系统变量中定义了“ PARENT_FOLDER”

Those changes works OK. 这些更改正常。

But, as you can see, I always loking for file on "/conf/dev" 但是,正如您所看到的,我一直在寻找“ / conf / dev”上的文件

I need to make dynamic the "dev" part of the path. 我需要使动态成为路径的“开发”部分。

I Can't use Windows environment variables/system variable because the 3 environments are deployed on the same Server. 我不能使用Windows环境变量/系统变量,因为3个环境部署在同一服务器上。

I'm trying to use a "property" (using ) on web.xml, but I don't know how to find the property in my Spring appContext file. 我正在尝试在web.xml上使用“属性”(使用),但是我不知道如何在Spring appContext文件中找到该属性。

I definy the property like this: 我这样定义属性:

<env-entry>
    <env-entry-name>ENVIRONMENT</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>Dev</env-entry-value>
</env-entry>

But I don't know how to access "ENVIRONMENT" property on Spring 但我不知道如何在Spring上访问“环境”属性

I don't know what to do. 我不知道该怎么办。 I a little desperate 我有点绝望

Can someone help me? 有人能帮我吗?

Thanks and sorry for my poor english 谢谢,抱歉我的英语不好

Have you considered using JNDI ? 您是否考虑过使用JNDI

With JNDI you will define the db connection properties inside tomcat itself. 使用JNDI,您将在tomcat本身内定义数据库连接属性。 This way your spring configuration is independent of the environment and you can deploy the same war on all environments. 这样,您的spring配置就独立于环境,并且您可以在所有环境中部署相同的战争。 See also this . 另请参见

If you need to run it locally that you can always use the 'new' spring environment profiles feature. 如果您需要在本地运行它,则可以始终使用“新” spring环境配置文件功能。

Other option (if JNDI is not an option and assuming you use maven) is the maven replacer plugin where you will generate the db.properties at build time. 其他选项(如果不是JNDI,并且假定您使用maven)是maven替换插件,您将在构建时在其中生成db.properties。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM