I ran into the same problem and designed a Cemo solution . Here is an example implementation.
context.xml
<bean id="mbeanExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter"> <property name="namingStrategy"> <bean class="com.foo.MultiAppMetadataNamingStrategy"> <property name="applicationName" value="${application.name}" /> </bean> </property> </bean>
MultiAppMetadataNamingStrategy.java
public class MultiAppMetadataNamingStrategy implements ObjectNamingStrategy, InitializingBean { private String applicationName; public MultiAppMetadataNamingStrategy() { } public MultiAppMetadataNamingStrategy(String applicationName) { this.applicationName = Preconditions.checkNotNull(applicationName, "applicationName must not be null"); } public void setApplicationName(String applicationName) { this.applicationName = Preconditions.checkNotNull(applicationName, "applicationName must not be null"); } @Override public void afterPropertiesSet() throws Exception { if (applicationName == null) { throw new IllegalArgumentException("Property 'applicationName' is required"); } } @Override public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException { Class managedClass = AopUtils.getTargetClass(managedBean); String domain = ClassUtils.getPackageName(managedClass); Hashtable<String, String> properties = new Hashtable<>(); properties.put("type", ClassUtils.getShortName(managedClass)); properties.put("name", beanKey);
This allows you to configure mbean as:
package com.foo; @ManagedResource(description = "Bean description") public class MyBean { ... }
which will register mbean with the object name com.foo:name=myBean,type=MyBean,app=CustomAppName
source share