Skip to content

Commit 1e58c80

Browse files
committed
MBeanExporter silently ignores null beans
Issue: SPR-15031 (cherry picked from commit 9c55d22)
1 parent a92ae4b commit 1e58c80

File tree

2 files changed

+109
-46
lines changed

2 files changed

+109
-46
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
* via the {@link #setListeners(MBeanExporterListener[]) listeners} property, allowing
8484
* application code to be notified of MBean registration and unregistration events.
8585
*
86-
* <p>This exporter is compatible with MBeans and MXBeans on Java 6 and above.
86+
* <p>This exporter is compatible with MBeans as well as MXBeans.
8787
*
8888
* @author Rob Harrop
8989
* @author Juergen Hoeller
@@ -466,7 +466,7 @@ public ObjectName registerManagedResource(Object managedResource) throws MBeanEx
466466
objectName = JmxUtils.appendIdentityToObjectName(objectName, managedResource);
467467
}
468468
}
469-
catch (Exception ex) {
469+
catch (Throwable ex) {
470470
throw new MBeanExportException("Unable to generate ObjectName for MBean [" + managedResource + "]", ex);
471471
}
472472
registerManagedResource(managedResource, objectName);
@@ -578,7 +578,8 @@ protected boolean isBeanDefinitionLazyInit(ListableBeanFactory beanFactory, Stri
578578
* @param mapValue the value configured for this bean in the beans map;
579579
* may be either the {@code String} name of a bean, or the bean itself
580580
* @param beanKey the key associated with this bean in the beans map
581-
* @return the {@code ObjectName} under which the resource was registered
581+
* @return the {@code ObjectName} under which the resource was registered,
582+
* or {@code null} if the actual resource was {@code null} as well
582583
* @throws MBeanExportException if the export failed
583584
* @see #setBeans
584585
* @see #registerBeanInstance
@@ -599,12 +600,14 @@ protected ObjectName registerBeanNameOrInstance(Object mapValue, String beanKey)
599600
}
600601
else {
601602
Object bean = this.beanFactory.getBean(beanName);
602-
ObjectName objectName = registerBeanInstance(bean, beanKey);
603-
replaceNotificationListenerBeanNameKeysIfNecessary(beanName, objectName);
604-
return objectName;
603+
if (bean != null) {
604+
ObjectName objectName = registerBeanInstance(bean, beanKey);
605+
replaceNotificationListenerBeanNameKeysIfNecessary(beanName, objectName);
606+
return objectName;
607+
}
605608
}
606609
}
607-
else {
610+
else if (mapValue != null) {
608611
// Plain bean instance -> register it directly.
609612
if (this.beanFactory != null) {
610613
Map<String, ?> beansOfSameType =
@@ -621,10 +624,11 @@ protected ObjectName registerBeanNameOrInstance(Object mapValue, String beanKey)
621624
return registerBeanInstance(mapValue, beanKey);
622625
}
623626
}
624-
catch (Exception ex) {
627+
catch (Throwable ex) {
625628
throw new UnableToRegisterMBeanException(
626629
"Unable to register MBean [" + mapValue + "] with key '" + beanKey + "'", ex);
627630
}
631+
return null;
628632
}
629633

630634
/**
@@ -816,7 +820,7 @@ protected ModelMBean createAndConfigureMBean(Object managedResource, String bean
816820
mbean.setManagedResource(managedResource, MR_TYPE_OBJECT_REFERENCE);
817821
return mbean;
818822
}
819-
catch (Exception ex) {
823+
catch (Throwable ex) {
820824
throw new MBeanExportException("Could not create ModelMBean for managed resource [" +
821825
managedResource + "] with key '" + beanKey + "'", ex);
822826
}
@@ -984,7 +988,7 @@ private void registerNotificationListeners() throws MBeanExportException {
984988
}
985989
}
986990
}
987-
catch (Exception ex) {
991+
catch (Throwable ex) {
988992
throw new MBeanExportException("Unable to register NotificationListener", ex);
989993
}
990994
}
@@ -1004,7 +1008,7 @@ private void unregisterNotificationListeners() {
10041008
this.server.removeNotificationListener(mappedObjectName, bean.getNotificationListener(),
10051009
bean.getNotificationFilter(), bean.getHandback());
10061010
}
1007-
catch (Exception ex) {
1011+
catch (Throwable ex) {
10081012
if (logger.isDebugEnabled()) {
10091013
logger.debug("Unable to unregister NotificationListener", ex);
10101014
}

0 commit comments

Comments
 (0)