2222import javax .management .Notification ;
2323import javax .management .ObjectName ;
2424
25+ import org .springframework .beans .factory .BeanFactory ;
2526import org .springframework .beans .factory .BeanFactoryAware ;
2627import org .springframework .beans .factory .BeanFactoryUtils ;
2728import org .springframework .beans .factory .InitializingBean ;
@@ -60,7 +61,6 @@ public class NotificationPublishingMessageHandler extends AbstractMessageHandler
6061
6162 private String defaultNotificationType ;
6263
63- @ Nullable
6464 private OutboundMessageMapper <Notification > notificationMapper ;
6565
6666
@@ -109,11 +109,12 @@ public String getComponentType() {
109109
110110 @ Override
111111 public final void onInit () {
112- Assert .isTrue (this .getBeanFactory () instanceof ListableBeanFactory , "A ListableBeanFactory is required." );
112+ BeanFactory beanFactory = getBeanFactory ();
113+ Assert .isTrue (beanFactory instanceof ListableBeanFactory , "A ListableBeanFactory is required." );
113114 Map <String , MBeanExporter > exporters =
114- BeanFactoryUtils .beansOfTypeIncludingAncestors ((ListableBeanFactory ) getBeanFactory () ,
115+ BeanFactoryUtils .beansOfTypeIncludingAncestors ((ListableBeanFactory ) beanFactory ,
115116 MBeanExporter .class );
116- Assert .isTrue (exporters .size () > 0 , "No MBeanExporter is available in the current context." );
117+ Assert .state (exporters .size () > 0 , "No MBeanExporter is available in the current context." );
117118 MBeanExporter exporter = null ;
118119 for (MBeanExporter exp : exporters .values ()) {
119120 exporter = exp ;
@@ -124,9 +125,11 @@ public final void onInit() {
124125 if (this .notificationMapper == null ) {
125126 this .notificationMapper = new DefaultNotificationMapper (this .objectName , this .defaultNotificationType );
126127 }
127- exporter .registerManagedResource (this .delegate , this .objectName );
128- if (this .logger .isInfoEnabled ()) {
129- this .logger .info ("Registered JMX notification publisher as MBean with ObjectName: " + this .objectName );
128+ if (exporter != null ) {
129+ exporter .registerManagedResource (this .delegate , this .objectName );
130+ if (this .logger .isInfoEnabled ()) {
131+ this .logger .info ("Registered JMX notification publisher as MBean with ObjectName: " + this .objectName );
132+ }
130133 }
131134 }
132135
0 commit comments