Spring Jndi上下文和PropertyPlaceholderConfigurer [英] Spring Jndi Context and PropertyPlaceholderConfigurer

查看:208
本文介绍了Spring Jndi上下文和PropertyPlaceholderConfigurer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





定义数据。 ... 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>

使用java查看

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

但是我想把数据放在我的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>

可能是这样的东西

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

但是上下文做同样的

 上下文envEntryContext =(Context)new InitialContext()。lookup(java:comp / env); 
String mydata =(String)envEntryContext.lookup(varName);

可能是这样的:

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

任何人都知道正确的方式?



感谢提前

解决方案

您可以创建自己的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);
}
返回值;
}

private String resolveJndiPlaceholder(String placeholder){
try {
String value =(String)jndiTemplate.lookup(jndiPrefix + placeholder,String.class);
返回值;
} catch(NamingException e){
//忽略
}
返回null;
}

public void setJndiPrefix(String jndiPrefix){
this.jndiPrefix = jndiPrefix;
}

public void setJndiTemplate(JndiTemplate jndiTemplate){
this.jndiTemplate = jndiTemplate;
}
}

然后在您的 applicationContext.xml

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

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


$ b $
class =mypkg.helper.JndiPropertyPlaceholderConfigurer>
< property name =locationvalue =classpath:/defaults.properties/>
< / bean>


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

解决方案

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>

这篇关于Spring Jndi上下文和PropertyPlaceholderConfigurer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆