-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ConfigurationClassEnhancer
should explicitly set custom ClassLoader
on CGLIB Enhancer
(aligned with CglibAopProxy
)
#34274
Comments
This comment has been minimized.
This comment has been minimized.
@AlexanderShchelkunov Thanks for the report. Please move all that text into a sample we can actually run ourselves. You can attach a zip to this issue or push the code to a GitHub repository. |
Hi @snicoll, |
ClassCastException
: XXX$$SpringCGLIB$$0
cannot be cast to org.springframework.cglib.proxy.Factory
with custom ClassLoader
ClassCastException
: XXX$$SpringCGLIB$$0
cannot be cast to org.springframework.cglib.proxy.Factory
with custom ClassLoader
ConfigurationClassEnhancer
should explicitly set custom ClassLoader
on CGLIB Enhancer
(aligned with CglibAopProxy
)
Reopening due to a side effect with package-visible superclasses not being accessible. |
demo.zip
Description
Hello!
I have a Spring Boot application, that supports 3rd party plugins. Because of that I need to use a custom class loader. But if there is a
@Configuration
that has@Lazy
annotation (when Spring has to create a proxy), the app will not start. See the possible way to fix at the bottom.Environment
Spring Boot: 3.3.3
Spring Framework: 6.1.12
Java: JDK 17
Steps to Reproduce
Start the app
Error Message
Minimal sample application
build.gradle
DemoApplication.java
SomeConfig.java
SomeService.java
Possible way to fix
This happens because Enhancer, that creates Configuration does not respect provided custom class loader.
In
ConfigurationClassEnhancer
in method newEnhancer it does not call enhancer.setClassLoader(classLoader)In the same time CglibAopProxy respects provided custom class loader, it sets classloader in buildProxy method
There is also cache in AbstractClassGenerator, where key is ClassLoader:
private static volatile Map<ClassLoader, ClassLoaderData> CACHE = new WeakHashMap<>();
When Spring loads configuration class the first time, it does not use custom classloader and key is the default classloader (method create in AbstractClassGenerator).
When Spring creates cglib proxy for the configuration class, it calles
create
again, but this time custom class loader set to classLoader field, so the key in the CACHE is different. And this causes the issue.This probably could be fixed if enhanchers for the same class will always use the same class loader.
For example you can set classloader in ConfigurationClassEnhancer -> newEnhancer method. I tried to do it in debug and it seems to work.
The text was updated successfully, but these errors were encountered: