2

Spring 3.1 アプリケーションがあり、コンテキスト ファイルでシステム変数を使用しようとしています。変数「JAVA_MY_ENV」はシステムで定義されています(Windowsでは、コントロールパネルの「システム変数」にあります)。

web.xml では、変数として使用でき、動作します。変数の実際の値 (「electrotype」としましょう) に正常に置き換えられます。

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> 
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log/${JAVA_MY_ENV}.log4j.properties</param-value>
</context-param>

メインの「Bean」コンテキストで使用して、インポートを行うこともできます。これも機能します。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"       
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <!-- (...) -->

    <import resource="classpath:spring/app-config.xml" />
    <import resource="classpath:spring/env/context-env-${JAVA_MY_ENV}.xml" />

</beans>

しかし、私の他のコンテキストファイルの1つである「app-config.xml」で、これを試してみましたが、機能しません:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"       
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <bean id="appConfiguration" class="com.xxx.app.AppConfiguration">
        <constructor-arg value="${JAVA_MY_ENV}" />
    </bean>

</beans>

com.xxx.app.AppConfiguration は、解釈された値ではなく、文字列 "${JAVA_MY_ENV}" をコンストラクタ パラメータとして受け取ります。

${} 変数が解釈される場所と解釈されない場所がわかりません。

解釈された ${JAVA_MY_ENV} 値を com.xxx.app.AppConfiguration コンストラクターに渡す方法はありますか?

4

3 に答える 3

4

Springの3.0以降、プロパティに値を注入する必要があります

@Value("#{ systemProperties['JAVA_MY_ENV'] }") 
private String myVar;

また

<property name ="myVar" value="#{systemProperties['JAVA_MY_ENV']}"/>

または、PropertySourcesPlaceholderConfigurer または同様のクラスの使用を検討することもできます。これを作成すると、春に変数を探す方法が伝えられます。多くの場合、多くのプロパティ ファイルも作成して、環境および内部プロパティ ファイルの値をアプリで使用できるようにします。例えば

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <list>
        <value>classpath:someprops.properties</value>
    </list>
  </property>
  <property name="ignoreResourceNotFound" value="true" />
  <property name="searchSystemEnvironment" value="true" />
  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />

上記の例の重要な要素は、「searchSystemEnvironment」が true に設定されていることです。これは春にenv変数を使用するように指示します(これはあなたが望むものです)

于 2012-04-22T18:03:01.123 に答える
2

子 (Web) コンテキストにアクセスできない <context:property-placeholder> プロパティの重複の可能性 私が理解しているように、これは予想される動作です。サーブレット コンテキストでのみ Bean を注入するか、構成 Bean を servlet-context.xml に含める必要があります。

于 2012-04-23T08:16:44.097 に答える
0

BruceLowe によって提案された PropertyPlaceholderConfigurer を使用するには、機能します。特定のプロパティを解決するために私が見つけた別の方法(おそらくもっと複雑)は、次を使用しています:

<bean id="JAVA_MY_ENV" class="org.springframework.util.SystemPropertyUtils" factory-method="resolvePlaceholders">
    <constructor-arg value="${JAVA_MY_ENV}" /> 
</bean>

これにより、解決された${JAVA_MY_ENV} の値を含むString Bean が作成されます。

次に、Bean ref を使用できる場所ならどこでもこの Bean を使用できます。たとえば、constructor-arg として:

<bean id="appConfiguration" class="com.xxx.app.AppConfiguration">
    <constructor-arg ref="JAVA_MY_ENV" />
</bean>

したがって、PropertyPlaceholderConfigurer を追加せずに、解釈される場所で ${JAVA_MY_ENV} を使用し、それ以外の場合は JAVA_MY_ENV Bean を使用します。

于 2012-04-23T01:40:29.170 に答える