私は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>
私も使用することができます私のメインの「豆」文脈でそれ、インポートを行うには、それはまた、動作します:
<?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>
しかし、「アプリ-config.xmlの」、私の他のコンテキストファイルの一つで、私はこれを試してみて、それdoesnの't仕事:
<?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コンストラクタに渡す方法はありますか? 3.0のよう
"-DJAVA_MY_ENV = electrotype"でアプリケーションを起動しないで、代わりにJAVA_MY_ENVをシステムグローバル変数に設定します。私のアプリでは、System.getenv( "JAVA_MY_ENV")は "electrotype"を返しますが、System.getProperty( "JAVA_MY_ENV")はNULLを返します。 @Value( "#{systemProperties ['JAVA_MY_ENV']}")もNULLです。 web.xmlが使用する* $ {JAVA_MY_ENV}変数と同じ値を使うことができれば、値も同じであることを確認できます。 PropertyPlaceholderConfigurerを使用すると、 "-D" varかグローバルシステムプロパティ(何らかの理由でいずれかの日に両方を使用する場合)であるかどうかはわかりません。ありがとう。 – electrotype