Spring / Spring Retry Interview Questions
What is the proxyTargetClass attribute in @EnableRetry?
The proxyTargetClass attribute of @EnableRetry controls whether Spring Retry uses subclass-based CGLIB proxies or interface-based JDK dynamic proxies to intercept methods annotated with @Retryable.
Default behavior:
@EnableRetry // proxyTargetClass = false by default public class AppConfig { }
When proxyTargetClass = false (default), Spring uses JDK dynamic proxies. This requires the bean to implement at least one interface. The @Retryable method must be declared on the interface for the proxy to intercept it.
When proxyTargetClass = true, Spring uses CGLIB to create a subclass proxy of the concrete class, so no interface is needed:
@EnableRetry(proxyTargetClass = true) public class AppConfig { }
When to use proxyTargetClass = true:
- Your service classes do not implement interfaces
- You want retry on methods that are not declared on any interface
- You encounter proxy-related class cast exceptions at runtime
In Spring Boot applications that already use CGLIB for @Configuration classes, setting proxyTargetClass = true is consistent and avoids mixed proxy types. The CGLIB approach has a slight startup cost for proxy generation but no runtime overhead difference.
More Related questions...