001/* 002 * Copyright 2002-2013 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.messaging.core; 018 019import org.springframework.beans.BeansException; 020import org.springframework.beans.factory.BeanFactory; 021import org.springframework.beans.factory.BeanFactoryAware; 022import org.springframework.messaging.MessageChannel; 023import org.springframework.util.Assert; 024 025/** 026 * An implementation of {@link DestinationResolver} that interprets a destination 027 * name as the bean name of a {@link MessageChannel} and looks up the bean in 028 * the configured {@link BeanFactory}. 029 * 030 * @author Mark Fisher 031 * @since 4.0 032 */ 033public class BeanFactoryMessageChannelDestinationResolver 034 implements DestinationResolver<MessageChannel>, BeanFactoryAware { 035 036 private BeanFactory beanFactory; 037 038 039 /** 040 * A default constructor that can be used when the resolver itself is configured 041 * as a Spring bean and will have the {@code BeanFactory} injected as a result 042 * of ing having implemented {@link BeanFactoryAware}. 043 */ 044 public BeanFactoryMessageChannelDestinationResolver() { 045 } 046 047 /** 048 * A constructor that accepts a {@link BeanFactory} useful if instantiating this 049 * resolver manually rather than having it defined as a Spring-managed bean. 050 * @param beanFactory the bean factory to perform lookups against 051 */ 052 public BeanFactoryMessageChannelDestinationResolver(BeanFactory beanFactory) { 053 Assert.notNull(beanFactory, "beanFactory must not be null"); 054 this.beanFactory = beanFactory; 055 } 056 057 058 @Override 059 public void setBeanFactory(BeanFactory beanFactory) { 060 this.beanFactory = beanFactory; 061 } 062 063 064 @Override 065 public MessageChannel resolveDestination(String name) { 066 Assert.state(this.beanFactory != null, "No BeanFactory configured"); 067 try { 068 return this.beanFactory.getBean(name, MessageChannel.class); 069 } 070 catch (BeansException ex) { 071 throw new DestinationResolutionException( 072 "Failed to find MessageChannel bean with name '" + name + "'", ex); 073 } 074 } 075 076}