繁体   English   中英

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将使用引号将值注入。 我显然可以修剪它们,但是我希望能够插入不带引号的网址。

笔记:

  • 我可能可以创建一个单独的.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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM