簡體   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