1 16 17 package org.springframework.jdbc.datasource.lookup; 18 19 import javax.sql.DataSource ; 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 37 public class BeanFactoryDataSourceLookup implements DataSourceLookup, BeanFactoryAware { 38 39 private BeanFactory beanFactory; 40 41 42 47 public BeanFactoryDataSourceLookup() { 48 } 49 50 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 getDataSource(String dataSourceName) throws DataSourceLookupFailureException { 71 Assert.notNull(this.beanFactory, "BeanFactory is required"); 72 try { 73 return (DataSource ) this.beanFactory.getBean(dataSourceName, DataSource .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 |