Play Framework + Spring:从 application.conf 注入 URL
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 将用引号 注入值 。我显然只能 trim 它们,但我希望能够注入 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>
我将 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 将用引号 注入值 。我显然只能 trim 它们,但我希望能够注入 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>