运行环境:Spring Boot 2.5.0, IDEA 2020.3.2

异常详细信息:

Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.demo.config.MyConfigProperties' 
available: expected single matching bean but found 2: myConfigProperties,test-com.example.demo.config.MyConfigProperties

一开始是这样写的

@Configuration
@PropertySource("classpath:myConfig.properties")
@ConfigurationProperties(prefix = "test")
@Component
@Data
public class MyConfigProperties {
private String name;
private int age;
}
@SpringBootApplication
@EnableConfigurationProperties(MyConfigProperties.class)
public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args);
} }
@RestController
public class IndexController {
@Resource
private MyConfigProperties myConfig; @RequestMapping("/config")
String customConfig() {
String name = myConfig.getName();
return name + myConfig.getAge();
}
}
 
然后,运行的时候就报了上面那个错。
 
解决方案是删除入口类上的 
@EnableConfigurationProperties(MyConfigProperties.class)
 
 
 
 

Spring Boot 自定义配置文件异常"expected single matching bean but found 2"的更多相关文章

  1. 深入Spring Boot:怎样排查expected single matching bean but found 2的异常

    写在前面 这个demo来说明怎么排查一个常见的spring expected single matching bean but found 2的异常. https://github.com/hengy ...

  2. spring依赖注入单元测试:expected single matching bean but found 2

    异常信息:org.springframework.beans.factory.UnsatisfiedDependencyException: Caused by: org.springframewor ...

  3. spring " expected single matching bean but found 2" 问题一例。

    初入java,使用spring时遇到一个问题,左边是一个接口和实现.右边是service和实现. @Service@Transactional(rollbackFor = Exception.clas ...

  4. Spring 3.2 @Autowired异常:expected single matching bean but found 2

    在使用Sping做单元测试时候,对RequestMappingHandlerAdapter(从处理器包装过来的适配器)进行自动装配, 发现报:expected single matching bean ...

  5. 多个@bean无法通过@resource注入对应的bean(org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found )

    一.异常 org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ' ...

  6. expected single matching bean but found 2

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'acc ...

  7. 多数据源报错 expected single matching bean but found 2: xxx,xxx

    问题: expected single matching bean but found 2: xxx,xxx 原因:在 Spring 容器中配置了两个类型Bean,Spring 容器将无法确定到底要用 ...

  8. MyBatis Plus:No qualifying bean of type 'com.baomidou.mybatisplus.mapper.BaseMapper<?>' available: expected single matching bean but found 4

    场景: 应用MyBatis Plus 和通用Mapper 继承自ServiceImpl实现对Service里的方法进行包装再处理. public interface IServiceBase2< ...

  9. spring boot自定义配置文件

    把一些可能会经常变动的东西写在配置文件中,可以增加程序的灵活性,避免多次改版发版. 在sping boot中除了自带的默认配置文件application.properties之外,我们还可以在reso ...

随机推荐

  1. css上下居中

    position: absolute; top: 20%; left: 50%; transform: translateX(-50%); -ms-transform: translateX(-50% ...

  2. Go - 如何编写 ProtoBuf 插件 (三) ?

    目录 前言 演示代码 小结 推荐阅读 前言 上篇文章<Go - 如何编写 ProtoBuf 插件 (二) >,分享了基于 自定义选项 定义了 interceptor 插件,然后在 hell ...

  3. 《剑指offer》面试题41. 数据流中的中位数

    问题描述 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值. 例 ...

  4. 5.12-jsp分页功能学习

    1.分页功能相关资料查询 分页须知知识点: (1)JDBC2.0的可滚动结果集. (2)HTTP GET请求. 一.可滚动结果集   Connection con  = DriverManager.g ...

  5. MVC框架---转

    浅析MVC模式 摘要:MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面 ...

  6. insert语句

    7.4.插入数据insert(DML语句) 语法格式: insert into 表名(字段名1,字段名2,字段名3...) values(值1,值2,值3): 注意:字段名和值要一一对应.什么是一一对 ...

  7. ES6新增的数组的方法

    forEach forEach()会遍历数组, 循环体内没有返回值,forEach()循环不会改变原来数组的内容, forEach()有三个参数, 第一个参数是当前元素, 第二个参数是当前元素的索引, ...

  8. 云原生新时代弄潮儿k8s凭什么在容器化方面独树一帜?

    云原生新时代弄潮儿k8s凭什么在容器化方面独树一帜? Kubernetes 可以为做些什么? 在学习一种新技能之前,囧囧建议不要上去先看各种牛叉的实现,我们需要先搞清楚这个技能是什么?学习了之后能为我 ...

  9. linux区分atime,ctime and mtime

  10. ApacheCN 所有教程/文档集已备份到 Coding

    QuantLearning 数据科学 WIKI 安全 WIKI 团队 WIKI Sklearn 中文文档 PyTorch 中文文档 TutorialsPoint 中文系列教程 Java8 中文官方教程 ...