Spring JMX support

Spring provides a nice way to expose plain java objects and its
methods and properties as JMX managed objects. Maybe the best way to do
so is using annotations.

        objectName = "domain:name=testBean",
        description = "A test Spring-JMX bean")
public class JmxTestBean {

    private int age;

    private String name;

    @ManagedAttribute(description = "age attribute")
    public void setAge(int age) {
        this.age = age;

    @ManagedAttribute(description = "age attribute")
    public int getAge() {
        return age;

    @ManagedAttribute(description = "name attribute")
    public void setName(String name) {
        this.name = name;

    @ManagedAttribute(description = "name attribute")
    public String getName() {
        return name;

    public String dontExposeThisMethod() {
        return "dontExposeThisMethod return";

    @ManagedOperation(description = "method exposed")
    public String exposeThisMethod() {
        return "exposeThisMethod return";


In the application context you need

    <bean id="jmxAdapter.parent" class="org.springframework.jmx.export.MBeanExporter" abstract="true">
        <property name="autodetect">
        <property name="assembler">
            <ref local="metadataMBeanInfoAssembler"/>
        <property name="namingStrategy">
            <ref local="namingStrategy"/>

    <!-- Use metadata to filter the exposed operations and attributes -->
    <bean id="metadataMBeanInfoAssembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
        <property name="attributeSource">
            <ref local="annotationJmxAttributeSource"/>
    <bean id="annotationJmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
    <bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
        <property name="attributeSource">
            <ref local="annotationJmxAttributeSource"/>

    <bean id="testBean" class="com.arcmind.springjmx.JmxTestBean">
        <property name="name">
        <property name="age">

For testing you’d need to explicitly set the mbean server

    <bean id="jmxAdapter" parent="jmxAdapter.parent">
        <property name="server">
            <ref local="mbeanServer"/>

    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"/>

For deploying it gets autodetected

    <bean id="jmxAdapter" parent="jmxAdapter.parent"/>

Other options for the assembler property are

    <!-- Expose everything through reflection -->
    <bean id="simpleReflectiveMBeanInfoAssembler" class="org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler"/>

    <!-- Use method names to filter the exposed operations and attributes -->
    <bean id="methodNameBasedMBeanInfoAssembler" class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler">
        <property name="managedMethods">

    <!-- Expose only those methods in the interfaces implemented by the bean -->
    <bean id="interfaceBasedMBeanInfoAssembler" class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler"/>

But then you may need to explicitly say what beans you want to
expose instead of autodetecting them in the jmxAdapter.parent bean.

        <property name="beans">
                <entry key="bean:name=testBean1">
                    <ref local="testBean"/>


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s