4

I have 2 spring boot applications.

application_A dependsn_on application_B

Actually each of applications has main class marked as @SpringBootApplication

application_B starts successfully but application_A doesn't start:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication # JMX name of the application admin MBean.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at pack.Application.main(Application.java:36)
Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication # JMX name of the application admin MBean.
    at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
    at org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar.afterPropertiesSet(SpringApplicationAdminMXBeanRegistrar.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 15 common frames omitted

In debug I see that spring executes org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration#springApplicationAdminRegistrar twice during startup and fails.

I tried to set property:

 spring.application.admin.enabled=false

But it doesn't help me.

How can I avoid this exception?

P.S.

I've found this one https://github.com/spring-projects/spring-boot/issues/6378 but there is no solution

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

2 Answers2

1

In case somebody is still facing any issue regarding this you can consider this: As somebody has mentioned "I ocasionally invoked SpringApplication.run(Application.class, args); twice"

Solution that worked for me: We should start the application(ApplicationContext) through different class, i.e., other than @SpringBootApplication annotated class.

Why: @SpringBootApplication is the combination of three annotations: @ComponentScan, @EnableAutoConfiguration, and @Configuration, hence one instance will be created already. So when we create another instance of ApplicationContext interface (xml or annotated based) inside the same class, it will encounter already created instance and will throw exception.

Jagmit
  • 11
  • 2
0

The SpringApplicationAdminJmxAutoConfiguration has the code like:

String jmxName = this.environment.getProperty(JMX_NAME_PROPERTY,
            DEFAULT_JMX_NAME);
    if (this.mbeanExporters != null) { // Make sure to not register that MBean twice
        for (MBeanExporter mbeanExporter : this.mbeanExporters) {
            mbeanExporter.addExcludedBean(jmxName);
        }
    }
    return new SpringApplicationAdminMXBeanRegistrar(jmxName);

Where we have those constants:

/**
 * The property to use to customize the {@code ObjectName} of the application admin
 * mbean.
 */
private static final String JMX_NAME_PROPERTY = "spring.application.admin.jmx-name";

/**
 * The default {@code ObjectName} of the application admin mbean.
 */
private static final String DEFAULT_JMX_NAME = "org.springframework.boot:type=Admin,name=SpringApplication";

So, you should consider to make that jmx-name as unique for each application. I mean you need specify spring.application.admin.jmx-name configuration property.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • I tried to use *spring.application.admin.jmx-name=unique_name* and I get MalformedObjectNameException: Key properties cannot be empty – gstackoverflow Feb 01 '18 at 17:43
  • Correct. That must be something like: `org.springframework.boot:type=Admin,name=SpringApplication2` on the second app and so on. Please, study more what is object name in JMX – Artem Bilan Feb 01 '18 at 17:46
  • Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication2 – gstackoverflow Feb 01 '18 at 17:49
  • Looks like you did'n catch.When I start **application_A** - during startup spring invokes **org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration#springApplicationAdminRegistrar** twice with the same name(*org.springframework.boot:type=Admin,name=SpringApplication2*). It doesn't matter if **application_B** started or not – gstackoverflow Feb 01 '18 at 17:52
  • Do you mean even if you don't start `application_B`, the `application_A` fails with that exception because it calls `SpringApplicationAdminJmxAutoConfiguration` twice somehow? – Artem Bilan Feb 01 '18 at 19:15
  • BTW, any chances to have such a project somewhere on GitHub to let us to reproduce on our side ? – Artem Bilan Feb 01 '18 at 19:15
  • 5
    Actually I've found the mistake. I ocasionally invoked **SpringApplication.run(Application.class, args);** twice. – gstackoverflow Feb 02 '18 at 07:52
  • My Test cases were failing with this same error, on commenting out the following property in application.properties the issue got fixed `spring.application.admin.enabled=true` – balatamilmani Apr 26 '21 at 13:00