Skip to content

Obtain real Class object for Spring bean

I am using Spring to inject beans. And I am using some annotations to annotate bean methods (Security, TransactionManagement, ExceptionHanling, Logging). The problem is:

I want to create JUnit test to check if I forgot annotate some methods. But Spring returns $ProxyXXX class without any annotations on methods..

Method[] methods = logic.getClass().getMethods();

  for (Method method : methods) {
     Annotation[] annotations = method.getAnnotations();   // empty array!

How can I get annotations for method or obtain a real class object?

P.S. Spring 2.5.6, JDKDynamicProxy (not CGLib)

Answer

Spring’s interface-based proxies implement TargetClassAware.