I\'m having a problem with some Spring bean definitions. I have a couple of context xml files that are being loaded by my main() method, and both of them contain almost exclusiv
Explanation internal working on this error
You are getting this error because after instantiation the container is trying to assign same object to both classes as class name is same irrespective of different packages......thats why error says non compatible bean definition of same name ..
Actually how it works internally is--->>>>.
pkg test1; …. @RestController class Test{}
pkg test2; …. @RestController class Test{}
First container will get class Test and @RestController indicates it to instantiate as…test = new Test(); and it won’t instantiate twice After instantiating container will provide a reference variable test(same as class name) to both the classes and while it provide test reference To second class it gets non compatible bean definition of same name ……
Solution—>>>>
Assign a refrence name to both rest controller so that container won’t instantiate with default name and instantiate saperately for both classes irrespective Of same name
For example——>>>
pkg test1; …. @RestController(“test1”) class Test{}
pkg test2; …. @RestController(“test2”) class Test{}
Note:The same will work with @Controller,@Service,@Repository etc..
Note: if you are creating reference variable at class level then you can also annotate it with @Qualifier("specific refrence name") for example @Autowired @Qualifier("test1") Test test;