运行环境: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. 深入理解Java虚拟机之图解Java内存区域与内存溢出异常

    Java内存区域与内存溢出异常 运行时数据区域 程序计数器 用于记录从内存执行的下一条指令的地址,线程私有的一小块内存,也是唯一不会报出OOM异常的区域 Java虚拟机栈 Java虚拟机栈(Java ...

  2. 开源数据可视化BI工具SuperSet(使用)

    上一篇介绍了Linux 下如何安装SuperSet ,本篇简单介绍一下如何使用 1.输入安装时设置的用户名密码登录控制台  2.控制界面如下  3.第一步添加数据源(已安装好的mysql) 点击 da ...

  3. C#图片转成流

    Bitmap b = new Bitmap(Server.MapPath(ppath)); Stream ms = new MemoryStream(); b.Save(ms, System.Draw ...

  4. win+ r 命令

    Win 键+R calc:计算器 notepad:记事本 mspaint:画图 cmd:控制台 control:控制面板 desk.cpl:打开控制面板中的桌面设置 main.cpl:鼠标设置 ine ...

  5. 单片机main函数退出后发生什么——以stm32为例

    STM32:main函数退出后发生什么? 我们都在说单片机要运行在无限循环里,不能退出,可退出之后会发生什么? 讨论STM32启动过程的文章数不胜数,可main函数结束之后会发生什么却少有讨论. 几日 ...

  6. java之类的抽取与对象的创建

    Java语言之类的抽取 前言:世界由什么组成?This is a question.有人说是原子.分子,有人说是山川草木. 诚然,一千个人眼中有一千个哈姆雷特.而在程序员眼中,万物皆对象. 定义: 在 ...

  7. 【记录一个问题】用ndk的gcc命令行无法编译C++11的lambda等语法的代码

    /Users/ahfu/code/android/android-ndk-r14b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_6 ...

  8. Markdown anywhere

    最近经常写文章,发现Markdown是一个非常方便的网页排版规范,详见:http://cesiumcn.org/markdown.html | http://cesium.coinidea.com/m ...

  9. 服务器+nextcloud搭建自己的私有云盘

    简介 Nextcloud是一款开源免费的私有云存储网盘项目,可以让你快速便捷地搭建一套属于自己或团队的云同步网盘,从而实现跨平台跨设备文件同步.共享.版本控制.团队协作等功能.它的客户端覆盖了Wind ...

  10. js文件中三斜杠注释///reference path的用途

    编辑某个js文件时,要想这个js文件出现其他js成员的ide提示,可以在js文件开头使用3个斜杠注释和reference指令的path指向此js文件路径,这样在编写这个js文件时,ide就会自动出现p ...