我有一个小测试应用程序,用于使用Spring向JMX公开“Bean”。它使用基于XML的配置,一切正常:
<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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.dmclaughlin.spring" />
<context:property-placeholder location="classpath:test.properties"/>
<bean id="SimpleJmxController" class="com.dmclaughlin.spring.jmx.SimpleJmxBean">
<property name="activated" value="${some.activated}"/>
</bean>
<!-- Spring JMX -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="autodetect" value="true"></property>
<property name="namingStrategy" ref="namingStrategy"></property>
<property name="assembler" ref="assembler"></property>
</bean>
<bean id="attributeSource"
class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
<bean id="assembler"
class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
<property name="attributeSource" ref="attributeSource"/>
</bean>
<bean id="namingStrategy"
class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
<property name="attributeSource" ref="attributeSource"/>
</bean>
但我需要添加此功能的应用程序,使用@Configuration样式,我正在尝试将上述XML转换为工作。我添加了这样的东西:
@Bean
public MetadataNamingStrategy getNamingStrategy() {
MetadataNamingStrategy strategy = new MetadataNamingStrategy();
strategy.setAttributeSource(new AnnotationJmxAttributeSource());
return strategy;
}
@Bean
public MetadataMBeanInfoAssembler getMbeanInfoAssembler() {
return new MetadataMBeanInfoAssembler(new AnnotationJmxAttributeSource());
}
@Bean
public MBeanExporter getExporter() {
MBeanExporter exporter = new MBeanExporter();
exporter.setAutodetect(true);
exporter.setNamingStrategy(getNamingStrategy());
exporter.setAssembler(getMbeanInfoAssembler());
return exporter;
}
所有内容都会编译,但是当我加载JConsole时,我的Bean注释了@ManagedResource并且@ManagedAttribute没有公开。我在这里错过了一些简单的东西吗?
编辑:下面的答案没有解决我的问题(问题是我在Tomcat环境中测试我的XML,但在独立应用程序中测试我的非XML配置,这意味着那里是没有JMXServer出现.. d'oh),但它确实帮助我简化了一次我调试我搞砸了。
答案 0 :(得分:3)
对于me,只需添加:
@Bean
public AnnotationMBeanExporter annotationMBeanExporter() {
return new AnnotationMBeanExporter();
}
答案 1 :(得分:0)
您应该使用&#34; eager&#34;
配置您的mbeanexporter@Bean
@Lazy(false)
public MBeanExporter getExporter() {
...
}
问候
AccLess