全网最完美地解决使用spring boot的@Value(“${xxx}“)注解时报出的错误:Could not resolve placeholder ‘xxx‘ in value “${xxx}“

导读:本篇文章讲解 全网最完美地解决使用spring boot的@Value(“${xxx}“)注解时报出的错误:Could not resolve placeholder ‘xxx‘ in value “${xxx}“,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1. 复现问题

今天做上传文件的相关功能,但首先在application-local.yml文件中做如下代码的配置:

uploadFile:
  profile: /profile  # 访问路径
  location: D:/project/img  #存储路径
  imgType: png,jpg,jpeg
  fileType: doc,docx,xls,xlsx

同时,创建如下ProfileConfig.java配置类,如下代码所示:

package com.cloud.lowcode.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @author 念兮为美
 * @datetime 2022/12/8 11:20
 * @desc 文件存储配置类
 */
@Configuration
@Data
public class ProfileConfig {

  @Value("${uploadFile.profile}")
  private String profile;

  @Value("${uploadFile.location}")
  private String location;

  @Value("${uploadFile.imgType}")
  private String imgType;

  @Value("${uploadFile.fileType}")
  private String fileType;
}

但在项目启动时,却报出如下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'uploadFile.profile' in value "${uploadFile.profile}"
	at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180)
	at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
	at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
	at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
	at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1321)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
	... 71 common frames omitted

Disconnected from the target VM, address: '127.0.0.1:59479', transport: 'socket'

Process finished with exit code 1

Could not resolve placeholder 'uploadFile.profile' in value "${uploadFile.profile}"

2. 分析问题

将这句错误Could not resolve placeholder 'uploadFile.profile' in value "${uploadFile.profile}"翻译成中文即“无法解析值”${uploadFile.profile}“中的占位符'uploadFile.profile'

为什么无法解析配置文件中的profile占位符呢?

反复分析application-local.yml配置文件和ProfileConfig.java类,并没有发现有什么问题。

经过反复分析得到:由于我是多文件配置(application-local.yml、application-dev.yml、application-prd.yml),文件配置完成后,我并没有重新勾选Profiles。因而,项目启动时,还是读取原来的默认配置,即没有激活新的配置文件,如下图所示:

在这里插入图片描述

3. 解决问题

既然没有重新勾选Profiles,那么勾选上即可,如下图所示:

在这里插入图片描述

此时便可成功启动项目,如下图所示:

在这里插入图片描述

4. 其他解决方案

如果我上述问题的解决方式,无法解决你的这类问题,你可以按如下方式解决:

4.1 检测语法是否正确

@Value(${xxx})注解中不能出现空格或其他符号,如下代码所示:

@Value("${ uploadFile.profile }")
private String profile;

4.2 检测配置文件中是否有进行配置

检查配置文件(.yml)中是否存在@Value("${xxx}")的相关配置,且.yml是否正确配置,比如语法,换行,空格等。

4.3 检测是否增加注解

类上是否使用了@Configuration的注解?如下代码所示:

@Configuration
@Data
public class ProfileConfig {
    ......
}

4.4 检测代码中的书写方式

不要在无参构造器中,进行new对象的操作,否则就会造成@Value注解失败。

4.5 @Value无法注入static属性

使用@Value直接放在static的属性上是无法注入内容的,切记!!!

此方式会一直是null

4.6 检测配置文件的字段与注解值

要保证yml配置文件的字段与@Value注解的参数值一致,如下代码所示:

  1. application.yml
uploadFile:
  profile: "/profile"  # 访问路径
  1. TestConfig.java
@Configuration
@Data
public class ProfileConfig {

  @Value("${uploadfile.profile}")
  private String profile;
}

application.yml中写的是uploadFile,但在TestConfig.java中写的是uploadfile,因而会报出如上错误。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99110.html

(1)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!