Spring Jndi Context and PropertyPlaceholderConfigurer

Using Spring, I want to read a variable in the context of Webspehere.

Read Java environment variable with Websphere

To define data ... inside web.xml

<env-entry> <env-entry-name>varName</env-entry-name> <env-entry-value>56</env-entry-value> <env-entry-type>java.lang.String</env-entry-type> </env-entry> 

To see using java

 Context envEntryContext = (Context) new InitialContext().lookup("java:comp/env"); String mydata = (String)envEntryContext.lookup("varName"); 

But I want to take the data in my common.xml file, for example

 <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/WEB-INF/context/servweb.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders"> <value>true</value> </property> </bean> 

maybe with something like this

  <constructor-arg> <jee:jndi-lookup jndi-name="java:comp/env" default-value="data" /> </constructor-arg> 

but with context for the same as

 Context envEntryContext = (Context) new InitialContext().lookup("java:comp/env"); String mydata = (String)envEntryContext.lookup("varName"); 

maybe something like this:

  <constructor-arg> <jee:jndi-lookup jndi-name="java:comp/env"> <jee:environment> varName=default </jee:environment> </jee:jndi-lookup> 

Does anyone know the right way?

Thanks at Advance

+6
source share
4 answers

You can create your own PropertyPlaceholderConfigurer

 public class JndiPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private String jndiPrefix = "java:comp/env/"; private JndiTemplate jndiTemplate = new JndiTemplate(); @Override protected String resolvePlaceholder(String placeholder, Properties props) { String value = null; value = resolveJndiPlaceholder(placeholder); if (value == null) { value = super.resolvePlaceholder(placeholder, props); } return value; } private String resolveJndiPlaceholder(String placeholder) { try { String value = (String)jndiTemplate.lookup(jndiPrefix + placeholder, String.class); return value; } catch (NamingException e) { // ignore } return null; } public void setJndiPrefix(String jndiPrefix) { this.jndiPrefix = jndiPrefix; } public void setJndiTemplate(JndiTemplate jndiTemplate) { this.jndiTemplate = jndiTemplate; } } 

and then use it in applicationContext.xml

 <bean id="propertyPlaceholderConfigurer" class="mypkg.helper.JndiPropertyPlaceholderConfigurer"> <property name="properties"> <props> <prop key="varName">default</prop> </props> </property> </bean> 

or to define default values ​​in the properties file

 <bean id="propertyPlaceholderConfigurer" class="mypkg.helper.JndiPropertyPlaceholderConfigurer"> <property name="location" value="classpath:/defaults.properties"/> </bean> 
+7
source

I am doing the same in my web application but cannot read from Initialcontext

applicationcontext.xml has

 <bean class="com.test.webappl.JndiPropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> <property name="ignoreResourceNotFound" value="true"/> <property name="location" value="file:c:\my.properties"/> </bean> 

my.properties has

 default_mask=9999 

trying to read

 Context context = new InitialContext(); String resource = context.lookup("java:comp/env/default_mask"); 

but context binding only has an env entry from web.xml, not from the properties file

+1
source

If you just want to get the value of the variable that was defined in the context of the container and use it as a string without creating a placeholder object, you can do the following (this has been tested in Tomcat, but most likely works the same in other containers / JEE- servers such as WebSphere):

Define the environment variable in Tomcat context.xml (or use your own server syntax):

 <Environment type="java.lang.String" name="myString" value="hello"/> 

In the Spring XML text file:

Add the jee namespace to the root element:

 xmlns:jee="http://www.springframework.org/schema/jee" 

and in xsi:schemaLocation :

 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 

Now you can easily find the value (note that you do not need to specify java:/comp/env material java:/comp/env , Spring does this for you):

 <jee:jndi-lookup id="myStringValue" jndi-name="myStringValue" expected-type="java.lang.String" /> 

Then you can use it, for example, pass it to the bean constructor as a reference:

 <bean id="observationFileManager" class="my.service.Bean"> <constructor-arg name="myString" ref="myStringValue" /> </bean> 

bean will get "hello" as its arg constructor.

EDIT:

If you run your Spring context outside the container (Tomcat, Websphere ...) to test integration, the search will not work. Therefore, if you have a special test context, simply add the following String definition that will override jee:lookup and set the value that you want to use for testing:

 <!-- This overrides the jndi jee:lookup used in the real context --> <bean id="mediaFilesBaseDirPath" class="java.lang.String" > <constructor-arg value="Z:" /> </bean> 
+1
source
 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:resources/my-jndi.properties</value> </property> </bean> 

In my-jndi.properties: JNDI-qconnfactory = JMS / QConnFactory

 <jee:jndi-lookup id="connectionFactory" jndi-name="${jndi-qconnfactory}"/> 
0
source

Source: https://habr.com/ru/post/906823/


All Articles