代码之家  ›  专栏  ›  技术社区  ›  Robert Munteanu

使用JMX导出Spring@Bean对象

  •  2
  • Robert Munteanu  · 技术社区  · 14 年前

    @Configuration ,我注意到 @Bean 未使用JMX注册。

    豆子是用线连接的

    @Bean
    protected CountingHttpInterceptor countingHttpInterceptor() {
    
        return new CountingHttpInterceptor();
    }
    

    类的定义是

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

    这个 @配置 文件是在构建了基于XML的主应用程序上下文之后处理的,并且没有机会参与使用xmlbean定义激活的发现过程( org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource 和弗里德)。

    如何从JMX启用 文件?


    更新

    <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>
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   lane.maxwell    10 年前

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

    您的配置类将如下所示。。。

    @Configuration
    @EnableMBeanExport
    public class SpringConfiguration {
       @Bean
       protected CountingHttpInterceptor countingHttpInterceptor() {
          return new CountingHttpInterceptor();
       }
    }
    
        2
  •  4
  •   skaffman    14 年前

    尽管世界的诱惑 @Configuration <context:mbean-export> . 这些基本上表示由交互对象的复杂排列组成的“宏”。

    现在,你 能够 同学们,但这真的是太麻烦了。相反,我建议将这些系统级的东西放到XML中,并从 @配置

    @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>