【问题标题】:Spring @Value assigning null to variable. @PropertySourceSpring @Value 将 null 分配给变量。 @PropertySource
【发布时间】: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

【问题讨论】:

标签: spring


【解决方案1】:

添加@Value注解时,有需要添加

//To resolve ${} in @Value
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
    return new PropertySourcesPlaceholderConfigurer();
}

这将解析${} 表达式。

Source

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 2020-10-07
  • 2023-01-07
  • 2011-04-23
相关资源
最近更新 更多