Skip to content
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

ClassCastException: XXX$$SpringCGLIB$$0 cannot be cast to org.springframework.cglib.proxy.Factory with custom ClassLoader #34274

Open
AlexanderShchelkunov opened this issue Jan 16, 2025 · 3 comments
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@AlexanderShchelkunov
Copy link

AlexanderShchelkunov commented Jan 16, 2025

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

Caused by: java.lang.ClassCastException: class com.example.demo.SomeConfig$$SpringCGLIB$$0 cannot be cast to class org.springframework.cglib.proxy.Factory (com.example.demo.SomeConfig$$SpringCGLIB$$0 and org.springframework.cglib.proxy.Factory are in unnamed module of loader org.springframework.boot.loader.launch.LaunchedClassLoader @433c675d)
        at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:91) ~[spring-aop-6.1.12.jar!/:6.1.12]
        at org.springframework.aop.framework.CglibAopProxy.buildProxy(CglibAopProxy.java:221) ~[spring-aop-6.1.12.jar!/:6.1.12]
        ... 30 common frames omitted

Minimal sample application

build.gradle

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.3.3'
	id 'io.spring.dependency-management' version '1.1.7'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(17)
	}
}
configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}
repositories {
	mavenCentral()
}
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-aop'
	implementation 'org.springframework:spring-core'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'
}
tasks.named('test') {
	useJUnitPlatform()
}

DemoApplication.java

@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    URLClassLoader classLoader = new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader());
    SpringApplication app = new SpringApplication(new DefaultResourceLoader(classLoader), DemoApplication.class);
    app.run(args);
  }
}

SomeConfig.java

@Configuration
public class SomeConfig {
}

SomeService.java

@Service
public class SomeService {

  private final SomeConfig config;

  @Lazy
  public SomeService(SomeConfig config) {
    this.config = config;
  }

}

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

if (classLoader != null) {
  enhancer.setClassLoader(classLoader);
...

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 16, 2025
@Vovch0

This comment has been minimized.

@snicoll
Copy link
Member

snicoll commented Jan 20, 2025

@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.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Jan 20, 2025
@AlexanderShchelkunov
Copy link
Author

AlexanderShchelkunov commented Jan 20, 2025

@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,
Please see the attached file.

demo.zip

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jan 20, 2025
@sbrannen sbrannen changed the title java.lang.ClassCastException: class XXX$$SpringCGLIB$$0 cannot be cast to class org.springframework.cglib.proxy.Factory on app startup with custom classloader ClassCastException: XXX$$SpringCGLIB$$0 cannot be cast to org.springframework.cglib.proxy.Factory with custom ClassLoader Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

4 participants