[英]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.