使用JMX导出Spring @Bean对象 [英] Exporting Spring @Bean objects using JMX
问题描述
我正在使用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屋!