Skip to content

Commit 09bd703

Browse files
committed
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
1 parent 49c21a0 commit 09bd703

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
161161
private boolean exposeManagedResourceClassLoader = true;
162162

163163
/** A set of bean names that should be excluded from autodetection */
164-
private Set<String> excludedBeans;
164+
private Set<String> excludedBeans = new HashSet<String>();
165165

166166
/** The MBeanExporterListeners registered with this exporter. */
167167
private MBeanExporterListener[] listeners;
@@ -314,7 +314,18 @@ public void setExposeManagedResourceClassLoader(boolean exposeManagedResourceCla
314314
* Set the list of names for beans that should be excluded from autodetection.
315315
*/
316316
public void setExcludedBeans(String... excludedBeans) {
317-
this.excludedBeans = (excludedBeans != null ? new HashSet<String>(Arrays.asList(excludedBeans)) : null);
317+
this.excludedBeans.clear();
318+
if (excludedBeans != null) {
319+
this.excludedBeans.addAll(Arrays.asList(excludedBeans));
320+
}
321+
}
322+
323+
/**
324+
* Add the name of bean that should be excluded from autodetection.
325+
*/
326+
public void addExcludedBean(String excludedBean) {
327+
Assert.notNull(excludedBean, "ExcludedBean must not be null");
328+
this.excludedBeans.add(excludedBean);
318329
}
319330

320331
/**
@@ -922,10 +933,9 @@ private void autodetect(AutodetectCallback callback) {
922933
* Indicates whether or not a particular bean name is present in the excluded beans list.
923934
*/
924935
private boolean isExcluded(String beanName) {
925-
return (this.excludedBeans != null &&
926-
(this.excludedBeans.contains(beanName) ||
927-
(beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX) &&
928-
this.excludedBeans.contains(beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length())))));
936+
return (this.excludedBeans.contains(beanName) ||
937+
(beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX) &&
938+
this.excludedBeans.contains(beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length()))));
929939
}
930940

931941
/**

spring-context/src/test/java/org/springframework/jmx/export/MBeanExporterTests.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.jmx.export;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collection;
2022
import java.util.Collections;
2123
import java.util.HashMap;
2224
import java.util.List;
@@ -643,6 +645,28 @@ public void testMBeanIsUnregisteredForRuntimeExceptionDuringInitialization() thr
643645
ObjectNameManager.getInstance(objectName2));
644646
}
645647

648+
@Test
649+
public void testIgnoreBeanName() throws MalformedObjectNameException {
650+
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
651+
String firstBeanName = "spring:type=TestBean";
652+
factory.registerSingleton(firstBeanName, new TestBean("test"));
653+
String secondBeanName = "spring:type=TestBean2";
654+
factory.registerSingleton(secondBeanName, new TestBean("test2"));
655+
656+
MBeanExporter exporter = new MBeanExporter();
657+
exporter.setServer(getServer());
658+
exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(firstBeanName, secondBeanName));
659+
exporter.setBeanFactory(factory);
660+
exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL);
661+
exporter.addExcludedBean(secondBeanName);
662+
663+
start(exporter);
664+
assertIsRegistered("Bean not autodetected in (AUTODETECT_ALL) mode",
665+
ObjectNameManager.getInstance(firstBeanName));
666+
assertIsNotRegistered("Bean should have been excluded",
667+
ObjectNameManager.getInstance(secondBeanName));
668+
}
669+
646670
private ConfigurableApplicationContext load(String context) {
647671
return new ClassPathXmlApplicationContext(context, getClass());
648672
}
@@ -763,15 +787,15 @@ public RuntimeExceptionThrowingConstructorBean() {
763787
private static final class NamedBeanAutodetectCapableMBeanInfoAssemblerStub extends
764788
SimpleReflectiveMBeanInfoAssembler implements AutodetectCapableMBeanInfoAssembler {
765789

766-
private String namedBean;
790+
private Collection<String> namedBeans;
767791

768-
public NamedBeanAutodetectCapableMBeanInfoAssemblerStub(String namedBean) {
769-
this.namedBean = namedBean;
792+
public NamedBeanAutodetectCapableMBeanInfoAssemblerStub(String... namedBeans) {
793+
this.namedBeans = Arrays.asList(namedBeans);
770794
}
771795

772796
@Override
773797
public boolean includeBean(Class<?> beanClass, String beanName) {
774-
return this.namedBean.equals(beanName);
798+
return this.namedBeans.contains(beanName);
775799
}
776800
}
777801

0 commit comments

Comments
 (0)