Allow MBeans to be excluded additively

Previously, one could only set the list of bean names to exclude from
auto-detection and there was no way to add additional bean names.

MBeanExporter now exposes a addExcludedBean method that can be invoked
during the initialization phase to add bean names to ignore.

Issue: SPR-12686
This commit is contained in:
Stephane Nicoll 2015-02-09 09:41:03 +01:00
parent 8f0ddf1b1d
commit 982f9ce6c9
2 changed files with 44 additions and 10 deletions

View File

@ -161,7 +161,7 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
private boolean exposeManagedResourceClassLoader = true; private boolean exposeManagedResourceClassLoader = true;
/** A set of bean names that should be excluded from autodetection */ /** A set of bean names that should be excluded from autodetection */
private Set<String> excludedBeans; private Set<String> excludedBeans = new HashSet<String>();
/** The MBeanExporterListeners registered with this exporter. */ /** The MBeanExporterListeners registered with this exporter. */
private MBeanExporterListener[] listeners; private MBeanExporterListener[] listeners;
@ -314,7 +314,18 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
* Set the list of names for beans that should be excluded from autodetection. * Set the list of names for beans that should be excluded from autodetection.
*/ */
public void setExcludedBeans(String... excludedBeans) { public void setExcludedBeans(String... excludedBeans) {
this.excludedBeans = (excludedBeans != null ? new HashSet<String>(Arrays.asList(excludedBeans)) : null); this.excludedBeans.clear();
if (excludedBeans != null) {
this.excludedBeans.addAll(Arrays.asList(excludedBeans));
}
}
/**
* Add the name of bean that should be excluded from autodetection.
*/
public void addExcludedBean(String excludedBean) {
Assert.notNull(excludedBean, "ExcludedBean must not be null");
this.excludedBeans.add(excludedBean);
} }
/** /**
@ -922,10 +933,9 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
* Indicates whether or not a particular bean name is present in the excluded beans list. * Indicates whether or not a particular bean name is present in the excluded beans list.
*/ */
private boolean isExcluded(String beanName) { private boolean isExcluded(String beanName) {
return (this.excludedBeans != null && return (this.excludedBeans.contains(beanName) ||
(this.excludedBeans.contains(beanName) || (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX) &&
(beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX) && this.excludedBeans.contains(beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length()))));
this.excludedBeans.contains(beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length())))));
} }
/** /**

View File

@ -17,6 +17,8 @@
package org.springframework.jmx.export; package org.springframework.jmx.export;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -643,6 +645,28 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests {
ObjectNameManager.getInstance(objectName2)); ObjectNameManager.getInstance(objectName2));
} }
@Test
public void testIgnoreBeanName() throws MalformedObjectNameException {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
String firstBeanName = "spring:type=TestBean";
factory.registerSingleton(firstBeanName, new TestBean("test"));
String secondBeanName = "spring:type=TestBean2";
factory.registerSingleton(secondBeanName, new TestBean("test2"));
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(getServer());
exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(firstBeanName, secondBeanName));
exporter.setBeanFactory(factory);
exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL);
exporter.addExcludedBean(secondBeanName);
start(exporter);
assertIsRegistered("Bean not autodetected in (AUTODETECT_ALL) mode",
ObjectNameManager.getInstance(firstBeanName));
assertIsNotRegistered("Bean should have been excluded",
ObjectNameManager.getInstance(secondBeanName));
}
private ConfigurableApplicationContext load(String context) { private ConfigurableApplicationContext load(String context) {
return new ClassPathXmlApplicationContext(context, getClass()); return new ClassPathXmlApplicationContext(context, getClass());
} }
@ -763,15 +787,15 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests {
private static final class NamedBeanAutodetectCapableMBeanInfoAssemblerStub extends private static final class NamedBeanAutodetectCapableMBeanInfoAssemblerStub extends
SimpleReflectiveMBeanInfoAssembler implements AutodetectCapableMBeanInfoAssembler { SimpleReflectiveMBeanInfoAssembler implements AutodetectCapableMBeanInfoAssembler {
private String namedBean; private Collection<String> namedBeans;
public NamedBeanAutodetectCapableMBeanInfoAssemblerStub(String namedBean) { public NamedBeanAutodetectCapableMBeanInfoAssemblerStub(String... namedBeans) {
this.namedBean = namedBean; this.namedBeans = Arrays.asList(namedBeans);
} }
@Override @Override
public boolean includeBean(Class<?> beanClass, String beanName) { public boolean includeBean(Class<?> beanClass, String beanName) {
return this.namedBean.equals(beanName); return this.namedBeans.contains(beanName);
} }
} }