取消部署WebLogic应用程序时自动取消注册MBean(由Spring注册)

在我的Spring应用程序(部署在WebLogic服务器上)中,我有以下Spring bean定义:
<context:mbean-server />

<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
        <map>
            <entry key="SpringBeans:name=hibernateStatisticsMBean,subsystem=${subsystem}" value-ref="hibernateStatisticsMBean" />                           
        </map>
    </property>
</bean>

<bean name="hibernateStatisticsMBean" class="org.hibernate.jmx.StatisticsService">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
它在部署应用程序时注册新的MBean并且运行良好。但是,当我取消部署应用程序并再次部署它时,它会抱怨MBean已存在。 如何在应用程序取消部署期间自动取消注册MBean?它可以通过Spring完成,还是需要为此做一些WebLogic魔术?     
已邀请:
添加以下属性:
<property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>
这样你的mBeanExporter看起来像:
<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>
    <property name="beans">
        <map>
            <entry key="SpringBeans:name=hibernateStatisticsMBean,subsystem=${subsystem}" value-ref="hibernateStatisticsMBean" />                           
        </map>
    </property>
</bean>
    
使用REGISTRATION_REPLACE_EXISTING可以解决您的问题,即它让应用程序重新启动,但它没有解决您提出的问题 - “如何在应用程序取消部署期间自动取消注册MBean?”。 MBeanExporter实现了DisposableBean,因此当ApplicationContext关闭时(应用程序取消部署时应该发生),会调用其destroy方法,该方法会注销先前注册的bean和侦听器。 有多种日志记录可以输出取消注册期间出现的任何问题。 您应该检查ApplicationContext是否实际关闭,因为这是触发器。     
当前接受的答案现在引用已被弃用的代码。为了遵守更新(Spring 3.2及更高版本),必须进行一些小的更改。
<property name="registrationPolicy">
   <util:constant static-field="org.springframework.jmx.support.RegistrationPolicy.REPLACE_EXISTING" />
</property>
“registrationPolicy”属性替换了提供的答案中的“registrationBehaviorName”。     

要回复问题请先登录注册