Spring Jndi上下文和PropertyPlaceHolderConfigure
使用Spring,我想在Websphere的上下文中读取一个变量 要定义数据。。。。在web.xml内部Spring Jndi上下文和PropertyPlaceHolderConfigure,spring,websphere,environment-variables,jndi,Spring,Websphere,Environment Variables,Jndi,使用Spring,我想在Websphere的上下文中读取一个变量 要定义数据。。。。在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>
<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>
但是我想在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/env">
<jee:environment>
varName=default
</jee:environment>
</jee:jndi-lookup>
varName=默认值
有人知道正确的方法吗
提前感谢您可以创建自己的PropertyPlaceHolderConfiguration
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>
违约
或用于定义特性文件中的默认值
<bean id="propertyPlaceholderConfigurer"
class="mypkg.helper.JndiPropertyPlaceholderConfigurer">
<property name="location" value="classpath:/defaults.properties"/>
</bean>
类路径:resources/my-jndi.properties
在my-jndi.properties中:
jndi qconnfactory=jms/qconnfactory
<jee:jndi-lookup id="connectionFactory" jndi-name="${jndi-qconnfactory}"/>
我在我的Web应用程序中执行相同的操作,但无法从Initialcontext读取
applicationcontext.xml已被删除
<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>
试图阅读
Context context = new InitialContext();
String resource = context.lookup("java:comp/env/default_mask");
但是上下文的绑定只有web.xml中的env条目,而不是属性文件中的env条目如果您只想获取容器上下文中定义的变量的值并将其用作字符串而不创建占位符对象,则可以执行以下操作(这在Tomcat中进行了测试,但很可能在其他容器/JEE服务器(如WebSphere)中也适用):
<bean id="propertyPlaceholderConfigurer"
class="mypkg.helper.JndiPropertyPlaceholderConfigurer">
<property name="location" value="classpath:/defaults.properties"/>
</bean>
在Tomcat的context.xml
中定义环境变量(或使用您自己的服务器语法):
在xsi:schemaLocation
中:
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
现在,您可以轻松地查找一个值(请注意,您不必指定java:/comp/env
stuff,Spring会为您这样做):
很好,没有问题非常感谢
<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>
default_mask=9999
Context context = new InitialContext();
String resource = context.lookup("java:comp/env/default_mask");
<Environment type="java.lang.String" name="myString" value="hello"/>
xmlns:jee="http://www.springframework.org/schema/jee"
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
<jee:jndi-lookup id="myStringValue"
jndi-name="myStringValue"
expected-type="java.lang.String" />
<bean id="observationFileManager" class="my.service.Bean">
<constructor-arg name="myString" ref="myStringValue" />
</bean>
<!-- This overrides the jndi jee:lookup used in the real context -->
<bean id="mediaFilesBaseDirPath" class="java.lang.String" >
<constructor-arg value="Z:" />
</bean>