![](/img/trans.png)
[英]How to read a property from the application.conf file in play framework?
[英]Play Framework + Spring: Injecting URL from application.conf
我將Spring 4.1.x與Play 2.3.x結合使用,並且嘗試從Play的application.conf
文件中注入屬性值。
由於以下原因,我可以訪問屬性:
@PropertySource("classpath:application.conf")
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
我的application.conf
有一個URL值:
photo.url="https://our-photo-storage.net"
注意引號 。 如果刪除它們,Play將拋出com.typesafe.config.ConfigException$Parse
。
我正在像這樣注入屬性值:
@Autowired
public PhotoToUrlMapper(@Value("${photo.url}") url) {
// trim quotes :-/
}
Spring將使用引號將值注入。 我顯然可以修剪它們,但是我希望能夠插入不帶引號的網址。
筆記:
.properties
文件,但我想避免這種情況(更多配置) Play.current.configuration.getString("photo.url")
,但實際上我想編寫單元測試... 有合理的解決方法嗎?
我想這樣做的唯一方法是使用屬性文件,我認為這不需要太多配置,因為它為您的代碼提供了更好的可讀性。 引用了包含屬性文件所需的代碼示例
<bean id="placeholderProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/url.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="1" />
</bean>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.