Spring Jndi Context and PropertyPlaceholderConfigurer

北城以北 提交于 2019-11-29 10:39:29

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 your applicationContext.xml

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

or for defining the default values in a properties file

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

I'm doing the same thing in my webapplication but unable to 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 the context's binding has only env-entry from web.xml, not from the properties file

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

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

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

In the Spring XML context file :

Add the jee namespace to the root element :

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

and in the the xsi:schemaLocation :

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

Now you can easily look up a value (note that you don't have to specify the java:/comp/env stuff, Spring does that 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 constructor of a bean as a reference :

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

The bean will receive "hello" as its construcotr arg.

EDIT :

If you run your Spring context outside the container (Tomcat, Websphere...) for integration testing, the lookup will not work. So if you have a special test context, just add the following String definition that will overrides the jee:lookup and set the value 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>
<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}"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!