Spring can expose beans as JMX MBeans so you can monitor and manage a running application from JConsole, VisualVM or any JMX client. Annotate a class with @ManagedResource, mark readable or writable properties with @ManagedAttribute and callable methods with @ManagedOperation, and Spring registers the bean with the platform MBean server.
In Spring Boot, JMX support is switched off by default since Boot 2.2, so you enable it with spring.jmx.enabled=true. Plain Spring needs @EnableMBeanExport.
Java Management Extensions (JMX) is a standard API in the JDK for managing and monitoring applications. A managed resource is represented by an MBean, which has attributes (readable and writable values) and operations (methods you can invoke). Tools such as JConsole, VisualVM and Java Mission Control connect to the MBean server of a JVM and show these.
Writing standard MBeans by hand requires an interface named after the class and registration code. Spring removes this boilerplate. It builds the MBean description from annotations and registers the bean for you through an MBeanExporter.
In Spring Boot, set the property in application.properties:
With this on, Boot registers beans annotated with @ManagedResource automatically. It also exposes Actuator endpoints over JMX when Spring Boot Actuator is on the classpath; limit them with management.endpoints.jmx.exposure.include.
In a non-Boot application, add @EnableMBeanExport to a configuration class:
The example below exposes a feature switch and a counter, plus a method to reset it. Everything not annotated stays hidden from JMX.
A getter marks an attribute readable, and adding a setter makes it writable. Without objectName, Spring derives a name from the package and bean name.
Local JVMs appear automatically in JConsole (jconsole in the JDK bin folder). Open the MBeans tab, find the com.example.app domain and expand OrderStats. You can read Processed, flip AcceptingOrders and run reset from the Operations tab.
For remote access, start the JVM with the standard JMX options. Always enable authentication and TLS outside a trusted network.
Beans can implement NotificationPublisherAware to send JMX notifications to listeners, for example when a threshold is crossed. For numeric time series (latency, counts) prefer Micrometer, which can publish to Prometheus or a JMX registry, and use JMX mainly for management operations and ad-hoc inspection.
Treat an exposed MBean server as an administrative interface:
Not in current versions. Since Spring Boot 2.2 you must set spring.jmx.enabled=true.
@ManagedAttribute exposes a property through its getter or setter, while @ManagedOperation exposes a method that a client can invoke.
Yes. @ManagedResource works with plain Spring and Boot alone. Actuator only adds its own endpoints to JMX.
Register it with an MBeanExporter and an MBeanInfoAssembler such as MethodNameBasedMBeanInfoAssembler, which selects exposed methods by name instead of annotations.
Annotate beans with @ManagedResource, @ManagedAttribute and @ManagedOperation, enable JMX with spring.jmx.enabled=true (Boot) or @EnableMBeanExport (plain Spring), and inspect them in JConsole or VisualVM. Secure any remote JMX port, and use Micrometer for metrics rather than JMX attributes.