在通过SpringBoot读取配置文件信息时,读取失败抛出异常.

java.lang.IllegalStateException: Failed to load ApplicationContextat org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emailConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'mail.host' in value "${mail.host}"at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)... 24 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'mail.host' in value "${mail.host}"at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172)at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237)at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211)at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:851)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1188)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)... 40 more

大概是原因没有获得配置文件中的属性信息.
属性文件

# Email config
api.aliyun.email.mail.host=smtp.aliyun.com
api.aliyun.email.mail.transport.protocol=smtp
api.aliyun.email.mail.service.address=XXX@aliyun.com
api.aliyun.email.mail.service.password=XXXX

原因,自己在写获取属性路径时,@ConfigurationProperties注解没有指向配置文件的最后一级.

@Component
@PropertySource("classpath:config/api.properties")
@ConfigurationProperties(prefix="api.aliyun.email")
@Data
public class EmailConfig {@Value("${mail.host}")private String mailHost;@Value("${mail.transport.protocol}")private String mailTransportProtocol;@Value("${mail.service.address}")private String mailServiceAddress;@Value("${mail.service.password}")private String mailServicePassword;
}

这里@ConfigurationProperties中,prefix只是指向了一部分属性前缀信息,剩余的想通过@Value来指向.

修改为
删除@ConfigurationProperties注解,并在@Value中指向配置属性

@Component
@PropertySource("classpath:config/api.properties")
@Data
public class EmailConfig {@Value("${api.aliyun.email.mail.host}")private String mailHost;@Value("${api.aliyun.email.mail.transport.protocol}")private String mailTransportProtocol;@Value("${api.aliyun.email.mail.service.address}")private String mailServiceAddress;@Value("${api.aliyun.email.mail.service.password}")private String mailServicePassword;
}

SpringBoot: Could not resolve placeholder 'XXXX' in value ${XXXX}相关推荐

  1. Springboot Could not resolve placeholder ‘spring.data.mongodb.database’ in value “${spring.data.mong

    Springboot Could not resolve placeholder 'spring.data.mongodb.database' in value "${spring.data ...

  2. 【解决方法汇总】SpringBoot项目报错 Could not resolve placeholder ‘‘ in value “${}“

    SpringBoot项目启动报错 Could not resolve placeholder 'show.tips.text' in value "${show.tips.text}&quo ...

  3. SpringBoot启动报错Could not resolve placeholder ‘XXX.XXX‘ in value

    SpringBoot启动项目时报错: Exception encountered during context initialization - cancelling refresh attempt: ...

  4. springboot启动错误 Could not resolve placeholder

    Could not resolve placeholder 'spring.datasource.druid.initialSize' in value "${spring.datasour ...

  5. springboot启动突然报错Could not resolve placeholder 'spring.datasource.driver-class-name'

    之前好好的项目,今天启动突然报错Could not resolve placeholder 'spring.datasource.driver-class-name' 按照网友的方法,添加了 @Pro ...

  6. Could not resolve placeholder ‘project.version‘ in value “${project.version}”

    使用版本: SpringBoot:2.1.18.RELEASESpringCloud: Greenwich.SR6SpringCloud Alibaba: 2.1.2.RELEASEjasypt-sp ...

  7. Could not resolve placeholder 占位符不能被解析

    Could not resolve placeholder 'active.logging.path' in value "${active.logging.path}" spri ...

  8. SpringCloud读取Nacos配置中心报错:Could not resolve placeholder ‘xxx’ in value ‘${xxx}

    hello,我是灰小猿,一个超会写bug的程序员! 近期在写一个spring cloud Alibaba读取Nacos配置中心远程配置文件的内容时,出现了几个比较坑的bug,在此记录一下,帮大家避避坑 ...

  9. 查看提供者的端口时报Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘server.port‘

    @Value("${server.port}")private String port; @Value("${server.port}") 表示获取属性文件ym ...

最新文章

  1. android 51 有序广播
  2. proshow_ARTSLINK零创|巴特莱特B-Pro Show:建筑让我自由
  3. PHP $_REQUEST获取表单提交的代码
  4. C++关系运算符重载
  5. java第一次课必修实验答案,Java第一次实验报告
  6. Scikit-learn 秘籍 第三章 使用距离向量构建模型
  7. 易邮服务器com组件注册失败,com组件注册失败有什么办法可以解决
  8. R、Rstudio的下载和安装教程
  9. pwnable.tw---start
  10. 京东全球购与花王达成区块链溯源合作
  11. 关于累加偶数奇数的c语言程序,c语言 在1-100之间,求所有奇数和偶数的个数和所有奇数和偶数的和(写到一个里面)...
  12. 【多线程】Lock和ReentrantLock使用和源码分析
  13. 教你如何做个妖精(女人必读)
  14. 远程计算机IP地址如何获取,获取远程主机的IP地址
  15. 2012年5月后QQ空间农场Farmkey最新计算字符串
  16. 奖金711万!这位“山东宝妈”破解美国运算100万年才可能解开的密码!
  17. json转excel_Excel如何制作这种专业的仪表盘?
  18. 机械臂(一)---机器臂的启动
  19. 如何利用油猴脚本(tampermonkey)免费使用知网维普?
  20. 【多式联运】基于粒子群结合遗传算法实现陆海空多式联运问题附matlab代码

热门文章

  1. 佳能Canon PIXMA MG3000 打印机驱动
  2. 编程人士的探索之路(第二弹)
  3. 信息安全数学基础(一):同余
  4. 模块预制式数据中心方案评估与建议
  5. 魔兽争霸之PHP设计模式-单件模式[Singleton]
  6. 达人评测 r5 5500u和i5 11300h选哪个好
  7. js中对象数组根据对象id分组并转map
  8. 万年历(1900年之后日历)
  9. 拓嘉辰丰电商:拼多多开店绑定的身份证能不能改?
  10. 爱情公寓原版片头AE模板