我希望公開一個Properties
Spring bean,其值已經通過典型的屬性擴展機制進行了擴展。我正在使用Spring 3.1。讓我離題了。PropertyFactoryBean屬性擴展
考慮以下屬性文件:
server.host=myhost.com
service.url=http://${server.host}/some/endpoint
和Spring XML配置文件中的這一部分:
<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:default.properties</value>
</list>
</property>
</bean>
<context:property-placeholder properties-ref="appProperties" />
我可以寫下面的工作代碼:
@Component
public class MyComponent {
@Autowired
@Qualifier("appProperties")
private Properties appProperties;
@Value("${service.url}")
private String serviceUrl;
// remainder omitted
}
唯一問題是如果我從獲得service.url
的值我得到http://${server.host}/some/endpoint
- 即價值未擴張。但是,如果我從serviceUrl
獲得service.url
的值,則該值已擴展爲:http://myhost.com/some/endpoint
。
有誰知道一個很好的方式來公開一個Properties
實例作爲一個其值已擴展的Spring bean嗎?或者,如果任何人都可以將我指向一個Spring bean(必須是Spring 3.1),它將爲我做擴展,我也會接受它! (有趣的是,如果您手動拉離Environment
或屬性值PropertySource
你會發現,這些也未展開。)
感謝, 道穆埃爾。
也許http://stackoverflow.com/a/7547942/180100 – 2012-10-25 06:34:19
感謝您的鏈接。不幸的是,它並沒有真正解決問題。我可以採用'@ Configuration'方法而不是XML,但是我需要做大量的代碼來手動執行所有的擴展。 – Muel
@Muel,你怎麼看這個庫我寫道:http://owner.aeonbits.org它確實處理可變擴展,它是基於註釋的。我不知道你是否可以和Spring一起使用它。 –