Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a tomcat Environment variable in a PropertyPlaceholderConfigurer

I try to get an Environment variable specified in Tomcat's server.xml in a 'PropertyPlaceholderConfigurer' located in my jpa-spring.xml file.

So far, the setup looks as follows:

Tomcat server.xml

<Environment description="Identifies the server environement" 
             name="server-env" 
             type="java.lang.String" 
             value="dev" />

The in WebContent/META-INF/context.xml:

<Context>
    <ResourceLink name="server-env" global="server-env" type="java.lang.String"/>
</Context>

Which is referenced in WebContent/WEB-INF/web.xml like:

<resource-env-ref>
    <description>Identifies server environement</description>
    <resource-env-ref-name>server-env</resource-env-ref-name>
    <resource-env-ref-type>java.lang.String</resource-env-ref-type>
</resource-env-ref>

<!-- Spring Integration -->

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/config/jpa-spring.xml
</param-value>
</context-param>

And in /WEB-INF/config/jpa-spring.xml I try to get that variable as a replacement:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>WEB-INF/config/db.${server-env}.properties</value>
        </list>
    </property>
</bean>

This is a setup I put together using information from several articles found on the web.

However, I get an error like ...

Could not resolve placeholder 'server-env' in [WEB-INF/config/db.${server-env}.properties] as system property: neither system property nor environment variable found
05 Nov 2011 14:45:13,385 org.springframework.web.context.ContextLoader
ERROR Context initialization failed

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/config/db.${server-env}.properties]

... when starting tomcat.

What is the right approach to achieve what I am looking for?

I know that this question is similar to this, and this question. However, I even couldn't figure it out with the information from these answers.

like image 898
Rico Leuthold Avatar asked Feb 23 '23 12:02

Rico Leuthold


1 Answers

Here are my suggestion

  • With the current set up, its really going to be complicated to read JNDI property server-env and use the same to load the property file.
  • The way you have assembled the spring application (and PropertyPlaceholderConfigurer), spring will try to look for the property server-env first in OS environment then in java system properties (passed from command using -D option). It finds it at neither of these places and hence fails.
  • So currently the easiest way out right now is to pass the value of server-env form command prompt of your application server (where you invoke java ; typical syntax would be -Dserver-env=dev). I leave this to you to figure out.
  • if above option appears a bit complicated, another easier way out is set an environment variable with name server-env to its appropriate values (on Windows its set server-env=dev. Plz check respect OS documentations for this).
like image 150
Santosh Avatar answered Feb 26 '23 21:02

Santosh