【发布时间】:2017-01-15 22:55:36
【问题描述】:
我试图在变量上使用@Value 注释来分配一个值,但是被分配的值是空的。我究竟做错了什么?我正在使用@Configuration 使用java spring 配置。我还使用@PropertySource 指向属性文件。
@Configuration
@PropertySource("classpath:application.properties")
public class SpringConfiguration {
@Value("${app_prop_1}") Integer aValue;
@Bean
public Integer getInteger() {
return new Integer(aValue); // throws NullPointerException
}
}
application.properties 文件位于项目的 maven 资源文件夹中。内容如下:
app_prop_1=2000
【问题讨论】:
-
提示:根据jira.spring.io/browse/SPR-8539 的cmets,不建议在
@Configuration中使用@Value,而应使用@Inject Environment env;然后env.getProperty("app_prop_")
标签: spring