2014-10-07 64 views
1

我正在使用純Java配置(無xml)使用Spring編寫Web應用程序。我想要一個解決方案來顯示各種環境特定的屬性,這取決於我的應用程序運行的位置(dev/test/prod)。使用Spring XML配置,並通過XML的PropertyPlaceholderConfigurer,我會做這樣的事情:使用純java彈簧配置訪問環境屬性

<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> 
    <property name="ignoreResourceNotFound" value="true" /> 
    <property name="locations"> 
     <list> 
      <value>classpath:shift.properties</value> 
      <value>classpath:shift-${env}.properties</value> 
     </list> 
    </property> 
</bean> 

在Java配置中,我想要做的基礎是:

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = { "com.values.shift" }) 
public class WebConfig extends WebMvcConfigurerAdapter { 

@Autowired 
private static Environment springEnv; 

@Bean 
public static PropertyPlaceholderConfigurer propertyConfigurer() throws IOException { 
    PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer(); 
    System.out.println("Environment:" + env.toString()); 
    props.setLocations(
      new Resource[] { 
        new ClassPathResource("shift.properties"), 
        new ClassPathResource("shift-" + springEnv.getProperty("env") + ".properties")} 
      ); 
    props.setIgnoreResourceNotFound(true); 
    return props; 
} 

我在Tomcat上將-Denv = localhost設置爲VM參數。我也有它設置爲我的mac系統屬性(即在終端輸出本地主機echo $ env)

我似乎無法弄清楚如何訪問使用純Java的環境變量。我試過以下內容:

  • 使用Spring Environment,如上面的代碼所示。
  • @Value(「#{systemEnvironment ['env']}」)爲新變量並以字符串形式訪問它
  • @Value(「#{systemProperties ['env']}」)和訪問它作爲一個字符串
  • @Value(「$ {ENV}」)的一個新的變量和訪問它作爲一個字符串

上述所有回報空的。很高興看到一個如何在Spring中使用純Java配置訪問環境變量的工作示例。

感謝您的幫助。

+0

是位於shift-local.properties文件中的'env'屬性,或者您只是嘗試使用@Value(「$ {env}」)在String字段中獲取'local'? – freakman 2014-10-07 12:59:25

+0

我試圖讓字符串字段中的'local'。然後,該字符串將形成我的環境特定屬性文件名的一部分。 'env'是傳入的變量名稱。 – Gabe 2014-10-07 13:21:15

回答

0

它試圖現在在屬性文件中查找'env'屬性。

您錯過了在PropertyPlaceholderConfigurer上使用systemPropertiesModeName方法,這會使您的@Value(「#{systemProperties ['env']}」)有效。

//編輯:

不要在靜態字段中使用@Autowired。

//編輯2:

這是即時通訊使用的是什麼:

@Configuration 
public class PropertiesConfig { 

@Bean 
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
    return PropertyLoadHelper.getPropertySourcesPlaceholderConfigurer(); 
} 

public static class PropertyLoadHelper { 
    public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() { 
     PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer(); 
     properties.setLocations(new ClassPathResource[]{ 
       new ClassPathResource("config/app." + System.getenv("ENV") + "properties"), 
       new ClassPathResource("config/local.app." + System.getenv("ENV") + "properties"), 
       new ClassPathResource("config/application.properties") 
     }); 
     properties.setBeanName("app"); 
     properties.setLocalOverride(true); 
     properties.setIgnoreResourceNotFound(true); 
     return properties; 
    } 


    public static Properties loadProperties(String propertiesPath) { 
     PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); 
     propertiesFactoryBean.setLocation(new ClassPathResource(propertiesPath)); 
     Properties properties = null; 
     try { 
      propertiesFactoryBean.afterPropertiesSet(); 
      properties = propertiesFactoryBean.getObject(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return properties; 
    } 

} 

}

區別:使用 PropertySourcesPlaceholderConfigurer,System.getenv改用自動裝配Autowired環境。也許環境不能在設置PropertyPlaceHolder bean之前使用?

+0

感謝您的建議。我已經看過了,看起來如果我沒有設置它Spring檢查我的應用程序屬性文件後應該'回退'到系統屬性:「默認是」後備「:如果無法解析佔位符使用指定的屬性,系統屬性將被嘗試。「 – Gabe 2014-10-07 13:24:32

+0

你是對的:)你springEnv字段是靜態的,這是不是最好的選擇,而使用@Autowired – freakman 2014-10-07 13:30:44

+0

我有這樣的變量作爲靜態的,因爲我是在靜態propertyConfigurer方法(我已閱讀應該是靜態的,以及內訪問它)雖然它們都不是靜態的,但它仍然是空的,如果你有一個使用你所描述的方法的工作示例,那麼可能還有其他的事情正在進行中 – Gabe 2014-10-07 13:49:17