2012-01-25 54 views
6

使用Spring,我想读取Webspehere上下文中的变量。Spring Jndi上下文和PropertyPlaceholderConfigurer

Read a Environment Variable in Java with 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> 

要看到用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/env" default-value="data" /> 
    </constructor-arg> 

但上下文做

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> 

任何人都知道正确的方法是什么?

由于提前

回答

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; 
    } 
} 

,然后在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> 
+0

作品,没有问题..非常感谢你 – Kaltresian

0
<bean id="propertyConfigurer" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<property name="location"> 
<value>classpath:resources/my-jndi.properties</value>  
</property> 
</bean> 

在my-jndi.properties: JNDI-qconnfactory = JMS/QConnFactory

<jee:jndi-lookup id="connectionFactory" jndi-name="${jndi-qconnfactory}"/> 
1

我在做同样的事情在我的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> 

my.properties有

default_mask=9999 

试图读取

Context context = new InitialContext(); 
String resource = context.lookup("java:comp/env/default_mask"); 

但上下文的绑定只有从网络ENV条目。xml,而不是属性文件

1

如果您只想获取在容器上下文中定义的变量的值并将其用作字符串而不创建占位符对象,则可以执行以下操作(此操作已经过测试在Tomcat中,但最有可能工作在其他容器/ JEE服务器,比如WebSphere)相同:

定义Tomcat的context.xml环境变量(或使用自己的服务器的语法):

<Environment type="java.lang.String" name="myString" value="hello"/> 

在Spring XML上下文文件:

的JEE命名空间添加到根元素:

xmlns:jee="http://www.springframework.org/schema/jee" 

,并在该xsi:schemaLocation

http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 

现在你可以很容易地找到一个值(请注意,您不必指定java:/comp/env东西,春天会替你):

<jee:jndi-lookup id="myStringValue" 
        jndi-name="myStringValue" 
        expected-type="java.lang.String" /> 

然后你可以使用它,例如把它传递给一个bean的构造函数作为参考ence:

<bean id="observationFileManager" class="my.service.Bean"> 
    <constructor-arg name="myString" ref="myStringValue" /> 
</bean> 

这个豆将收到"hello"作为其constructor arg。

编辑:

如果您运行的容器(Tomcat的,...的Websphere)进行集成测试之外的Spring上下文,查找将无法正常工作。所以,如果你有一个特殊的测试情况下,只需添加以下String定义,将覆盖jee:lookup和设置要用于测试的值:

<!-- This overrides the jndi jee:lookup used in the real context --> 
<bean id="mediaFilesBaseDirPath" class="java.lang.String" > 
    <constructor-arg value="Z:" /> 
</bean> 
相关问题