我遇到了同样的问题,并基于Cemo的解决方案进行了改进。这里是一个示例实现。
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);
properties.put("app", applicationName);
return ObjectNameManager.getInstance(domain, properties);
}
}
这可以设置一个像这样的MBean:
这允许设置类似于:
package com.foo;
@ManagedResource(description = "Bean description")
public class MyBean {
...
}
这将使用对象名称 com.foo:name=myBean,type=MyBean,app=CustomAppName
注册一个 MBean。
@...(name="${prop}FooBar")
- Bozho