2011-08-26 109 views
2

我有一個小的測試應用程序,用於使用Spring向JMX公開「Bean」。它使用基於XML的配置,一切工作正常:將JMX的XML Spring配置轉換爲Java配置

<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豆標註有@ManagedResource和@ManagedAttribute不外露。我在這裏錯過簡單的東西嗎?

編輯:回答下面沒有解決我的問題(這個問題是我在測試在Tomcat的環境我的XML,但在一個獨立的應用程序,這意味着有測試我的非XML配置是不存在JMXServer ..ð),但它確實幫助我簡化了一次,我調試了我搞砸了。

+0

是您的問題解決 – Bhupi

回答

3

對於me這足以補充:

@Bean 
public AnnotationMBeanExporter annotationMBeanExporter() { 
    return new AnnotationMBeanExporter(); 
} 
+0

謝謝,但這對我也不起作用。我有一個用@ManagedResource註解的「ServiceImpl」,它被IoCProvider找到..所以我不確定它爲什麼不出現在jConsole中。 –

+0

這適用於我(使用Spring Boot 0.5.0 M6) –

0

您應該配置與 「渴望」

@Bean 
@Lazy(false) 
public MBeanExporter getExporter() { 
... 
} 

問候

AccLess你的MBeanExporter