我有一個Spring 3.1應用程序,我嘗試在上下文文件中使用系統變量。變量「JAVA_MY_ENV」在我的系統上定義(在Windows上,它位於控制面板的「系統變量」中)。
在web.xml中,我可以把它作爲一個變量,它的工作原理,它是成功的變量的實際值替換(假設「電鑄」):
<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>
但在「APP-config.xml中」,我的其他方面的文件之一,我嘗試這樣做,它沒有按'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構造函數?
問題是我沒有用「-DJAVA_MY_ENV = electrotype」啓動我的應用程序,而是將JAVA_MY_ENV設置爲系統全局變量。在我的應用程序中,System.getenv(「JAVA_MY_ENV」)返回「電子類型」,但System.getProperty(「JAVA_MY_ENV」)返回NULL。 @Value(「#{systemProperties ['JAVA_MY_ENV']}」)也是NULL。我會優先考慮能夠使用web.xml使用的*完全相同的* $ {JAVA_MY_ENV}變量,以確保該值也相同!通過PropertyPlaceholderConfigurer,我不確定它是否是「-D」var或全局系統屬性(如果我因某種原因而使用了兩天)。謝謝。 – electrotype 2012-04-22 19:01:35