如何在 MBean 描述中公开 属性

How to expose a property in MBean description

项目中存在以下托管操作:

@ManagedOperation(description = "Some description")
@ManagedOperationParameters({
        @ManagedOperationParameter(name = "key", description = "Some description"),
})
public void foo(String key) {
    // some logic
}

还有一个 属性 可以在 Spring 上下文中使用,方法是用美元符号和方括号括起来:

"${some.property.key}"

是否可以在托管操作注释描述中使用上述 属性 键的值?类似于:

@ManagedOperationParameter(name = "key", 
    description = "Some description, please note that the key is ${some.property.key}")

不是 out-of-the-box,但自定义起来非常容易...

public class CustomAttributeSource extends AnnotationJmxAttributeSource implements EmbeddedValueResolverAware {

    private StringValueResolver embeddedValueResolver;

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.embeddedValueResolver = resolver;
    }

    @Override
    public ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadataException {
        ManagedAttribute managedAttribute = super.getManagedAttribute(method);
        if (this.embeddedValueResolver != null) {
            managedAttribute
                    .setDescription(this.embeddedValueResolver.resolveStringValue(managedAttribute.getDescription()));
        }
        return managedAttribute;
    }

    @Override
    public ManagedOperation getManagedOperation(Method method) throws InvalidMetadataException {
        ManagedOperation managedOperation = super.getManagedOperation(method);
        if (this.embeddedValueResolver != null) {
            managedOperation
                    .setDescription(this.embeddedValueResolver.resolveStringValue(managedOperation.getDescription()));
        }
        return managedOperation;
    }

}

然后...

<bean class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
    <property name="assembler">
        <bean class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
            <property name="attributeSource">
                <bean class="foo.CustomAttributeSource" />
            </property>
        </bean>
    </property>
</bean>