【问题标题】:Getting exception while refreshing Spring ApplicationContext in Spring Boot application在 Spring Boot 应用程序中刷新 Spring ApplicationContext 时出现异常
【发布时间】:2014-12-16 07:53:54
【问题描述】:

我们正在为我们的应用程序使用 Spring Boot。 启动应用程序后,在运行时我们正在向现有的 Applicationcontext 添加(加载)一个新 Bean。

AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
appContext.register(NewBean.class);
appContext.refresh();

添加 bean 后,我们正在刷新 applicationContext

在刷新期间,MBean 试图重新注册一些端点,我们收到以下错误 (所有这些端点都出现错误 - requestMappingEndpoint、environmentEndpoint、healthEndpoint、beanEndpoint、infoEndpoint、metricsEndpoint、traceEndpoint、dumpEndpoint、 autoConfigurationAuditEndpoint, shutdownEndpoint, configurationPropertiesReportEndpoint)

Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Endpoint,name=configurationPropertiesReportEndpoint
        at com.sun.jmx.mbeanserver.Repository.addMBean(Unknown Source)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(Unknown Source)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(Unknown Source)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(Unknown Source)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(Unknown Source)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(Unknown Source)
        at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
        at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:662)
        at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:605)

谁能告诉我如何跳过这个异常?

我也试过以下方法

@EnableIntegrationMBeanExport(registration = RegistrationPolicy.REPLACE_EXISTING)

但得到以下异常

Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [org.springframework.integration.monitor.IntegrationMBeanExporter@16c5464] with key 'integrationMbeanExporter'; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.integration.monitor:name=integrationMbeanExporter,type=IntegrationMBeanExporter
    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:609)
    at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:534)
    at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:416)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 22 more
Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.integration.monitor:name=integrationMbeanExporter,type=IntegrationMBeanExporter
    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.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
    at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:662)
    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:599)
    ... 26 more

【问题讨论】:

  • 你应该先销毁它,否则 bean 会继续运行。为什么需要添加bean 添加运行时?你在这里展示的是你正在构建一个新的上下文,所以不确定你认为应该发生什么? register 方法也适用于配置类而不是任意 bean,如果您想添加这些,只需使用 ApplicationContext 上的方法之一,例如 registerSingleton。但总的来说,在运行时添加 bean 应该是一件坏事(恕我直言)。
  • @M.Deinum 这相当于一个答案,而不是一个评论。

标签: java spring spring-boot jmx


【解决方案1】:

现有 ApplicationContext 上调用 refresh 之前,您应该首先销毁它,否则 bean 会继续运行。

AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
appContext.register(NewBean.class);
appContext.refresh();

您在这里展示的是您正在构建一个 上下文,而不是重用现有的上下文。 register 方法也适用于@Configuration 类而不是任意bean。如果您想添加这些,只需使用ApplicationContext 上的一种方法,例如registerSingleton。但总的来说,在运行时添加 bean 应该是一件坏事(恕我直言)。

【讨论】:

    【解决方案2】:

    有些 applicationContext 可以刷新时间,但 AnnotationConfigApplicationContext 不行。 构造AnnotationConfigApplicationContext时,已经运行了refresh方法,所以会报异常,可以先销毁,再刷新

    【讨论】:

      猜你喜欢
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 2019-04-23
      • 2020-06-24
      • 2015-07-18
      • 2018-10-22
      相关资源
      最近更新 更多