Skip to content

Avoid need for reflection hints for MBeanExporter in native image #30846

@beckermarc

Description

@beckermarc

When setting spring.jmx.enabled to true in a Spring Boot application and building a native image the native image fails with the following exception during startup:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mbeanExporter': Instantiation of supplied bean failed
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1220) ~[demo:6.0.10]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1158) ~[demo:6.0.10]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[demo:6.0.10]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[demo:6.0.10]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[demo:6.0.10]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[demo:6.0.10]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[demo:6.0.10]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[demo:6.0.10]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[demo:6.0.10]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[demo:6.0.10]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[demo:6.0.10]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[demo:3.1.1]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[demo:3.1.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[demo:3.1.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[demo:3.1.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[demo:3.1.1]
        at com.example.demo.DemoApplication.main(DemoApplication.java:10) ~[demo:na]
Caused by: java.lang.IllegalArgumentException: Only values of autodetect constants allowed
        at org.springframework.jmx.export.MBeanExporter.setAutodetectMode(MBeanExporter.java:237) ~[demo:6.0.10]
        at org.springframework.jmx.export.annotation.AnnotationMBeanExporter.<init>(AnnotationMBeanExporter.java:51) ~[demo:6.0.10]
        at org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration.mbeanExporter(JmxAutoConfiguration.java:68) ~[demo:3.1.1]
        at org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration__BeanDefinitions.lambda$getMbeanExporterInstanceSupplier$1(JmxAutoConfiguration__BeanDefinitions.java:40) ~[na:na]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:68) ~[demo:6.0.10]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:54) ~[demo:6.0.10]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.lambda$get$2(BeanInstanceSupplier.java:200) ~[na:na]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58) ~[demo:6.0.10]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46) ~[demo:6.0.10]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.invokeBeanSupplier(BeanInstanceSupplier.java:212) ~[na:na]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:200) ~[na:na]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:947) ~[demo:6.0.10]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1214) ~[demo:6.0.10]
        ... 16 common frames omitted

This is due to missing reflection configuration on fields of MBeanExporter, which is required by the new Constants(MBeanExporter.class) call here: https://github.com/spring-projects/spring-framework/blob/main/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java#L142

After adding a reflection-configuration.json with the following content to the application the error no longer occurs:

[
	{
		"name": "org.springframework.jmx.export.MBeanExporter",
		"allDeclaredFields": true
	}
]

Is it reasonable to add this reflection configuration to Spring?

Steps to reproduce

  • Initialize a new Spring Boot app with Native Image support using Spring Initializr
  • Set spring.jmx.enabled: true in application.properties
  • Build native image using mvn native:compile -Pnative
  • Start the application and observe the exception

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)theme: aotAn issue related to Ahead-of-time processingtype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions