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