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