我将 Spring 4.1.x 与 Play 2.3.x 结合使用,并尝试从 Play 的 application.conf
文件注入(inject)属性值。
因此我可以访问属性:
@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
。
我正在注入(inject)这样的属性值:
@Autowired
public PhotoToUrlMapper(@Value("${photo.url}") url) {
// trim quotes :-/
}
Spring 将用引号注入(inject)值。显然我可以修剪它们,但我希望能够注入(inject)不带引号的网址。
注释:
- 我可能可以创建一个单独的
.properties
文件,但我想避免这种情况(更多配置) - 我可能会使用可怕的静态 Play 的
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>
关于java - Play框架+Spring : Injecting URL from application.conf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29765903/