I'm trying to understand Spring-AOP and am still confused by the AOP-proxies. Especially when it comes to classes that implement interfaces.
From the SpringFramework 2.x manual:
"If the target class implements one (or more) interfaces, then the type of proxy that is created depends on the configuration of the ProxyFactoryBean.
If the proxyTargetClass property of the ProxyFactoryBean has been set to true, then a CGLIB-based proxy will be created. "
When the proxyTargetClass is false, I presume a JDK proxy gets created. When I try to fetch it from the context via
Person p = (Person) ctx.getBean("diner");
I get the infamous error
java.lang.ClassCastException: $Proxy0 cannot be cast to ...
I've seen that the solution is to set the proxyTargetClass to true, causing the CGLIB proxy to be used, but I do not understand
1) How would one retrieve the JDK-proxy bean without the ClassCastException?
2) Under what circumstances would using the JDK-proxy be favored over the CGLIB-proxy?