【Java基础】Idea启动Spring boot失败之Could not resolve placeholder解决全攻略

刚刚新接了一个Spring boot项目,在Idea启动项目失败,组件依赖注入失败Injection of autowired dependencies failed,根异常是Could not resolve placeholder。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataFakerRunner': Unsatisfied dependency expressed through field 'dataFakerService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataFakerService': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'expressionFile' in value "${expressionFile}"

这是spring boot 启动时对组件进行初始化,没有读到相应的配置文件或配置项导致的。出现这个异常主要有以下几种场景:

1、配置文件中没有对应的配置

对于这种情况,在配置文件中增加配置项即可。

2、配置文件没有成功加载

配置文件没有成功加载有多种原因:

2.1、配置文件没有在项目resource目录。这时增加配置文件即可。
2.2、resource目录没有被识别为资源目录。这里右击resource目录->Mark Directory as-> Resource Root即可。

在这里插入图片描述

2.3、多个profile,目标profile没有被激活。

这时可以:
2.3.1 在application.properties中配置激活spring.profiles.active=dev
在这里插入图片描述
2.3.2 在Run/debug Configurations中配置。可以在VM options里配置-Dspring.profiles.active=dev, 或在active profiles中直接配置目标profile dev。二选一。
在这里插入图片描述

2.4、pom文件build标签里excludes了目标配置。这时在pom文件上注释掉excludes即可。这个是最隐藏的一项了,前面的都排查过了,如果没有排查这里的话,很容易怀疑人生!
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <excludes>
          <exclude>*</exclude>
        </excludes>
      </resource>
    </resources>
  </build>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值