spring 组件类名相同 bean name冲突
所属分类 spring
浏览量 511
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'helloIntfImpl' for bean class [dyyx.intf.b.HelloIntfImpl] conflicts with existing, non-compatible bean definition of same name and class [dyyx.intf.a.HelloIntfImpl]
package dyyx.intf.a;
import org.springframework.stereotype.Service;
@Service
public class HelloIntfImpl implements HelloIntf{
public String hello(String name){
return "hello a,"+name+","+this.getClass();
}
}
package dyyx.intf.b;
import org.springframework.stereotype.Service;
@Service
public class HelloIntfImpl implements HelloIntf {
public String hello(String name){
return "hello b,"+name+","+this.getClass();
}
}
package dyyx;
import dyyx.intf.a.HelloIntf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloAController {
@Autowired
HelloIntf intf;
@RequestMapping("/a/hello")
public String hello(){
return intf.hello("cat");
}
}
package dyyx;
import dyyx.intf.b.HelloIntf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloBController {
@Autowired
HelloIntf intf;
@RequestMapping("/b/hello")
public String hello(){
return intf.hello("cat");
}
}
http://127.0.0.1:8088/a/hello
hello a,cat,class dyyx.intf.a.HelloIntfImpl
http://127.0.0.1:8088/b/hello
hello b,cat,class dyyx.intf.b.HelloIntfImpl
类名相同(包不同) ,spring bean name 冲突
给 a包下的 指定 bean name ,问题解决 ,其他都不用动
注入时 @Autowired 会根据具体类型找到 对应的 bean
@Autowired默认按byType自动注入
@Service("HelloIntfImplA")
public class HelloIntfImpl implements HelloIntf
https://gitee.com/dyyx/hellocode/tree/master/demo/springbootdemo/src/main/java/dyyx
上一篇
下一篇
如何进行深度学习
提问的艺术
mysql JDBC rewriteBatchedStatements
获取有序数组中第一个大于等于目标值的索引
异常值检测的两种常用方法
好习惯之预习