KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > datasource > lookup > BeanFactoryDataSourceLookup


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.datasource.lookup;
18
19 import javax.sql.DataSource JavaDoc;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.BeanFactoryAware;
24 import org.springframework.util.Assert;
25
26 /**
27  * {@link DataSourceLookup} implementation based on a Spring {@link BeanFactory}.
28  *
29  * <p>Will lookup Spring managed beans identified by bean name,
30  * expecting them to be of type <code>javax.sql.DataSource</code>.
31  *
32  * @author Costin Leau
33  * @author Juergen Hoeller
34  * @since 2.0
35  * @see org.springframework.beans.factory.BeanFactory
36  */

37 public class BeanFactoryDataSourceLookup implements DataSourceLookup, BeanFactoryAware {
38
39     private BeanFactory beanFactory;
40
41
42     /**
43      * Create a new instance of the {@link BeanFactoryDataSourceLookup} class.
44      * <p>The BeanFactory to access must be set via <code>setBeanFactory</code>.
45      * @see #setBeanFactory
46      */

47     public BeanFactoryDataSourceLookup() {
48     }
49
50     /**
51      * Create a new instance of the {@link BeanFactoryDataSourceLookup} class.
52      * <p>Use of this constructor is redundant if this object is being created
53      * by a Spring IoC container, as the supplied {@link BeanFactory} will be
54      * replaced by the {@link BeanFactory} that creates it (c.f. the
55      * {@link BeanFactoryAware} contract). So only use this constructor if you
56      * are using this class outside the context of a Spring IoC container.
57      * @param beanFactory the bean factory to be used to lookup {@link DataSource DataSources}
58      */

59     public BeanFactoryDataSourceLookup(BeanFactory beanFactory) {
60         Assert.notNull(beanFactory, "BeanFactory is required");
61         this.beanFactory = beanFactory;
62     }
63
64
65     public void setBeanFactory(BeanFactory beanFactory) {
66         this.beanFactory = beanFactory;
67     }
68
69
70     public DataSource JavaDoc getDataSource(String JavaDoc dataSourceName) throws DataSourceLookupFailureException {
71         Assert.notNull(this.beanFactory, "BeanFactory is required");
72         try {
73             return (DataSource JavaDoc) this.beanFactory.getBean(dataSourceName, DataSource JavaDoc.class);
74         }
75         catch (BeansException ex) {
76             throw new DataSourceLookupFailureException(
77                     "Failed to look up DataSource bean with name '" + dataSourceName + "'", ex);
78         }
79     }
80
81 }
82
Popular Tags