8

I need read an 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 have done the next:

1 Define 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 is run correctly, However if I define a full path in:

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

I have the next problem:

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

I cant define a full path in env-entry-value Why?

1
  • In case anyone comes by this question, it should've been <env-entry-value>file:C:/V3/</env-entry-value> Commented Feb 25, 2014 at 16:33

4 Answers 4

4

You can lookup JNDI entries (both environment entries and resources) with the JndiObjectFactoryBean or <jee:jndi-lookup>:

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

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

That defines a spring bean named "PATH_ENV" that contains (as a string) the path configured int the environment entry. You can now inject it into other beans:

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

The remaining difficulty is concatenating the strings. (Unfortunately, there is no JndiPlaceholderConfigurer that would replace placeholders with JNDI environment entries, so you can't use the ${property}/foo syntax to concatenate, and must supply yet 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> 

(code untested as I don't have a Spring project at hand to test it)

Sign up to request clarification or add additional context in comments.

4 Comments

I cant read the context-param.Is this the correct way to read the context-param in applicationContext.xml ? value="${PATH_ENV}/myprop.properties"
A context param is not the same as an environment entry. Your question says environment entry (<env-entry>), so why are you now talking about context parameters (<context-param>)? As for how to refer to it, I'll edit to include a sample.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PATH_ENV': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: El nombre PATH_ENV no este asociado a este contexto
This solution was most right for me, except that one needs to make sure that PATH_ENV starts with "file:"
0

You can use context-param, that will work.

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

Comments

0

Why not just use the following?

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

2 Comments

the properties files need to have a relative path based on the same variable
why don't you put .properties files in resources or the similar folder in your project which is in classpath?
-1

I solved something, I think, 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 complete the path on Spring AppContext like this:

<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 really help, but for me works

1 Comment

My problem is that the Variable cant be a System Variable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.