Spring-boot @Value绑定问题

时间:2015-06-10 13:29:29

标签: spring spring-boot

面对Spring-boot属性解析器的一个奇怪问题。

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

1 个答案:

答案 0 :(得分:2)

我猜您在Web容器中将应用程序部署为战争,但您需要确认。您需要将以下方法添加到您的班级。查看相关的posting

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