在Spring Boot应用程序中刷新Spring ApplicationContext时获取异常

时间:2014-12-16 07:53:55

标签: java spring spring-boot jmx

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

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

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

在刷新期间,MBean正在尝试重新注册某些端点,并且我们收到以下错误 (获取所有这些端点的错误 - requestMappingEndpoint,environmentEndpoint,healthEndpoint,beansEndpoint,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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

某些applicationContext可以刷新次数,但不能刷新AnnotationConfigApplicationContext。  当你构造运行了刷新方法的AnnotationConfigApplicationContext时,会报告异常,你可以先破坏它,然后刷新