Following various example configurations from Spring documentation as well as some forums on the Internet, my application context file looks like:
<beans>
<bean id="dH" class="abc.def.ghi.DH">
<constructor-arg>
<value>0</value>
</constructor-arg>
<property name="num" value="100"/>
</bean>
<bean class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=dH1" value-ref="dH"/>
</map>
</property>
</bean>
<bean class="org.springframework.jmx.support.MBeanServerFactoryBean"/>
</beans>
I’m running this without any container and on plain JVM. I’m able to connect to my process via JConsole but the MBean doesn’t show up. However registering the bean programmatically exposes it successfully.
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
DH dh = new DH(0);
mbeanServer.registerMBean(dh, new ObjectName("bean:name=dH1"));
I’ve tried playing with the Spring configuration without success. I think the bean is not registering to the already running MBean server that was accessible from ManagementFactory.getPlatformMBeanServer().
Any ideas on the issue?
In addition to defining an MBeanServerFactory bean (as Nicholas noted in their answer) using …
… you need to tell the MBeanExporter what to manage:
Assuming your
abc.def.ghi.DHclass does not implement any JMX interface, try defining yourMBeanExporteras:Looking at the OpenJDK 7, update 2, build 21
DefaultMBeanServerInterceptor.javasource, line 898 creates aDynamicMBeanfor regular objects:I haven’t debugged it, but I bet
mbeanServer.registerMBean(dh, new ObjectName("bean:name=dH1"))eventually callsDefaultMBeanServerInterceptor.registerObject(), which creates aDynamicMBeanfor you and properly registers your standardJavaBeanproperties’ setters and getters.Here are some test files that work using Spring Framework 3.0.5 and Oracle HotSpot Java 1.6.0_24. After setting your
CLASSPATHenvironment variable, just runjavac *.javaandjava Mainand use VisualVM (or similar application) to connect to the running java application to see the registered MBeans.ac.xml:
Test.java:
Main.java: