2012-01-25 12 views
6

Springを使用して、Webspehereのコンテキスト内で変数を読み取る必要があります。Spring JndiコンテキストとPropertyPlaceholderConfigurer

Read a Environment Variable in Java with Websphere

は、java

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

を表示するには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> 

内....データを定義するには、しかし、私は私のする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"> 
     <jee:environment> 
      varName=default 
    </jee:environment> 
    </jee:jndi-lookup> 

誰が正しい方法を知っている:は多分その

<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”); 

は多分そのような何かという同じことをしますか?アドバンス

答えて

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> 
my-jndi.propertiesで
+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> 

: JNDI-qconnfactory =のJMS/QConnFactory

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

私は私のwebアプリケーションで同じことをやってますがInitialContextの

のApplicationContextから読み取ることができませんよ。 XMLは

0を持っている

<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-entryを持っています。xml、プロパティファイルからではありません

1

コンテナコンテキストで定義された変数の値を取得し、プレースホルダオブジェクトを作成せずに文字列として使用する場合は、 )Tomcatのではなく、最も可能性の高いWebSphereなど他の容器/ JEEサーバで同じに動作します:

は、Tomcatのcontext.xml(または独自のサーバーの構文を使用)で環境変数を定義します。春のXMLで

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

をコンテキストファイル:

ルート要素にJEEの名前空間を追加します。あなたは指定する必要はありませんのでご注意今、あなたは簡単に(値を調べることができます

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

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

xsi:schemaLocationjava:/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> 

Beanはそのconstrucotrの引数として"hello"を受け取ることになります。

EDIT:

あなたが統合テストのためのコンテナ(Tomcatのは、WebSphere ...)外のごSpringコンテキストを実行すると、検索が機能しません。あなたは特別なテストコンテキストを持っているのであれば、ちょうどjee:lookupを無効にして、テストのために使用する値に設定されます、次のString定義を追加:問題なく

<!-- This overrides the jndi jee:lookup used in the real context --> 
<bean id="mediaFilesBaseDirPath" class="java.lang.String" > 
    <constructor-arg value="Z:" /> 
</bean> 
関連する問題