0

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

Mark Comix
  • 1,878
  • 7
  • 28
  • 44

1 Answers1

0

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.

Community
  • 1
  • 1
Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
  • But I need to do this for, at least JDBC and Log4j. I need a solution for those 2 files and maybe on the future another files – Mark Comix Sep 12 '12 at 13:59
  • In any case it is better to deploy your log4j.properties outside your war. If that's not want you want then you can consider setting JVM properties when starting tomcat and reuse these in the log4j.properties or to have different log4j.properties and tell tomcat which to use by setting the JVM property `-Dlog4j.configuration=log4j.xml` – Stijn Geukens Sep 12 '12 at 14:29
  • That's what I'm trying to doing. I have the external "jdbc.properties" and "log4j.properties" files. I think that you are not understanding what I'm saying – Mark Comix Sep 12 '12 at 14:38
  • I would still use JNDI for the database properties. But the JVM options mentioned above should work as well. – Stijn Geukens Sep 12 '12 at 14:55
  • Can you give me an example of JVM properties? Thanks – Mark Comix Sep 12 '12 at 14:57