2015-06-10 79 views
1

面對spring-boot屬性解析器的一個奇怪問題。Spring-boot @Value綁定問題

試圖爲其中一個屬性做一個簡單的@Value。在調試日誌中,我看到正在匹配的屬性,但是報告它以後無法找到幾行。任何想法出了什麼問題?

Java代碼:

@Configuration 
public class TestAppConfig{ 

    @Value("${appName}") 
    private String applicationName; 
} 

登錄以下

10 Jun 2015 18:41:42 org.springframework.core.env.PropertySourcesPropertyResolver DEBUG {Searching for key 'appName' in [applicationConfig: [classpath:/config/application-dev.yml]]} 
10 Jun 2015 18:41:42 org.springframework.core.env.PropertySourcesPropertyResolver DEBUG {Searching for key 'appName' in [applicationConfig: [classpath:/config/application.yml]]} 
10 Jun 2015 18:41:42 org.springframework.core.env.PropertySourcesPropertyResolver DEBUG {Found key 'appName' in [applicationConfig: [classpath:/config/application.yml]] with type [String] and value 'testApp'} 

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'appName' in string value "${appName}" 

回答

2

我猜你部署你的應用程序作爲Web容器內的戰爭,但你需要確認。您需要將以下方法添加到您的課程中。看一個相關的posting

@Bean 
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { 
    return new PropertySourcesPlaceholderConfigurer(); 
} 
+0

有一個重複的PropertyPlaceholderConfigurer定義在其中一個XML配置導致此問題。謝謝!這種情況發生在WAR模式和啓動模式(mvn spring-boot:run) –