我正在使用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 将注入带有引号的值。我显然可以修剪它们,但我希望能够注入没有引号的 url。
笔记:
- 我可能会创建一个单独的
.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>