使用JMX导出Spring @Bean对象 [英] Exporting Spring @Bean objects using JMX

查看:99
本文介绍了使用JMX导出Spring @Bean对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring的@Configuration,并且我注意到@Bean并未使用JMX注册.

I'm using Spring's @Configuration, and I noticed that a @Bean does not get registered using JMX.

bean的接线方式为

The bean is wired as

@Bean
protected CountingHttpInterceptor countingHttpInterceptor() {

    return new CountingHttpInterceptor();
}

并且类定义是

@ManagedResource
public class CountingHttpInterceptor implements HttpRequestInterceptor, HttpResponseInterceptor { /* code here*/ }

@Configuration文件在构建基于XML的主应用程序上下文之后进行处理,并且没有机会参与使用XML bean定义(org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource和frieds)激活的发现过程.

This @Configuration file is processed after the main , XML-based, application context is built, and does not have the chance to take part in the discovery process which is activated using XML bean definitions ( org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource and frieds ).

如何从@Configuration文件中JMX启用bean?

How can I JMX-enable the beans from the @Configuration file?

更新:xml配置

<bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="assembler" ref="assembler"/>
    <property name="namingStrategy" ref="namingStrategy"/>
    <property name="autodetect" value="true"/>
</bean>

<bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
    <property name="attributeSource" ref="jmxAttributeSource"/>
</bean>

<bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
    <property name="attributeSource" ref="jmxAttributeSource"/>
</bean>

推荐答案

您所拥有的一切都是正确的.在@Configuration类中,您需要再添加一个注释以导出MBean @EnableMBeanExport.

Everything you have is correct. In your @Configuration class you need to add one more annotation to export your MBeans, @EnableMBeanExport.

您的配置类看起来像这样...

Your configuration class would look something like this...

@Configuration
@EnableMBeanExport
public class SpringConfiguration {
   @Bean
   protected CountingHttpInterceptor countingHttpInterceptor() {
      return new CountingHttpInterceptor();
   }
}

这篇关于使用JMX导出Spring @Bean对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆