Spring Jndi 上下文和 PropertyPlaceholderConfigurer [英] Spring Jndi Context and PropertyPlaceholderConfigurer
本文介绍了Spring Jndi 上下文和 PropertyPlaceholderConfigurer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Spring,我想在 Webspehere 的上下文中读取一个变量.
Using Spring, I want to read a variable inside the context of Webspehere.
在 web.xml 中定义数据....
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>
用java看
Context envEntryContext = (Context) new InitialContext().lookup("java:comp/env");
String mydata = (String)envEntryContext.lookup("varName");
但我想在我的 common.xml 中获取数据,例如
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>
也许有类似的东西
<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");
也许是这样的:
<constructor-arg>
<jee:jndi-lookup jndi-name="java:comp/env">
<jee:environment>
varName=default
</jee:environment>
</jee:jndi-lookup>
有人知道正确的方法吗?
Anybody knows the correct way?
提前致谢
推荐答案
您可以创建自己的PropertyPlaceholderConfigurer
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;
}
}
然后在你的 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屋!
查看全文