diff --git a/spring-messaging/src/main/java/org/springframework/messaging/core/BeanFactoryMessageChannelDestinationResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/core/BeanFactoryMessageChannelDestinationResolver.java index 8c924ade77..ad0e2b35b4 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/core/BeanFactoryMessageChannelDestinationResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/core/BeanFactoryMessageChannelDestinationResolver.java @@ -17,6 +17,7 @@ package org.springframework.messaging.core; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.messaging.MessageChannel; import org.springframework.util.Assert; @@ -25,11 +26,34 @@ import org.springframework.util.Assert; * @author Mark Fisher * @since 4.0 */ -public class BeanFactoryMessageChannelDestinationResolver implements DestinationResolver { +public class BeanFactoryMessageChannelDestinationResolver implements DestinationResolver, BeanFactoryAware { - private final BeanFactory beanFactory; + private volatile BeanFactory beanFactory; + /** + * Create a new instance of the {@link + * BeanFactoryMessageChannelDestinationResolver} class. + *

The BeanFactory to access must be set via setBeanFactory. + * This will happen automatically if this resolver is defined within an + * ApplicationContext thereby receiving the callback upon initialization. + * + * @see #setBeanFactory + */ + public BeanFactoryMessageChannelDestinationResolver() { + } + + /** + * Create a new instance of the {@link + * BeanFactoryMessageChannelDestinationResolver} class. + *

Use of this constructor is redundant if this object is being created + * by a Spring IoC container as the supplied {@link BeanFactory} will be + * replaced by the {@link BeanFactory} that creates it (c.f. the + * {@link BeanFactoryAware} contract). So only use this constructor if you + * are instantiating this object explicitly rather than defining a bean. + * + * @param beanFactory the bean factory to be used to lookup {@link MessageChannel}s. + */ public BeanFactoryMessageChannelDestinationResolver(BeanFactory beanFactory) { Assert.notNull(beanFactory, "beanFactory must not be null"); this.beanFactory = beanFactory; @@ -48,4 +72,9 @@ public class BeanFactoryMessageChannelDestinationResolver implements Destination } } + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } + }