The @ManagedResource annotation marks a Spring bean so that Spring's JMX support exports it to the platform MBeanServer as an MBean. Once exported, tools such as JConsole or VisualVM can read its attributes and invoke its operations. You then choose what is exposed by annotating individual members with @ManagedAttribute and @ManagedOperation.
The annotation lives in org.springframework.jmx.export.annotation. It does nothing by itself: an MBean exporter must be active in the application context, and only annotated members are exposed.
Spring's MBeanExporter looks for beans carrying @ManagedResource and registers them with the MBeanServer. An MetadataMBeanInfoAssembler reads the annotations and builds the MBean description, so you never write an MBean interface or extend StandardMBean by hand.
The attributes of @ManagedResource are:
| Attribute | Purpose |
|---|---|
objectName | The JMX ObjectName, for example com.example:type=Counter |
description | Text shown by JMX consoles for the MBean |
currencyTimeLimit | Seconds attribute values may be cached before being re-read |
log, logFile | Optional notification logging settings |
persistPolicy, persistPeriod, persistName, persistLocation | Metadata hints for persisting MBean state, used by JMX implementations that support it |
If you leave objectName out, Spring derives a name from the bean, which is fine for experiments but hard to find in a console. In production, set it explicitly. The value is a plain string; it is not a Spring Expression Language expression.
In plain Spring, add @EnableMBeanExport to a configuration class:
In Spring Boot, the exporter is auto-configured but JMX is switched off by default since Boot 2.2. Turn it on in application.properties:
Boot then exports every bean annotated with @ManagedResource and also uses spring.jmx.default-domain for generated names if you set it.
The example below exposes a counter as a read-write attribute and a method as an operation:
After starting the application, open JConsole or VisualVM with the MBeans plugin, find com.example and select Counter. You will see the Counter attribute, which you can read and edit, and the increment operation, which you can invoke. internalHelper() does not appear.
The original idea that every public getter becomes a JMX attribute is wrong for annotation-based export. With @ManagedResource, Spring exposes only members you annotate:
@ManagedAttribute goes on a getter or setter. Annotating only the getter gives a read-only attribute; annotating both gives read-write.@ManagedOperation goes on a method you want to invoke from a console. Do not annotate getters with it when you mean an attribute.@ManagedOperationParameter and @ManagedOperationParameters give names and descriptions to operation arguments, which makes console forms readable.Prefer simple types for attributes and arguments: numbers, strings, booleans and enums. Complex objects require the JMX client to understand open types.
An exposed operation is a remote control for your application. Treat it that way:
com.sun.management.jmxremote.* system properties.management.endpoints.jmx.exposure.include.Usually the exporter is not active. In Spring Boot set spring.jmx.enabled=true, and in plain Spring add @EnableMBeanExport. Also check that the bean is a Spring bean and that the annotated members are public.
No. A standard MBean needs an interface named after the class with an MBean suffix. Spring builds the MBean description from annotations, so a plain class is enough.
Yes, if the attribute has a setter annotated with @ManagedAttribute. A getter alone produces a read-only attribute.
Exporting needs a single, stable instance and ObjectName, so use it on singleton beans.
@ManagedResource turns a Spring bean into an MBean that JMX tools can inspect and control. Enable export with @EnableMBeanExport or spring.jmx.enabled=true, set an explicit objectName, and annotate exactly the attributes and operations you want to expose. Keep remote JMX authenticated and encrypted, or use Actuator for routine monitoring.