In my spring controllers, annotated with aspects, I am attempting to remove CGLib proxies and replace them with JDK dynamic proxies. I know that Spring AOP uses CGLib when the class does not implement an interface, since JDK dynamic proxies work only on interfaces. I also realize that annotations need to be present on both the interface and the implementing class. However, the problem I am running into is that the controller no longer shows up as a bean with a JDK proxy.
My controller bean is scanned for like such:
<context:annotation-config/>
<context:component-scan base-package="com.package.name"/>
This works, but controller shows up as CGLibController$$EnhancerByCGLIB$$5f0b2287:
package com.package.name;
@Controller
public class CGLibController {
@AOP_Aspect
@RequestMapping("some_url")
public void foo();
}
//in a bean post processor
//in postProcessAfterInitialization(Object bean, String beanName)
Controller controller = AnnotationUtils
.findAnnotation(bean.getClass(), Controller.class);
//controller will exist
//bean name is CGLibController$$EnhancerByCGLIB$$5f0b2287
This doesn't work, it never gets to the bean post processor:
package com.package.name;
@Controller
public interface ITest{
@AOP_Aspect
@RequestMapping("some_url")
public void foo();
}
package com.package.name;
@Controller
public class DynamicController implements ITest{
@AOP_Aspect
@RequestMapping("some_url")
public void foo();
}
However, if I explicitly create this DynamicController bean as in
<bean class="com.package.name.DynamicController"/>
then when I start up my server the ServletContext complains that
Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'dynamicController' to URL path [some_url]: There is already handler of type [class $Proxy61] mapped.
So something is happening here, DynamicController is a dynamic proxy. But I don't know what else is happening and I know it's not a bean/controller any longer. I don't want "controller, aspect, dynamic proxy: pick any two" I want all three. Is this possible somehow?
com.name.package
but your controller is incom.package.name
..is it just a typo? – Biju Kunjummen<context:component-scan base-package="com.package.name"/>
implies<context:annotation-config/>
so you have some unnecessary duplication. – Dave Syer