Working with Spring-AOP there are moments when you want to access the class type of the incoming method call. In my example I’ve implemented a MethodInterceptor to capture method invocations on a class of type net.solidsyntax.MyProxiedClass.
Surprisingly the output does not show something like:
Instead we get a:
As it turns out the object on which we are working is already wrapped in a proxy by another aspect. In this case one to handle transactions.
So how do we access the concrete class from a spring proxy ?
Our first option is to use the Spring AopUtils.
However this solution still gives us:
The reason for this is rather simple. Not only is our object already wrapped by a transaction proxy, there is also a proxy for exception handling. We could iterate until we get a concrete class but we don’t have to. Spring already contains AopProxyUtils which does the work for us.
This changes our output to:
AopProxyUtils can handle class-based proxies generated with CGLIB as well as interface proxies.