简体   繁体   中英

Dynamic Change of files directory for jdbc.properties and log4j.properties

I'm working in a Java Web Project. 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.

I have this folders and subfolders:

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

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\\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)

First i made some changes to use a windows system variable to get the parent folder (c:\\myProject). To do that, i made this on Spring appContext file:

<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

Those changes works OK.

But, as you can see, I always loking for file on "/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.

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.

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

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 ?

With JNDI you will define the db connection properties inside tomcat itself. This way your spring configuration is independent of the environment and you can deploy the same war on all environments. See also this .

If you need to run it locally that you can always use the 'new' spring environment profiles feature.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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