2011-05-31 133 views
3

文档我使用了这些Spring注解一些代码:如何自动生成的类注释春天JMX注释

org.springframework.jmx.export.annotation.ManagedAttribute; 
org.springframework.jmx.export.annotation.ManagedOperation; 
org.springframework.jmx.export.annotation.ManagedOperationParameter; 
org.springframework.jmx.export.annotation.ManagedOperationParameters; 
org.springframework.jmx.export.annotation.ManagedResource; 

我要生成使用注解注释的一些文件(甚至只是的javadoc)例如考虑以下方法?

@ManagedOperation(description="Does foo to bar") 
@ManagedOperationParameters({ 
@ManagedOperationParameter(name = "bar", description = "The bar you want to foo.")})  
public long fooBar(Bar bar) throws Exception { 
    ... 
} 

有一些方法可以让我自动生成此文件,否则我将不得不重复,除了它在所有的javadoc注释字符串?

回答

2

首先,使用委托给getRegisteredObjectNames()的公共方法创建一个自定义AnnotationMbeanExporter。用它作为你的mbeanExporter。

例如:

@Component 
// This is a copy of the AnnotationMBeanExporter with a public version of getRegisteredObjectNames() 
public class AnnotationMBeanExporter extends MBeanExporter { 

    @Autowired 
    MBeanServer mbeanServer; 

    AnnotationJmxAttributeSource annotationSource = new AnnotationJmxAttributeSource(); 

    AnnotationMBeanExporter() { 
     setServer(mbeanServer); 
     setNamingStrategy(new MetadataNamingStrategy(annotationSource)); 
     setAssembler(new MetadataMBeanInfoAssembler(annotationSource)); 
     setAutodetectMode(MBeanExporter.AUTODETECT_ALL); 
    } 

    public ObjectName[] getExportedObjectNames() { 
    return getRegisteredObjectNames(); 
    } 
} 

然后为您的报告,迭代对象名称从getExportedObjectNames()返回,并得到每个JMX豆相关的元数据。

例如:

for (ObjectName objectName: mbeanExporter.getExportedObjectNames()) { 
     MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(objectName); 
     MBeanOperationInfo[] operations = mbeanInfo.getOperations(); 
     // etc. 
    } 
+2

我想你错过了 'Java' 作为标签 – abalogh 2011-05-31 18:42:36

+0

:)是微不足道的是转换成Java。我剪切和粘贴现有的代码。 – sourcedelica 2011-05-31 22:21:56

+0

好吧好吧:)转换为java。 – sourcedelica 2011-05-31 22:49:08