Sub topic 17
Inspecting and debugging proxies
This chapter assumes you can write an @Aspect and know the difference between JDK and CGLIB proxies from earlier chapters. It shows how to confirm that a bean is proxied, see which advice is attached, and find out why advice did not run.
Recognizing a proxy
A proxied bean is not an instance of your class. Its runtime class name gives it away:
- A JDK proxy has a name like
jdk.proxy2.$Proxy45(com.sun.proxy.$Proxy45on older JDKs). - A CGLIB proxy has a name like
ReportService$$SpringCGLIB$$0. Spring 5 and earlier used$$EnhancerBySpringCGLIB$$.
If getClass().getName() prints your plain class name, no proxy wraps that bean, so no advice will run for it.
Asking Spring about a proxy
AopUtils and AopProxyUtils answer the same question in code. Every proxy Spring creates also implements the Advised interface, which exposes its advisors.
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.support.AopUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
class ProxyInspector implements CommandLineRunner {
private final GreetingService service;
ProxyInspector(GreetingService service) {
this.service = service;
}
@Override
public void run(String... args) {
System.out.println("class = " + service.getClass().getName());
System.out.println("aop proxy = " + AopUtils.isAopProxy(service));
System.out.println("jdk proxy = " + AopUtils.isJdkDynamicProxy(service));
System.out.println("cglib = " + AopUtils.isCglibProxy(service));
System.out.println("target = " + AopProxyUtils.ultimateTargetClass(service));
if (service instanceof Advised advised) {
for (Advisor advisor : advised.getAdvisors()) {
System.out.println("advisor = " + advisor.getAdvice().getClass().getSimpleName());
}
}
}
}If service is not a proxy, the instanceof Advised check fails and nothing is listed. Spring Boot sets spring.aop.proxy-target-class=true by default, so expect CGLIB even for beans that implement interfaces.
Turning on logging
Set this property to see which beans Spring considers for proxying:
logging.level.org.springframework.aop=DEBUGBeans that Spring declines to proxy often produce an info-level message such as "is not eligible for getting processed by all BeanPostProcessors". That message points to a bean created too early, which can leave it unproxied.
A diagnostic checklist
When advice does not run, work through these checks in order:
Common errors
- ClassCastException or injection failure. Injecting a JDK-proxied bean by its concrete class fails, because the proxy implements only the interfaces. Inject the interface, or force CGLIB with
proxyTargetClass=true. - Comparing or casting the injected object to its class. Use
AopProxyUtils.ultimateTargetClassinstead ofgetClass(). - Null fields inside a CGLIB proxy. Calling a
finalmethod on the proxy runs it on the proxy instance, whose fields were never initialized.
Exercise
A bean's getClass().getName() prints OrderService, not a name containing $$. Your @Around advice never runs. What does that tell you, and what do you check first?
Show answer
The bean is not proxied, so the problem is not self-invocation. Check that the aspect is a registered bean (@Component plus @EnableAspectJAutoProxy or the AOP starter), that the pointcut matches this bean's type and method, and that the bean was not created by new or too early in the container's startup.