1 16 17 package org.springframework.jdbc.core.support; 18 19 import java.sql.ResultSet ; 20 import java.sql.SQLException ; 21 import java.util.Properties ; 22 23 import javax.sql.DataSource ; 24 25 import org.springframework.beans.factory.support.BeanDefinitionRegistry; 26 import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader; 27 import org.springframework.jdbc.core.JdbcTemplate; 28 import org.springframework.jdbc.core.RowCallbackHandler; 29 import org.springframework.util.Assert; 30 31 48 public class JdbcBeanDefinitionReader { 49 50 private final PropertiesBeanDefinitionReader propReader; 51 52 private JdbcTemplate jdbcTemplate; 53 54 55 62 public JdbcBeanDefinitionReader(BeanDefinitionRegistry beanFactory) { 63 this.propReader = new PropertiesBeanDefinitionReader(beanFactory); 64 } 65 66 73 public JdbcBeanDefinitionReader(PropertiesBeanDefinitionReader beanDefinitionReader) { 74 Assert.notNull(beanDefinitionReader, "Bean definition reader must not be null"); 75 this.propReader = beanDefinitionReader; 76 } 77 78 79 83 public void setDataSource(DataSource dataSource) { 84 this.jdbcTemplate = new JdbcTemplate(dataSource); 85 } 86 87 91 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 92 Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null"); 93 this.jdbcTemplate = jdbcTemplate; 94 } 95 96 97 106 public void loadBeanDefinitions(String sql) { 107 Assert.notNull(this.jdbcTemplate, "Not fully configured - specify DataSource or JdbcTemplate"); 108 final Properties props = new Properties (); 109 this.jdbcTemplate.query(sql, new RowCallbackHandler() { 110 public void processRow(ResultSet rs) throws SQLException { 111 String beanName = rs.getString(1); 112 String property = rs.getString(2); 113 String value = rs.getString(3); 114 props.setProperty(beanName + "." + property, value); 116 } 117 }); 118 this.propReader.registerBeanDefinitions(props); 119 } 120 121 } 122 | Popular Tags |