I am using Spring Boot 2.7, which defaults to cglib proxies. I have a JavaConfig bean definition:
interface ServiceInterface {
@Async void log()
}
@Bean
ServiceInterface myBean() {
new Impl()
}
If the Impl
class is not subclassable by cglib (in this case, it uses a static factory method and has a private constructor), context startup fails with a "cglib can't subclass" error. However, if the declared bean type is an interface, in my view the ProxyFactory ought to use a JDK proxy instead (in the first place, but certainly if cglib fails).
Comment From: YongGoose
I’ve implemented the changes in this PR. (#35344)
When a CodeGenerationException
or IllegalArgumentException
occurs, the logic now falls back to using a JDK Dynamic Proxy. I tested it with a class that has a private constructor, and the exception that used to occur no longer happens.
I also have an additional idea.
What about adding an attribute like fallbackToJdkProxy
to the @EnableAspectJAutoProxy
annotation?
If the attribute is set to true
, the logic would fall back to a JDK dynamic proxy
. If it’s false
, the current behavior (throwing an exception) would remain unchanged.
This way, users can decide whether or not to enable this feature, which seems like a good approach.
If this sounds reasonable, I’d like to work on it in a separate PR 🚀