2012-04-22 19 views
2

我有一個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構造函數?

回答

4

由於3.0 Spring,你應該是價值注入特性

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

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

另外,您可以考慮使用PropertySourcesPlaceholderConfigurer或同級。創建這個會告訴Spring如何查找變量。我經常製作一些屬性文件,以便應用程序可以使用環境和內部屬性文件值。例如

<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」被設置爲真。這告訴spring使用env變量(這是你想要的)

+0

問題是我沒有用「-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

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的解決價值!

然後我可以在任何可以使用bean ref的地方使用這個bean。例如作爲構造精氨酸:

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

所以,現在我使用$ {} JAVA_MY_ENV在它被解釋的地方,而不會增加PropertyPlaceholderConfigurer和JAVA_MY_ENV豆否則。

相關問題