Spring中报错:required a single bean, but 2 were found:

本文详细介绍了在使用Spring框架时,如何解决@Autowired注解导致的注入冲突问题,通过@Qualifier注解成功定位并注入目标Bean,确保了应用程序的正常启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Service("ae01SchoolServiceImpl")
public class Ae01SchoolServiceImpl  implements IfAe01SchoolService {
}

public interface IfAe01SchoolService {
    Page<Ae01PO> 
}

public class Ae01SchoolController {

    @Autowired
    private IfAe01SchoolService schoolService;
}

执行后报错:

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Field template in com.jzd1997.studyredis.TemplateController required a single bean, but 2 were found:
	- redisTemplate: defined by method 'redisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration$RedisConfiguration.class]
	- stringRedisTemplate: defined by method 'stringRedisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration$RedisConfiguration.class]
 
 
Action:
 
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。

解决方案在错误提示里面也提到了,我采用@Qualifier解决。
 

@RestController
@RequestMapping("school")
public class Ae01SchoolController {

    @Autowired
    @Qualifier("ae01SchoolServiceImpl")
    private IfAe01SchoolService schoolService;
}

问题解决了,但是我想还会有其他方式的,待后续补充

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值