KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > namedparam > BeanPropertySqlParameterSource


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.namedparam;
18
19 import org.springframework.beans.BeanWrapper;
20 import org.springframework.beans.BeanWrapperImpl;
21 import org.springframework.beans.NotReadablePropertyException;
22
23 /**
24  * SqlParameterSource implementation that obtains parameter values
25  * from bean properties of a given JavaBean object. The names of
26  * the bean properties have to match the parameter names.
27  *
28  * <p>Uses a Spring BeanWrapper for bean property access underneath.
29  *
30  * @author Thomas Risberg
31  * @author Juergen Hoeller
32  * @since 2.0
33  * @see NamedParameterJdbcTemplate
34  * @see org.springframework.beans.BeanWrapper
35  */

36 public class BeanPropertySqlParameterSource extends AbstractSqlParameterSource {
37
38     private final BeanWrapper beanWrapper;
39
40
41     /**
42      * Create a new BeanPropertySqlParameterSource for the given bean.
43      * @param object the bean instance to wrap
44      */

45     public BeanPropertySqlParameterSource(Object JavaDoc object) {
46         this.beanWrapper = new BeanWrapperImpl(object);
47     }
48
49
50     public boolean hasValue(String JavaDoc paramName) {
51         return this.beanWrapper.isReadableProperty(paramName);
52     }
53
54     public Object JavaDoc getValue(String JavaDoc paramName) throws IllegalArgumentException JavaDoc {
55         try {
56             return this.beanWrapper.getPropertyValue(paramName);
57         }
58         catch (NotReadablePropertyException ex) {
59             throw new IllegalArgumentException JavaDoc(ex.getMessage());
60         }
61     }
62
63 }
64
Popular Tags