简体   繁体   English

Play Framework + Spring:从application.conf注入URL

[英]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: 笔记:

  • I could probably create a separate .properties file, but I'd like to avoid that (more configuration) 我可能可以创建一个单独的.properties文件,但我想避免这种情况(更多配置)
  • I could probably use horrible static Play's 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.

相关问题 如何从 play 框架中的 application.conf 文件中读取属性? - How to read a property from the application.conf file in play framework? 在GlobalSettings onStart(Play Framework)中访问application.conf - Accessing application.conf in GlobalSettings onStart (Play Framework) Play Framework 2.7.0 Config,无需通过application.conf进行常规配置即可进行测试 - Play Framework 2.7.0 Config for tests without general configurations from application.conf 无法从Play框架2.4中的cron作业加载application.conf - Not able to load application.conf from cron job in play framework 2.4 使用 Play 从 java 类访问 application.conf 属性! 2.0 - Accessing the application.conf properties from java class with Play! 2.0 Java Play 覆盖 application.conf 中的端口 - Java Play overwrites port in application.conf 在Play Java应用中标记化application.conf - tokenize application.conf in play java app 如何在Glassfish上部署Play Framework 2.4应用程序并阅读application.conf - How to deploy play framework 2.4 application on glassfish and read application.conf 无法访问Play框架中的Cloud Foundry自动配置(application.conf文件) - Not able to access cloud foundry auto configuration in play framework (application.conf file) 如何在Play Framework(Java)2.3.x中的application.conf文件中为键设置值 - How to set a value to a key in application.conf file in Play Framework (Java) 2.3.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM