1

As shown in the following figure, a custom Java agent package is added to the JVM options

-javaagent:/usr/local/maven/apache-maven-3.6.3/repository/com/graviton/Probe-Agent/1.0-SNAPSHOT/Probe-Agent-1.0-SNAPSHOT.jar 

Customized a jar named Probe-Agent to output logs for methods in Spring boot applications. Specifically, it outputs logs for methods that carry the custom annotation LogAdvice, as shown below

@RestController public class HelloController { @GetMapping("/hello") @LogAdvice public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { return String.format("Hello %s!", name); } } 
@SpringBootApplication @RestController public class SpringBootProbeApplication { public static void main(String[] args) { SpringApplication.run(SpringBootProbeApplication.class, args); } } 

The key methods in the Probe-Agent jar are shown below

public class ProbeAgent { public static void premain(String agentArgs, Instrumentation inst) { System.out.printf("filter class start: %d\n", System.currentTimeMillis()); LogAdviceTransformer logAdviceTransformer = new LogAdviceTransformer(); inst.addTransformer(logAdviceTransformer, true); System.out.printf("filter class end: %d\n", System.currentTimeMillis()); } } 

However, an error occurs when I start the Springboot application

ERROR o.s.boot.SpringApplication -- Application run failed org.springframework.beans.factory.BeanDefinitionStoreException: Could not enhance configuration class [com.graviton.springboot.probe.SpringBootProbeApplication]. Consider declaring @Configuration(proxyBeanMethods=false) without inter-bean references between @Bean methods on the configuration class, avoiding the need for CGLIB enhancement. at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:120) at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:534) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:311) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:363) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:153) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:788) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) at com.graviton.springboot.probe.SpringBootProbeApplication.main(SpringBootProbeApplication.java:12) Caused by: org.springframework.cglib.core.ReflectUtils$2: No compatible defineClass mechanism detected: JVM should be started with --add-opens=java.base/java.lang=ALL-UNNAMED for ClassLoader.defineClass to be accessible. On the module path, you may not be able to define this CGLIB-generated class at all. at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:547) at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:371) at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:575) at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.lambda$new$1(AbstractClassGenerator.java:107) at org.springframework.cglib.core.internal.LoadingCache.lambda$createEntry$1(LoadingCache.java:52) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:57) at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:130) at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:317) at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:562) at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:407) at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:147) at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:112) ... 13 common frames omitted Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @5f058f00 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199) at java.base/java.lang.reflect.Method.setAccessible(Method.java:193) at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:505) ... 26 common frames omitted 

I changed the JVM options.

-javaagent:/usr/local/maven/apache-maven-3.6.3/repository/com/graviton/Probe-Agent/1.0-SNAPSHOT/Probe-Agent-1.0-SNAPSHOT.jar --add-opens=java.base/java.lang=ALL-UNNAMED 

but it doesn't seem to work.

Application run failed org.springframework.beans.factory.BeanDefinitionStoreException: Could not enhance configuration class [com.graviton.springboot.probe.SpringBootProbeApplication]. Consider declaring @Configuration(proxyBeanMethods=false) without inter-bean references between @Bean methods on the configuration class, avoiding the need for CGLIB enhancement. at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:120) at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:534) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:311) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:363) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:153) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:788) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) at com.graviton.springboot.probe.SpringBootProbeApplication.main(SpringBootProbeApplication.java:12) Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.LinkageError-->loader 'app' attempted duplicate class definition for com.graviton.springboot.probe.SpringBootProbeApplication$$SpringCGLIB$$0. (com.graviton.springboot.probe.SpringBootProbeApplication$$SpringCGLIB$$0 is in unnamed module of loader 'app') at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:510) at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:371) at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:575) at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.lambda$new$1(AbstractClassGenerator.java:107) at org.springframework.cglib.core.internal.LoadingCache.lambda$createEntry$1(LoadingCache.java:52) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:57) at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:130) at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:317) at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:562) at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:407) at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:147) at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:112) ... 13 common frames omitted Caused by: java.lang.LinkageError: loader 'app' attempted duplicate class definition for com.graviton.springboot.probe.SpringBootProbeApplication$$SpringCGLIB$$0. (com.graviton.springboot.probe.SpringBootProbeApplication$$SpringCGLIB$$0 is in unnamed module of loader 'app') at java.base/java.lang.ClassLoader.defineClass1(Native Method) at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:507) ... 26 common frames omitted Process finished with exit code 1 
1
  • Welcome to SO. If you can publish a complete, minimal reproducer - ideally a Maven project - on GitHub and link to it, I could take a look. Reproducibility is a cornerstone of solving programming problems. It would also be interesting to see what the LogAdviceTransformer does. Commented Jun 11, 2024 at 6:35

1 Answer 1

0

With some investigations it seems that something similar to this issue is related to an old bug in Spring.

Incase you haven't already, could you try the suggestions and also re-confirm your spring version as described in Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1

Sign up to request clarification or add additional context in comments.

2 Comments

I try to add tis jvm option but it doesn't seem work
Apologies, I haven't worked with instrumentation before but looking at the logs could it be that the LogTransfomer is applying some transformation to the main "SpringBootProbeApplication" class as well. Therefore when spring attempts to reload the context cglib ends up writing another version of the main class which throws the above error? CgLib had some known issues with detecting duplicate classes stackoverflow.com/questions/19741285/… Is it possible to exclude the main application file from the transformer?(if any of the above is valid)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.