[英]Play Framework + Spring: Injecting URL from application.conf
I'm using Spring 4.1.x with Play 2.3.x and I'm trying to inject a property value from Play's application.conf
file. 我将Spring 4.1.x与Play 2.3.x结合使用,并且尝试从Play的application.conf
文件中注入属性值。
I can access properties thanks to this: 由于以下原因,我可以访问属性:
@PropertySource("classpath:application.conf")
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
I have a URL value in my application.conf
: 我的application.conf
有一个URL值:
photo.url="https://our-photo-storage.net"
Note the quotes . 注意引号 。 If I remove them, Play will throw com.typesafe.config.ConfigException$Parse
. 如果删除它们,Play将抛出com.typesafe.config.ConfigException$Parse
。
I am injecting property value like this: 我正在像这样注入属性值:
@Autowired
public PhotoToUrlMapper(@Value("${photo.url}") url) {
// trim quotes :-/
}
Spring will inject the value with the quotes . Spring将使用引号将值注入。 I can obviously just trim them, but I'd like to be able to inject the url without the quotes. 我显然可以修剪它们,但是我希望能够插入不带引号的网址。
Notes: 笔记:
.properties
file, but I'd like to avoid that (more configuration) 我可能可以创建一个单独的.properties
文件,但我想避免这种情况(更多配置) Play.current.configuration.getString("photo.url")
, but I actually like to write unit tests ... 我可能会使用可怕的静态Play的Play.current.configuration.getString("photo.url")
,但实际上我想编写单元测试... Is there a reasonable workaround for this? 有合理的解决方法吗?
I guess the only way to so is to use the properties file, i don't think that's too much configuration to do, since it provides you with a better readability for your code. 我想这样做的唯一方法是使用属性文件,我认为这不需要太多配置,因为它为您的代码提供了更好的可读性。 quoted a sample of the code you need to include your properties file 引用了包含属性文件所需的代码示例
<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.