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

查看:29
本文介绍了使用 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 连接为

@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 功能?

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天全站免登陆