Description
Affects: Spring 6.0.11 / Spring Boot 3.1.2
For reproducing the issue, I highly recommend starting with the attached reproducer linked below.
Assume an application with the following components:
- Spring Boot 3.1.2
- Spring JPA
- Spring Security
- Spring Web
- Spring test
- Spring Security test
- H2 as a database (for tests, I don't think the DB affects this issue)
Let this application have a controller with an endpoint accepting a MultipartFile
RequestParam
that calls an @Transactional
method.
Add an entity with a String
field (@Id
) and a CrudRepository
with a getBy<The string field>
.
Add an ApplicationListener<ApplicationReadyEvent>
which calls the above method.
Add a @SpringBootTest
with two @Test
methods, one of them annotated with @WithMockUser
. These test methods can be empty.
Run the tests using native-image
with mvn -PnativeTest test
. An exception like the following should occur:
java.lang.IllegalStateException: Failed to load ApplicationContext for [AotMergedContextConfiguration@5207f8d0 testClass = io.github.danthe1st.spring_test.SpringTestApplicationTests, contextInitializerClass = io.github.danthe1st.spring_test.SpringTestApplicationTests__TestContext001_ApplicationContextInitializer, original = [WebMergedContextConfiguration@6b119ba0 testClass = io.github.danthe1st.spring_test.SpringTestApplicationTests, locations = [], classes = [io.github.danthe1st.spring_test.SpringTestApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@64e953ce, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@29866a5b, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@329bad59, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@24c2dbca, org.springframework.boot.test.context.SpringBootTestAnnotation@6c69f2e0], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]]
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:143)
org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:127)
org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:191)
org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:130)
org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:241)
[...]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someController': Unexpected AOP exception
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:605)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
[...]
Caused by: org.springframework.aop.framework.AopConfigException: Unexpected AOP exception
org.springframework.aop.framework.CglibAopProxy.buildProxy(CglibAopProxy.java:228)
org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:155)
org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.buildProxy(AbstractAutoProxyCreator.java:517)
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:464)
[...]
Caused by: java.lang.UnsupportedOperationException: CGLIB runtime enhancement not supported on native image. Make sure to include a pre-generated class on the classpath instead: io.github.danthe1st.spring_test.SomeController$$SpringCGLIB$$1
org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363)
org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:575)
org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.lambda$new$1(AbstractClassGenerator.java:107)
org.springframework.cglib.core.internal.LoadingCache.lambda$createEntry$1(LoadingCache.java:52)
java.base@17.0.5/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[...]
Reproducer: https://github.com/danthe1st/spring-cglib-repro/actions/runs/5648996009
Build log with error: https://github.com/danthe1st/spring-cglib-repro/actions/runs/5648996009/job/15302392597
This issue is similar to #30937 but the error is different so I decided to report both issues.