@ConfigurationProperties(prefix=““)不起作用问题及解决方案
作者:qq_40420014
这段文章主要讨论了在Spring Boot项目中使用@ConfigurationProperties进行YAML配置自动导入时遇到的问题,并分享了三种常见的错误解决方法,帮助开发者避免相同错误
@ConfigurationProperties(prefix=““)不起作用
今天项目用到将yaml配置参数自动导入功能,结果不起作用,百度查了半天,借鉴了几种方法(虽然都没解决我的问题)和大家一起分享。
第一种
写了ConfigurationProperties但是没写Component,Component是
@Component @ConfigurationProperties(prefix = "")
第二种
实体类写成了静态,例如下面的下面的,就不生效
@Component
@ConfigurationProperties(prefix = "userinfo")
public class User
{
private static String name;
public static String getName()
{
return name;
}
public static void setName(String name)
{
this.name = name;
}
}第三种
也就是我的了,估计不会有人犯这种低级错误
//使用实体类的时候没有使用注解自动注入 User user = new User(); name = user.getName();//这样当然NullPointExecption
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- @ConfigurationProperties与EnableConfigurationProperties使用及说明
- @ConfigurationProperties注解原理与实践方式
- @ConfigurationProperties用法及说明
- 深入解析Spring Boot中的@ConfigurationProperties注解
- @ConfigurationProperties及@NestedConfigurationProperty的使用解读
- 将SpringBoot属性配置类@ConfigurationProperties注册为Bean的操作方法
- SpringBoot @ConfigurationProperties + Validation实现启动期校验解决方案
