SpringBoot启动项目时报错:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'msgReceiver':
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException:
Could not resolve placeholder 'file.post-url' in value "${file.post-url}"

还是一样,遇到SpringBoot启动报错,从最后看起,很明显这里是在msgReceiver中因为找不到file.post-url的定义。

查看MsgReceiver的代码:

    @Value("${file.post-url}")private String postUrl; 

问题就出在这里,这里将配置文件里的file.post-url的值赋给了postUrl,但是SpringBoot启动时,没有在配置文件application.yml(application.properties)中找到关于file:post-url:(file.post-url=)的定义。

可以去查看一下,有没有这个定义,没有加上即可。

笔者遇到的问题是,配置文件中有相关定义,但是启动依然报错。这是因为生成的Class文件中配置没有及时被更新,可以右键项目,找到Maven—>Reload Project、右键项目->Reload From Disk以及Build标签下的Build Project来刷新项目代码和重新build来解决。这三个操作是解决代码和生成的class文件不匹配的三板斧,遇到此类问题都可以这样操作。

源码解读:

我们可以再看看Value这个注解在源码中是怎么定义的:

/*** Annotation at the field or method/constructor parameter level* that indicates a default value expression for the affected argument.** <p>Typically used for expression-driven dependency injection. Also supported* for dynamic resolution of handler method parameters, e.g. in Spring MVC.** <p>A common use case is to assign default field values using* "#{systemProperties.myProp}" style expressions.** <p>Note that actual processing of the {@code @Value} annotation is performed* by a {@link org.springframework.beans.factory.config.BeanPostProcessor* BeanPostProcessor} which in turn means that you <em>cannot</em> use* {@code @Value} within* {@link org.springframework.beans.factory.config.BeanPostProcessor* BeanPostProcessor} or* {@link org.springframework.beans.factory.config.BeanFactoryPostProcessor BeanFactoryPostProcessor}* types. Please consult the javadoc for the {@link AutowiredAnnotationBeanPostProcessor}* class (which, by default, checks for the presence of this annotation).** @author Juergen Hoeller* @since 3.0* @see AutowiredAnnotationBeanPostProcessor* @see Autowired* @see org.springframework.beans.factory.config.BeanExpressionResolver* @see org.springframework.beans.factory.support.AutowireCandidateResolver#getSuggestedValue*/
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {/*** The actual value expression: e.g. "#{systemProperties.myProp}".*/String value();}

发现它是一个可以应用在Filed(成员变量)、Method(方法)、Parameter(参数)和Annotation_Type(注解类型)的运行时注解。用来指定被注解的Argument(就是前面提到的四种类型)的默认值。

关于RetentionPolicy有疑问的同学可以继续点进Retention源码:

/*** Indicates how long annotations with the annotated type are to* be retained.  If no Retention annotation is present on* an annotation type declaration, the retention policy defaults to* {@code RetentionPolicy.CLASS}.** <p>A Retention meta-annotation has effect only if the* meta-annotated type is used directly for annotation.  It has no* effect if the meta-annotated type is used as a member type in* another annotation type.** @author  Joshua Bloch* @since 1.5* @jls 9.6.3.2 @Retention*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {/*** Returns the retention policy.* @return the retention policy*/RetentionPolicy value();
}

它是定义注解类型会被保持多久。默认值是CLASS。另外只有在元注解类型被注解直接使用时,Retention注解才生效。如果元注解类型被用作另一个注解类型的成员变量,就不起作用。

元注解可以理解为注解的注解。注解的成员变量,就可以把注解看做是一个特殊的类,类就会有成员变量,比如这里的value就是Retention的一个成员变量。

那么怎么写才是注解的成员变量,怎么写才是注解直接使用呢?

成员变量就跟类的定义一样,在注解里写一个成员变量。而直接使用,就需要把这个注解写成一个单独的类,用@Interface修饰。也就是说它是一个注解的注解,也就是为什么说Retention是一个元注解了。

那么怎么这两种方式怎么使用的呢?这里举个例子:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

像这样,就是成员变量的使用方式,使用key=value的方式。

而元注解就像这样使用:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention 

这里Retention就是个元注解,他是注解在注解上的,是注解的注解。

再看RetentionPolicy是怎么定义的:

/*** Annotation retention policy.  The constants of this enumerated type* describe the various policies for retaining annotations.  They are used* in conjunction with the {@link Retention} meta-annotation type to specify* how long annotations are to be retained.** @author  Joshua Bloch* @since 1.5*/
public enum RetentionPolicy {/*** Annotations are to be discarded by the compiler.*/SOURCE,/*** Annotations are to be recorded in the class file by the compiler* but need not be retained by the VM at run time.  This is the default* behavior.*/CLASS,/*** Annotations are to be recorded in the class file by the compiler and* retained by the VM at run time, so they may be read reflectively.** @see java.lang.reflect.AnnotatedElement*/RUNTIME
}

它有三个枚举值,SOURCE、CLASS和RUNTIME。SOURCE是注解将会被编译器丢弃,CLASS是注解会 被编译器记录在Class文件中,RUNTIME值注解不仅会被记录在Class文件,还将在运行时被虚拟机保持,这样他们就可以被反射地读到。

这里就是解释了注解的生命周期,从源代码—>被编译器编译进Class文件—>运行时被虚拟机保持。

那么什么时候用哪个呢?

一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解,比如@Deprecated使用RUNTIME注解;

如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解;

如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,使用SOURCE 注解。

如果您觉得文章写得还不错,不妨点个赞。如果有什么疑惑或者不对的地方,可以留下评论,看到我会及时回复的。所有关注都会回关!

SpringBoot启动报错Could not resolve placeholder ‘XXX.XXX‘ in value相关推荐

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

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

  2. SpringBoot启动报错:Failed to introspect Class [XXX] from ClassLoader解决办法

    Failed to introspect Class [XXX] from ClassLoader 今天做项目引入一个maven之后项目启动报错,很是无奈.碰到这种引入jar包导致的启动问题很可能是就 ...

  3. springclould项目启动报错Could not resolve placeholder

    springclould+consul项目 报错 原因: 1.pom.xml文件里 application.properties和bootstrap.properties 数据源配置多个 2.pom. ...

  4. SpringBoot启动报错:Failed to introspect Class [XXX] from ClassLoader

    Failed to introspect Class [XXX] from ClassLoader sprintboot依赖之间没有构建好,按报错日志找到A项目依赖的包B,重新编译构建依赖包B的项目. ...

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

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

  6. springboot启动报错:Unregistering JMX-exposed beans on shutdown

    环境 1,maven 3.5.0 2,eclipse 4.7.1 3,spring boot 1.5.9.RELEASE springboot启动报错 按照spring 用户手册创建新的spring ...

  7. spring读取多个配置properties报错“Could not resolve placeholder“的解决方案

    spring读取多个配置properties报错"Could not resolve placeholder"的解决方案 参考文章: (1)spring读取多个配置properti ...

  8. SpringBoot启动报错:Parameter 0 of method hmset in com.qcby.rbac.util.RedisUtils required a bean of type

    SpringBoot启动报错,报错信息如下: 报错是由于A类中定义了含参数的构造函数,Spring自动构造和注入时未为该Bean传入参数,引起报错. 查了很多资料,最后发现,我是因为注释的时候没有把@ ...

  9. 启动Spring项目报错,Springboot启动报错 Disconnected from the target VM 解决过程

    启动Spring项目报错,Springboot启动报错 Disconnected from the target VM 解决过程 大概率是缺少了一下依赖 <dependency><g ...

最新文章

  1. 云上人替代方案训练代码
  2. oracle触发器比较,Oracle使用触发器和mysql中使用触发器的比较
  3. winform中listView
  4. semi-global matching 算法总结
  5. python爬虫之requests模块2
  6. 齐全且实用的MySQL函数使用大全
  7. 利用EViews做截面数据的线性回归分析
  8. xshell通过隧道连接_如何通过SSH隧道实现远程连接
  9. Vue3中Compositions API的使用(二)
  10. Vue.js 组件 - 组件间的循环引用
  11. EL表达式和JSTL标签库学习笔记
  12. 细数APDL中的流程控制命令
  13. HTML网页调用 网易云 音乐播放器代码
  14. 如何无损合并video.m4s与audio.m4s为mp4文件
  15. settings.xml详解
  16. 更改大商创立即购买【仿淘宝】
  17. Python简单网页抽奖
  18. Ripro子主题-站壳jizhi-chlid极致主题QIW源码市场
  19. 第九届山东理工大学ACM网络编程擂台赛 正式赛 sdut4074博弈 - ldq的吃瓜比赛
  20. kernal源码下载地址

热门文章

  1. python模型训练 warm_start_随机森林入门攻略(内含R、Python代码)
  2. 我的世界刷铁机java版_《我的世界》刷铁机存档
  3. phpgif图片包_php实现处理动态GIF图片和GIF动画的
  4. UI设计这个专业现在如何,未来就业前景都有哪些不错的选择
  5. 1003. 我要通过!
  6. SSD固态硬盘 4K对齐
  7. 如何构建大规模数据中心网络?智邦科技高密交换机给你答案
  8. 怎么理解cpu load?
  9. 面试题之利用call或者apply实现bind功能
  10. 免费的webservice接口 天气预报/IP查询/股票查询/手机归属地等