运行环境: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. 关于在Vue中使用WebScoket的随笔

    声明:请勿直接复制粘贴抄袭文章,若有需要,请规范转载,注明出处,谢谢! ---------------------------------------------------------------- ...

  2. IP第一次实验:静态综合

  3. golang中for循环的常用用法

    package main import "fmt" func main() { //printFormula() // 打印九九乘法表 //printLeftTriangle() ...

  4. SpringDataRedis入门到深入

    一:简介 SpringDataRedis是SpringData开源项目中的一部分,它可以在Spring项目中更灵活简便的访问和操作Redis:原先在没有SpringDataRedis时往往使用Jedi ...

  5. python matplotlib通过 plt.scatter在图上画圆

    import matplotlib.pyplot as plt lena = mpimg.imread(r'C:\Users\Administrator.WIN-QV9HPTF0DHS\Desktop ...

  6. C程序:年转化天

    突然想算算自己到底活了多少天了,e,就是纯属为了好玩,毕竟咱作为一名C初学者还是要多练练的- 为了好玩,加了个密码登陆的,密码是521,还有就是不太懂时间获取... 具体闰年的判断方法: 代码如下: ...

  7. C字符串倒置-中部对称

    问题如图 Code #include<stdio.h> #include<string.h> #define MAX_LENGTH 10//最大字符串长度 void inver ...

  8. bom案例4-模拟滚动条

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. windows 崩溃不提示

    转载请注明来源:https://www.cnblogs.com/hookjc/ 碰到某些程序崩溃时弹出带红色叉叉的错误窗口或者是叫你选择调试或关闭的窗口,很碍眼.不过平时也没去理它,点掉就好. 今天客 ...

  10. @property基本概念

    1.什么是@property @property是编译器的指令 什么是编译器的指令 ? 编译器指令就是用来告诉编译器要做什么! @property会让编译器做什么呢? @property 用在声明文件 ...