KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > support > JdbcBeanDefinitionReader


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.core.support;
18
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import javax.sql.DataSource JavaDoc;
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 /**
32  * Bean definition reader that reads values from a database table,
33  * based on a given SQL statement.
34  *
35  * <p>Expects columns for bean name, property name and value as String.
36  * Formats for each are identical to the properties format recognized
37  * by PropertiesBeanDefinitionReader.
38  *
39  * <p><b>NOTE:</b> This is mainly intended as an example for a custom
40  * JDBC-based bean definition reader. It does not aim to offer
41  * comprehensive functionality.
42  *
43  * @author Rod Johnson
44  * @author Juergen Hoeller
45  * @see #loadBeanDefinitions
46  * @see org.springframework.beans.factory.support.PropertiesBeanDefinitionReader
47  */

48 public class JdbcBeanDefinitionReader {
49
50     private final PropertiesBeanDefinitionReader propReader;
51
52     private JdbcTemplate jdbcTemplate;
53
54
55     /**
56      * Create a new JdbcBeanDefinitionReader for the given bean factory,
57      * using a default PropertiesBeanDefinitionReader underneath.
58      * <p>DataSource or JdbcTemplate still need to be set.
59      * @see #setDataSource
60      * @see #setJdbcTemplate
61      */

62     public JdbcBeanDefinitionReader(BeanDefinitionRegistry beanFactory) {
63         this.propReader = new PropertiesBeanDefinitionReader(beanFactory);
64     }
65
66     /**
67      * Create a new JdbcBeanDefinitionReader that delegates to the
68      * given PropertiesBeanDefinitionReader underneath.
69      * <p>DataSource or JdbcTemplate still need to be set.
70      * @see #setDataSource
71      * @see #setJdbcTemplate
72      */

73     public JdbcBeanDefinitionReader(PropertiesBeanDefinitionReader beanDefinitionReader) {
74         Assert.notNull(beanDefinitionReader, "Bean definition reader must not be null");
75         this.propReader = beanDefinitionReader;
76     }
77
78
79     /**
80      * Set the DataSource to use to obtain database connections.
81      * Will implicitly create a new JdbcTemplate with the given DataSource.
82      */

83     public void setDataSource(DataSource JavaDoc dataSource) {
84         this.jdbcTemplate = new JdbcTemplate(dataSource);
85     }
86
87     /**
88      * Set the JdbcTemplate to be used by this bean factory.
89      * Contains settings for DataSource, SQLExceptionTranslator, NativeJdbcExtractor, etc.
90      */

91     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
92         Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null");
93         this.jdbcTemplate = jdbcTemplate;
94     }
95
96
97     /**
98      * Load bean definitions from the database via the given SQL string.
99      * @param sql SQL query to use for loading bean definitions.
100      * The first three columns must be bean name, property name and value.
101      * Any join and any other columns are permitted: e.g.
102      * <code>SELECT BEAN_NAME, PROPERTY, VALUE FROM CONFIG WHERE CONFIG.APP_ID = 1</code>
103      * It's also possible to perform a join. Column names are not significant --
104      * only the ordering of these first three columns.
105      */

106     public void loadBeanDefinitions(String JavaDoc sql) {
107         Assert.notNull(this.jdbcTemplate, "Not fully configured - specify DataSource or JdbcTemplate");
108         final Properties JavaDoc props = new Properties JavaDoc();
109         this.jdbcTemplate.query(sql, new RowCallbackHandler() {
110             public void processRow(ResultSet JavaDoc rs) throws SQLException JavaDoc {
111                 String JavaDoc beanName = rs.getString(1);
112                 String JavaDoc property = rs.getString(2);
113                 String JavaDoc value = rs.getString(3);
114                 // Make a properties entry by combining bean name and property.
115
props.setProperty(beanName + "." + property, value);
116             }
117         });
118         this.propReader.registerBeanDefinitions(props);
119     }
120
121 }
122
Popular Tags