Bean instances from Spring context must be cast into interface, not implementing class
So I have interface Thinker, and a class Volunteer implementing that interface.
I have a bean like this in my context.xml:
I have a bean like this in my context.xml:
<bean id="thinker" class="springidol.classes.Volunteer"></bean>
And in the Java code:
Thinker vol = (Volunteer)context.getBean("thinker");
I got the following exception:
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy1 cannot be cast to springidol.classes.Volunteer
It turns out that I must cast all beans instantiated from context into its interface, not its implementing class. The following Java code is correct:
Thinker vol = (Thinker)context.getBean("thinker");
0 komentar:
Posting Komentar