I have an example of the code posted on github, so here I will not include fragments of long code, please look at this: https://github.com/ralf-lackinger/spring-inheritance/tree/master
I have Parent, and Childthey both Spring beans. When I want to get Parentthrough the BeanFactoryname bean, an instance is returned Child. This causes problems for me, since I want them to have different names and only return specific ones, since the subclass overrides the method that I use from the superclass:
Parent names: parent, parentName1, parentName2
Names of child elements: child, childName1, childName2
Short code example, see my github repository for a complete code example:
context.getBean("parentName1", Parent.class).print();
returns:
Print from child.
Maybe there is a solution to use Spring qualifiers, but I'm not sure if this will work. I already tried annotation @Primarybut didn't help me solve this problem.
I use:
java version "1.8.0_66"
Spring 4.2.3.RELEASE
EDIT:
I updated the github repository with a branch named "fixed-name-conflict", where I included the changes I needed to solve my problem. See Also accepted answer (and total votes) for more information.
Thanks @oailloud for the help.
EDIT 2: The
problematic parts were the following methods:
Parent:
@Bean(name = {"parentName1", "parentName2"})
public Parent additionalBeanNames() {
return new Parent();
}
and Child:
@Override
@Bean(name = {"childName1", "childName2"})
public Child additionalBeanNames() {
return new Child();
}
Child, Parent bean .