Spring-boot @Value 绑定问题

Spring-boot @Value binding Issue

遇到 spring-boot 属性-resolver 的奇怪问题。

正在尝试为其中一个属性做一个简单的@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}"

我猜您将应用部署为 war 在 Web 容器中,但您需要确认。您需要将以下方法添加到您的 class。看一个相关的posting

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