解决 org.springframework.context.annotation.ConflictingBeanDefinitionException

Java web 项目启动时报错

报错信息:

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'testService' for bean class [test.service.impl.TestServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [test.service.impl.TestService]

通过搜索定位到是以下代码片段造成的报错。

代码片段:

代码1.ITestService 的实现类代码:
@Service("testService")
public class TestServiceImpl implements ITestService {
......
}

代码2.在Controller中使用代码:
@Autowired
private ITestService testService;

 代码 1 和 代码2 中的 testService 冲突了,项目启动时编译器检测到有两个相同名称的bean就会报错。

解决方法:

修改代码1和代码2中任意一个的名字即可解决

例如:修改代码1中的名字

1.ITestService 的实现类代码:
@Service("testServiceImpl")
public class TestServiceImpl implements ITestService {
......
}
原文地址:https://www.cnblogs.com/etangyushan/p/13753942.html