I have a question regarding Spring inheritance:
Let’s say I have a class (bean) BraveKnight that implements the interface Knight. After I declare this bean in the xml file with the id->knight, I’m trying to get a reference of the bean:
Knight knight = (Knight) context.getBean("knight");
This is the way it works. But why isn’t possible to get the bean reference from the bean’s name itself: BraveKnight (and why doesn’t work with no interface at all)?
BraveKnight knight = (BraveKnight) context.getBean("knight");
First of all please use:
instead of:
You will avoid this nasty class cast.
Secondly your question about why it is not working with no interface is a little bit unobvious. I guess you have some kind of problem getting BraveKnight when it is not implementing any interface at all. If you want do that you have to force Spring to use CGLIB instead of dynamic proxy mechanism. If you have a problem with ClassCastException though when casting your bean to BraveKnight it means that Spring used dynamic proxy and implemented a $Proxy0 class which is only implementing Knigh interface and does not extend BraveKnigh directly. Whatever your problem is please try to get to know CGLIB and JDK dynamic proxy mechanism better so that you will not be surprised next time what Spring will do in the future with your objects.
If that answer does not correspond to your problem please try to attach some more info, stack trace or explanation.