Spring Jndi Context和PropertyPlaceholderConfigurer

Kal*_*ian 6 websphere spring jndi environment-variables

使用Spring,我想在Webspehere的上下文中读取一个变量.

使用Websphere在Java中读取环境变量

在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>
Run Code Online (Sandbox Code Playgroud)

用java看看

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

但我想把我的common.xml中的数据拿出来

<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>
Run Code Online (Sandbox Code Playgroud)

也许有类似的东西

 <constructor-arg>
     <jee:jndi-lookup jndi-name="java:comp/env" default-value="data" /> 
  </constructor-arg>
Run Code Online (Sandbox Code Playgroud)

但是用上下文做同样的事情

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

也许是这样的:

 <constructor-arg>
    <jee:jndi-lookup  jndi-name="java:comp/env">
      <jee:environment>
          varName=default
     </jee:environment>
  </jee:jndi-lookup>
Run Code Online (Sandbox Code Playgroud)

谁知道正确的方法?

提前致谢

ben*_*y23 7

您可以创建自己的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的 applicationContext.xml

<bean id="propertyPlaceholderConfigurer"
      class="mypkg.helper.JndiPropertyPlaceholderConfigurer">
    <property name="properties">
        <props>
            <prop key="varName">default</prop>
        </props>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

或者用于定义属性文件中的默认值

<bean id="propertyPlaceholderConfigurer"
      class="mypkg.helper.JndiPropertyPlaceholderConfigurer">
    <property name="location" value="classpath:/defaults.properties"/>
</bean>
Run Code Online (Sandbox Code Playgroud)