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 { ...... }