• SpringBoot中yml文件key值不匹配,引发的空指针异常解决办法


    . 问题背景

    很多小伙伴都知道SpringBoot中常见的配置文件有三种,分别是yml、yaml、properties三种类型。

    而有些时候我们需要在SpringBoot配置文件中去配置一些自定义的内容,比如邮箱的秘钥信息以及短信秘钥信息等。而配置完毕之后我们就需要进行配置的读取,在SpringBoot常见读取自定义配置信息的方式有两种,分别为@Value以及@ConfigurationProperties。

    另外我们要注意,使用@ConfigurationProperties的时候,需要在pom文件中引入

    spring-boot-configuration-processor的起步依赖。但是在读取配置数据的过程中,如果不注意细节的话,就会容易引发让人迷惑的空指针异常!!!

    接下来壹哥就用一个实际案例,给大家重新这个异常的产生及解决办法。

    二. 实际案例

    为了给大家讲清楚今天的这个异常,壹哥先给大家设计一个案例,实现过程如下。

    2.1 创建学生简介类

    1. public class StudentIntroduce {
    2. private int id;
    3. private String detailed;
    4. public StudentIntroduce() {
    5. }
    6. public StudentIntroduce(int id, String detailed) {
    7. this.id = id;
    8. this.detailed = detailed;
    9. }
    10. public int getId() {
    11. return id;
    12. }
    13. public void setId(int id) {
    14. this.id = id;
    15. }
    16. public String getDetailed() {
    17. return detailed;
    18. }
    19. public void setDetailed(String detailed) {
    20. this.detailed = detailed;
    21. }
    22. @Override
    23. public String toString() {
    24. return "StudentIntroduce{" +
    25. "id=" + id +
    26. ", detailed='" + detailed + '\'' +
    27. '}';
    28. }
    29. }

    2.2 准备一个学生类同时关联学生简介类

    1. public class Student {
    2. private String name;
    3. private String sex;
    4. private StudentIntroduce studentIntroduce;
    5. public Student() {
    6. }
    7. public Student(String name, String sex, StudentIntroduce studentIntroduce) {
    8. this.name = name;
    9. this.sex = sex;
    10. this.studentIntroduce = studentIntroduce;
    11. }
    12. public String getName() {
    13. return name;
    14. }
    15. public void setName(String name) {
    16. this.name = name;
    17. }
    18. public String getSex() {
    19. return sex;
    20. }
    21. public void setSex(String sex) {
    22. this.sex = sex;
    23. }
    24. public StudentIntroduce getStudentIntroduce() {
    25. return studentIntroduce;
    26. }
    27. public void setStudentIntroduce(StudentIntroduce studentIntroduce) {
    28. this.studentIntroduce = studentIntroduce;
    29. }
    30. @Override
    31. public String toString() {
    32. return "Student{" +
    33. "name='" + name + '\'' +
    34. ", sex='" + sex + '\'' +
    35. ", studentIntroduce=" + studentIntroduce +
    36. '}';
    37. }
    38. }

    2.3 在yml文件中配置学生类需要的数据

    yaml文件内容如下:

    1. student:
    2. name: zhangsan
    3. sex: male
    4. introduce:
    5. id: 1
    6. detailed: The student is very excellent !!!

    2.4 读取yml文件的数据

    我们选择@ConfigurationProperties方式读取,此时在学生类上方加上注解并指定yml中的前缀。

     Tips:一定要在学生类以及学生简介类中国提供getter和setter方法,不然读取数据注入将会失败,因为@ConfigurationProperties注入数据的原理,是根据set方法实现的。

    2.5 准备配置类将学生对象注入进IOC容器

    1. @Configuration
    2. public class StudentConfig {
    3. @Bean
    4. public Student getStudentDetail(){
    5. return new Student();
    6. }
    7. }

    2.6 在启动类中获取容器并得到学生类的bean对象

    1. @SpringBootApplication
    2. public class GateWayService9527 {
    3. public static void main(String[] args) {
    4. ConfigurableApplicationContext context = SpringApplication.run(GateWayService9527.class);
    5. Student student = (Student) context.getBean("getStudentDetail");
    6. StudentIntroduce studentIntroduce = student.getStudentIntroduce();
    7. System.out.println(studentIntroduce.getDetailed());
    8. }
    9. }

    2.7 运行启动类会发现控制台出现空指针异常

    2.8 原因解析

    2.9 解决办法以及建议

    在yaml文件中做自定义配置时,需要将配置的key名与注入类名的变量名一致!!!

    1. student:
    2. name: zhangsan
    3. sex: male
    4. studentIntroduce: ##修改位置
    5. id: 1
    6. detailed: The student is very excellent !!!

    重新运行启动类!!!

    三. 总结

    本文出现的问题只是在开发中比较常见的一些问题,但对于我们开发人员来讲,我们在完成业务开发的同时,也应该追求细致以及善于预判和分析,这样才能让我们在互联网这个领域不断积累丰富的经验!!!

  • 相关阅读:
    Spark---持久化,共享变量和RDD之间的依赖关系详解
    C 语言 while 和 do...while 循环
    程序员面对生活
    es6 Set和Map方法
    QLayout布局间消除间隙和QLayout边缘空白调整
    Linux——iMX6ULL的启动过程详细解析(启动模式配置、启动设备配置、镜像烧写imx文件基本组成)
    Phoenix Framework
    【数据结构】A : A DS图_传递信息
    【JUC系列-10】深入理解ArrayBlockingQueue的底层原理
    [Android][JAVA][kotlin] 获取各种语言的星期,月份,长月份,缩写月份
  • 原文地址:https://blog.csdn.net/syc000666/article/details/127727951