001/* 002 * Copyright 2002-2018 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.jms.support.destination; 018 019import javax.jms.Destination; 020import javax.jms.JMSException; 021import javax.jms.Session; 022 023import org.springframework.beans.BeansException; 024import org.springframework.beans.factory.BeanFactory; 025import org.springframework.beans.factory.BeanFactoryAware; 026import org.springframework.lang.Nullable; 027import org.springframework.util.Assert; 028 029/** 030 * {@link DestinationResolver} implementation based on a Spring {@link BeanFactory}. 031 * 032 * <p>Will lookup Spring managed beans identified by bean name, 033 * expecting them to be of type {@code javax.jms.Destination}. 034 * 035 * @author Juergen Hoeller 036 * @since 2.5 037 * @see org.springframework.beans.factory.BeanFactory 038 */ 039public class BeanFactoryDestinationResolver implements DestinationResolver, BeanFactoryAware { 040 041 @Nullable 042 private BeanFactory beanFactory; 043 044 045 /** 046 * Create a new instance of the {@link BeanFactoryDestinationResolver} class. 047 * <p>The BeanFactory to access must be set via {@code setBeanFactory}. 048 * @see #setBeanFactory 049 */ 050 public BeanFactoryDestinationResolver() { 051 } 052 053 /** 054 * Create a new instance of the {@link BeanFactoryDestinationResolver} class. 055 * <p>Use of this constructor is redundant if this object is being created 056 * by a Spring IoC container, as the supplied {@link BeanFactory} will be 057 * replaced by the {@link BeanFactory} that creates it (c.f. the 058 * {@link BeanFactoryAware} contract). So only use this constructor if you 059 * are using this class outside the context of a Spring IoC container. 060 * @param beanFactory the bean factory to be used to lookup {@link javax.jms.Destination Destination} 061 */ 062 public BeanFactoryDestinationResolver(BeanFactory beanFactory) { 063 Assert.notNull(beanFactory, "BeanFactory is required"); 064 this.beanFactory = beanFactory; 065 } 066 067 068 @Override 069 public void setBeanFactory(BeanFactory beanFactory) { 070 this.beanFactory = beanFactory; 071 } 072 073 074 @Override 075 public Destination resolveDestinationName(@Nullable Session session, String destinationName, boolean pubSubDomain) 076 throws JMSException { 077 078 Assert.state(this.beanFactory != null, "BeanFactory is required"); 079 try { 080 return this.beanFactory.getBean(destinationName, Destination.class); 081 } 082 catch (BeansException ex) { 083 throw new DestinationResolutionException( 084 "Failed to look up Destination bean with name '" + destinationName + "'", ex); 085 } 086 } 087 088}