You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot从application.properties取值为null的原因排查

为什么Spring Boot主类中@Value注解获取属性值为null?

核心原因是Spring的依赖注入仅对容器管理的Bean生效,且主类的初始化时机和普通Bean存在差异:

1. Spring容器的Bean管理逻辑

  • @RestController、@Component标注的类,会被Spring容器自动识别并创建为Bean实例。容器在初始化这些Bean时,会同步解析@Value注解,完成application.properties属性值的注入,因此能正常读取到配置。
  • 主类虽然被@SpringBootApplication标注(该注解包含@Component),会被容器注册为Bean,但问题出在属性访问的时机上:

2. 主类main方法的执行时机问题

main方法是Java程序的静态入口,执行SpringApplication.run()时Spring容器才开始启动。如果在main方法里直接访问主类的@Value字段:

@SpringBootApplication
public class TryValueApplication {
    @Value("${test.value1}")
    private String value1;

    public static void main(String[] args) {
        SpringApplication.run(TryValueApplication.class, args);
        // 此处输出null,因为容器还未完成当前类Bean的@Value注入
        System.out.println(value1);
    }
}

此时Spring容器处于启动阶段,主类的Bean实例尚未完成依赖注入流程,所以@Value标注的字段仍为null。

3. 主类正确获取属性的方式

如果要在主类中读取属性值,需要等到Spring容器完全初始化后再访问。可以通过实现ApplicationRunner或CommandLineRunner接口,在容器启动完成后执行逻辑:

@SpringBootApplication
public class TryValueApplication implements ApplicationRunner {
    @Value("${test.value1}")
    private String value1;
    @Value("${test.value3}")
    private String value3;

    public static void main(String[] args) {
        SpringApplication.run(TryValueApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 容器已完全初始化,@Value字段已完成注入
        System.out.println(value1); // 输出value1
        System.out.println(value3); // 输出value3
    }
}

总结差异

  • 普通Bean(@RestController、@Component):容器在创建实例时同步完成@Value注入,能直接获取属性值。
  • 主类直接在main方法访问:静态入口执行时容器未完成初始化,注入流程未结束,字段为null;需在容器启动完成后访问才能拿到正确值。

内容的提问来源于stack exchange,提问作者Prashant Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 05:50:36