Feb 16 2009
Exposing MBeans with Spring
Using JDK 5.0 Annotations
An other way to expose Mbeans is to use the JDK5 annotations.
Spring provides a set of annotations that mirror the Commons Attribute attribute classes and an implementation of the JmxAttributeSource strategy interface, the AnnotationsJmxAttributeSource class, that allows the MBeanInfoAssembler to read them.
Putting all together
- applicationContext-jmx.xml
XML:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:tx="http://www.springframework.org/schema/tx"
-
xmlns:util="http://www.springframework.org/schema/util"
-
xsi:schemaLocation="
-
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
-
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
-
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
-
<!-- indicate to first look for a server -->
-
<property name="locateExistingServerIfPossible" value="true"/>
-
</bean>
-
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
-
<property name="assembler" ref="assembler"/>
-
<property name="namingStrategy" ref="namingStrategy"/>
-
<property name="autodetect" value="true"/>
-
</bean>
-
<bean name="com.mycompany.bean:type=beanType" class="com.mycompany.class"/>
-
<bean id="jmxAttributeSource"
-
<!-- will create management interface using annotation metadata -->
-
<bean id="assembler"
-
<property name="attributeSource" ref="jmxAttributeSource"/>
-
</bean>
-
<!-- will pick up the ObjectName from the annotation -->
-
<bean id="namingStrategy"class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
-
<property name="attributeSource" ref="jmxAttributeSource"/>
-
</bean>
-
</beans>
- Mbean trough annotations
JAVA:
-
@ManagedResource(objectName="com.mycompany:type=beanImpl", description="MBean desc", log=true,
-
public class XXX{
-
@ManagedOperation(description="zzz")
-
public void getX(){
-
}


I was trying to set up a JMX managed standalone Spring application and the article helped a lot. Thanks!!!
Hello, I found your post about the Spring mbeans in the WebSphere 6.1. I am new to Spring and JMX and looking for a step by step guide to register and calling by wsadmin some Spring’s mbeans provided by the app developer. I read your post and do understand the websphere naming strategy but where do I need to place this class WebsphereNamingStrategy ?
br
Mark
Hello Mark,
You can place your WebsphereNamingStrategy class where you want. For my part, I place it in an “util” package…
Cheers,
Gilles
Hello Gilles,
Thanks a lot for a quick response. I just have one question if there is any possibility to enable the Spring’s mbean in WAS 6.1 without adding any new code to the project ? I do not have the source code and do not want to extend the EAR file.
br
Mark
excellent info, thanks!
was trying to get this running on websphere and also figured out that websphere changes the objectnames of the mbeans..your sources saved a lot of time for me yeehaa