【问题标题】:BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar'. InstanceAlreadyExistsExceptionBeanCreationException:创建名为“springApplicationAdminRegistrar”的 bean 时出错。 InstanceAlreadyExistsException
【发布时间】:2018-02-01 16:34:50
【问题描述】:

我有 2 个 Spring Boot 应用程序。

application_A 依赖于 application_B

实际上每个应用程序都有主类标记为@SpringBootApplication

application_B 启动成功但application_A 没有启动:

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

在调试中我看到 spring 在启动期间执行了两次 org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration#springApplicationAdminRegistrar 并且失败了。

我尝试设置属性:

 spring.application.admin.enabled=false

但这对我没有帮助。

如何避免这个异常?

附言

我找到了这个https://github.com/spring-projects/spring-boot/issues/6378,但没有解决办法

【问题讨论】:

    标签: java spring spring-boot spring-jmx


    【解决方案1】:

    SpringApplicationAdminJmxAutoConfiguration 的代码如下:

    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);
    

    我们有这些常量的地方:

    /**
     * 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";
    

    因此,您应该考虑将 jmx-name 设置为每个应用程序的唯一性。我的意思是你需要指定spring.application.admin.jmx-name 配置属性。

    【讨论】:

    • 我尝试使用 spring.application.admin.jmx-name=unique_name 得到 MalformedObjectNameException: Key properties cannot be empty
    • 正确。那一定是这样的:org.springframework.boot:type=Admin,name=SpringApplication2 在第二个应用程序上等等。请进一步研究 JMX 中的对象名称是什么
    • 引起:javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication2
    • 看起来你没有抓住。当我启动 application_A - 在启动期间 spring 调用 org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration#springApplicationAdminRegistrar 两次同名(org.springframework.boot:type=Admin,name=SpringApplication2)。 application_B 是否启动并不重要
    • 其实我发现了错误。我偶尔会调用 SpringApplication.run(Application.class, args); 两次。
    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 2015-09-24
    • 1970-01-01
    • 2020-02-07
    • 2018-12-17
    • 2011-10-09
    • 1970-01-01
    • 2015-02-19
    相关资源
    最近更新 更多