SpringBoot启动报错Injection of autowired dependencies failed; Could not resolve placeholder: 问题

2022年7月19日09:13:14

问题现象:

SpringBoot项目读取配置文件中参数给类中的变量赋值时,,报错:

Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'XXX'; |nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testClass': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException:  Could not resolve placeholder 'test1.test2.test3' in value "${test1.test2.test3}"

问题原因:

(1)首先就是要检查你的拼写有没有问题,在@Value注解中写的跟配置文件中写的单词是不是一样!!

(2)配置或依赖问题;

解决办法:

(1)检查@Value注解中的参数路径与配置文件中是否一样,就是'test1.test2.test3'这个有没有写错,很多人都是因为拼写错了,以为是程序和配置问题,结果弄了半天发现方向错了;

(2)配置和使用方法,分三步:

第一步:添加依赖,在pom文件中添加

<!-- 自动读取配置文件,赋值给成员变量依赖 -->

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-configuration-processor</artifactId>

    <optional>true</optional>

</dependency>

第二步:在application.yml中设置配置

test1:

  test2:

    test3: 11111111

第三步:在要读取application.yml中数值的类上,加上注解

@ConfigurationProperties(prefix = "test1")

public class TencentSmsSender extends AbstractSmsSender {
      @Value("${test1.test2.test3}")

       private String sdkAppId;

    ...................

}
 

只要这简单三步就可以了。

  • 作者:坐等海枯
  • 原文链接:https://blog.csdn.net/xinbadar/article/details/119772469
    更新时间:2022年7月19日09:13:14 ,共 930 字。