Read environment variable from applicationContext.xml

I need to read the environment variable defined in my web.xml

<env-entry> <description>Path Repositorio NFS</description> <env-entry-name>PATH_ENV</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>C:/V3</env-entry-value> </env-entry> 

from my applicationContext.xml

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="${PATH_ENV}/myprop.properties" /> </bean> 

How can i do this?


Finally, I did the following:

1 Define the environment variable in context.xml:

 <Environment name="PATH_ENV" type="java.lang.String"/> 

2 Define env-entry in web.xml

 <env-entry> <description>Path Repositorio NFS</description> <env-entry-name>PATH_ENV</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>/WEB-INF/</env-entry-value> </env-entry> 

3 Define in applicationContext.xml

 <bean id="configurationPath" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/PATH_ENV</value> </property> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <bean factory-bean="configurationPath" factory-method="concat"> <constructor-arg value="myprop.properties"/> </bean> </property> </bean> 

This works correctly, but if I define the full path in:

 <env-entry-value>C:/V3/</env-entry-value> 

I have the following problem:

 java.io.FileNotFoundException: Could not open ServletContext resource [/C:/V3/aesantasa.properties] 

I cannot determine the full path in env-entry-value Why?

+7
source share
4 answers

You can search for JNDI entries (both environment entries and resources) using JndiObjectFactoryBean or <jee:jndi-lookup> :

 <jee:jndi-lookup id="PATH_ENV" jndi-name="PATH_ENV"/> 

(To use the jee namespace, you must declare it).

Defines a spring bean named "PATH_ENV" that contains (as a string) the path configured in the environment record. Now you can enter it in another beans:

 <bean class="xy.Foo"> <property name="path" ref="PATH_ENV"/> </bean> 

The rest of the difficulty is string concatenation. (Unfortunately, there is no JndiPlaceholderConfigurer that would replace placeholders with JNDI environment entries, so you cannot use the ${property}/foo syntax for concatenation and must provide another bean definition:

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <bean factory-bean="PATH_ENV" factory-method="concat"> <constructor-arg>/myprop.properties</constructor-arg> </bean> </property> </bean> 

(the code has not been tested since I do not have a spring project to test it)

+4
source

You can use context-param , which will work.

 <context-param> <param-name>PATH_ENV</param-name> <param-value>C:/V3</param-value> </context-param> 
0
source

Why not just use the following?

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="file:C:/V3/myprop.properties"/> </bean> 
0
source

I decided something seems to be similar.

I create a Windows system variable with the changing part of the path:

 my computer --> advanced options --> environment options --> Systeme Variable 

And then with this I end the path in the Spring AppContext as follows:

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

I don’t know if I really help, but it works for me

-one
source

All Articles