开发日常报错

2025-01-17 12:01:25.261  WARN 16472 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.example.demo.DemoApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'saleController' for bean class [com.example.demo.controller.SaleController] conflicts with existing, non-compatible bean definition of same name and class [com.example.demo.config.SaleController]

 

问题原因:

  • Spring 在启动时会扫描到两个不同的类 SaleController,但这两个类都使用了默认的 Bean 名称 saleController(因为类名的首字母会被转换成小写作为 Bean 名)。
  • 这导致 Spring 容器无法决定使用哪个类作为 saleController 的 Bean,从而抛出了 ConflictingBeanDefinitionException 异常。

解决方案:

  1. 重命名其中一个 SaleController: 如果这两个类的作用不同,应该为它们分别定义不同的 Bean 名称。你可以在其中一个 SaleController 类上显式指定一个不同的 Bean 名称来避免冲突。

  2. 去掉不必要的 @Component 注解: 如果其中一个类是不需要作为 Spring Bean 被管理的,你可以去掉不需要的 @Component 注解,或者将其替换为其他注解,来避免被 Spring 扫描到。

  3. 使用不同的包名或不同的类名: 如果这两个类的功能非常不同,考虑将它们放到不同的包中,或者重命名其中之一,避免冲突。

步骤:

  1. 打开 com.example.demo.controller.SaleControllercom.example.demo.config.SaleController 这两个类。
  2. 根据需要修改其中一个类的 @Component 注解,或者修改其名称,确保它们不会使用相同的 Bean 名称。
  3. 重新启动你的应用。

通过这种方式,你应该能够解决 Bean 名称冲突的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值