2014-03-13 79 views
0

我已在XML文件中的下列配置一塊:春XML +性能配置Java類

<util:properties id="apiConfigurator" location="classpath:api.properties" scope="singleton"/> 

,這裏是我的屬性文件:

appKey=abc 
appSecret=def 

在我的春天類我得到一些的值是這樣的:

@Value("#{apiConfigurator['appKey']}") 

我想在Spring中創建一個@Configuration類來解析屬性f在這種方式

@Value("#{apiConfigurator['appKey']}") 

仍然工作徹底我使用這個類。我如何正確地做到這一點?

+0

嘗試[這](http://fahdshariff.blogspot.co.uk/2012/09/spring-3-javaconfig-loading -properties.html)。 –

+0

它看起來很接近,但它沒有說任何關於給配置設置一個id,並使其可以像'我想要的'類似''的方式來訪問:apiConfigurator ['foo'] – marcelorocks

回答

2

當您指定

<util:properties .../> 

春季註冊了一個PropertiesFactoryBean豆的名稱/ ID,您還規定。

所有你需要做的是提供這樣的@Bean自己

// it's singleton by default 
@Bean(name = "apiConfigurator") // this is the bean id 
public PropertiesFactoryBean factoryBean() { 
    PropertiesFactoryBean bean = new PropertiesFactoryBean(); 
    bean.setLocation(new ClassPathResource("api.properties")); 
    return bean; 
} 
+0

就是這樣。謝謝! – marcelorocks