2012-01-25 70 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> 
相關問題