Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

websphere - Spring Jndi Context and PropertyPlaceholderConfigurer

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

Read a Environment Variable in Java with Websphere

To define the 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 with java

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

But I want take the data in my common.xml like

<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 that

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

but with context for do the same that

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

maybe something like that:

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

Anybody knows the correct way?

Thanks in Advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...