使用JMX导出Spring @Bean对象

时间:2010-06-28 14:13:50

标签: java spring annotations jmx

我正在使用Spring的@Configuration,我注意到@Bean没有使用JMX注册。

bean连接为

@Bean
protected CountingHttpInterceptor countingHttpInterceptor() {

    return new CountingHttpInterceptor();
}

,类定义是

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

在构建基于XML的主应用程序上下文之后处理此@Configuration文件,并且没有机会参与使用XML bean定义激活的发现过程({{1}和炒的)。

我如何从org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource文件中启用JMX?


更新:xml配置

@Configuration

2 个答案:

答案 0 :(得分:4)

尽管存在基于@Configuration的方法的诱惑,但使用XML配置仍然可以做得更好。特别是基于命名空间的配置,例如<context:mbean-export>。这些基本上代表了由交互对象的复杂排列组成的“宏”。

现在,你可以在你的@Configuration课程中复制这个逻辑,但它确实比它的价值更麻烦。相反,我建议将这些系统级的东西放入XML中,然后从@Configuration类中导入它:

@ImportResource("/path/to/beans.xml")
public class MyConfig {
   @Bean
   protected CountingHttpInterceptor countingHttpInterceptor() {
      return new CountingHttpInterceptor();
   }
}

然后在/path/to/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           ">  

    <context:mbean-export/>
</beans>

答案 1 :(得分:2)

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

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

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