Spring中JdbcDaoSupport的DataSource注入问题

参考以下两篇文章:
[url]http://www.mkyong.com/spring/spring-jdbctemplate-jdbcdaosupport-examples/[/url]
[url]http://stackoverflow.com/questions/4762229/spring-ldap-invoking-setter-methods-in-beans-configuration[/url]

Spring JdbcDaoSupport 的使用非常简单,粗看没什么可疑的:

1.让Dao extends JdbcDaoSupport :

public class JdbcCustomerDAO extends JdbcDaoSupport implements CustomerDAO
{
//no need to set datasource here
public void insert(Customer customer){

String sql = "INSERT INTO CUSTOMER " +
"(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";

getJdbcTemplate().update(sql, new Object[] { customer.getCustId(),
customer.getName(),customer.getAge()
});

}

2.配置好数据源。这里是直接配置了,开发中可通过JNDI从Tomcat获取:
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">






3.引用数据源并注入:




但是,查看JdbcDaoSupport源码会发现,JdbcDaoSupport并没有dataSource这个字段,它唯一的字段是:
private JdbcTemplate jdbcTemplate;

那么dataSource是怎么注入到JdbcDaoSupport的呢?

原来,Spring注入时是根据property而不是field
查看Spring的源码就会发现,Spring先解析bean.xml并保存好property,然后通过反射调用property在类中对应的writeMethod(也就是set方法),把bean.xml中配置的值赋给bean;而不是反过来

例如你给customerDAO 配置了:

那么Spring就会到customerDAO 里面找setDataSource这个方法并调用,而不管有没有这个字段

JdbcDaoSupport的setDataSource方法:
public final void setDataSource(DataSource dataSource) {
if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
this.jdbcTemplate = createJdbcTemplate(dataSource);
initTemplateConfig();
}
}
protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}


在这里,是直接通过new来创建一个 JdbcTemplate
有了JdbcTemplate,数据库的操作就没问题了

说到Spring的DataSource,我用jadeclipse反编译查看某公司框架时,发现框架类似以下代码:
public abstract class JdbcBaseDao extends JdbcDaoSupport {
public void setDatasource(DataSource dataSource) {
setDataSource(dataSource);
}
}

在beans.xml里面配置依赖注入的时候,采用了auto-scan:
 



并没有像文章开头那样,显式地把DataSource注入到Dao当中,但框架运行正常
dataSource是怎么注入到Dao的呢?百思不得其解
后来用jd-gui.exe反编译时,才发现在setDatasource方法上面还有一个注解:
@Resource(name="dataSource")
水落石出。。

下面说说java bean中property的定义,


测试代码:

package com.ljn.spring;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;

public class PropertyTest {

public static void main(String[] args) throws IntrospectionException {
PropertyDescriptor[] descriptors =
Introspector.getBeanInfo(Dummy.class).getPropertyDescriptors();
//Introspector.getBeanInfo(Dummy.class, Object.class).getPropertyDescriptors();
for (PropertyDescriptor descriptor : descriptors) {
System.out.println(
"Property: " + descriptor.getName() +
", type: " + descriptor.getPropertyType());
}

Field[] fields = Dummy.class.getDeclaredFields();
for (Field field : fields) {
System.out.println(
"Field: " + field.getName() +
", type: " + field.getType());
}
}

}

class Dummy {

private String name;

//yes
public long getPropertyA() {
return 0L;
}

//yes
public void setPropertyB(int b) {
//...
}

//!!!yes
public int getPropertyC(int c) {
return 0;
}

//not a 'property', since 'j' is 'long' but the return-type is 'int'
public int getSeqid(long j) {
return 0;
}

//not a 'property', since it changes nothing
public void setSeqid() {
//...
}

//not a 'property', since it is 'private'
private int getSeqid() {
return 0;
}

//!!!yes, when "descriptors = Introspector.getBeanInfo(Dummy.class).getPropertyDescriptors();"
private static class InnerClassAsProperty{}
}

/*
以上程序输出:

Property: class, type: class java.lang.Class
Property: propertyA, type: long
Property: propertyB, type: int
Property: propertyC, type: null
Field: name, type: class java.lang.String

*/



由此可见,没有getter也没有setter的field并不认为是一个property
而符合标准的getter或者setter,则认为是一个property
符合标准的getter:形如“ getYzz(){...}”,则property为yzz,property的type就是方法返回值的type,注意方法不能有参数(如果有参数,则参数类型必须与返回类型一致)
符合标准的setter:形如“void setYzz( param){...}”property为yzz,property的type就是方法参数的type,注意方法必须有参数

要注意一点,对于静态内部类来说,如果
PropertyDescriptor[] descriptors = Introspector.getBeanInfo(Dummy.class).getPropertyDescriptors();
而不是
PropertyDescriptor[] descriptors = Introspector.getBeanInfo(Dummy.class, Object.class).getPropertyDescriptors();
那么静态内部类也会认为是一个“property”
为什么会这样?我也不明白
Stack Overflow有人给出这样的例子,排除'class':
for (PropertyDescriptor pd : Introspector.getBeanInfo(Foo.class).getPropertyDescriptors()) {
if (pd.getReadMethod() != null && !"class".equals(pd.getName()))
System.out.println(pd.getReadMethod().invoke(foo));
}

JavaBeansAPI specification的定义:
Basically properties are named attributes associated with a bean that can be read or written by calling appropriate methods on the bean
Properties are discrete, named attributes of a Java Bean that can affect its appearance or its behaviour.

也就是说,可以通过方法调用而获得(或改变)bean的状态或行为的,就称之为“property”,因此如果getter/setter声明为private,就不是“property”了

你可能感兴趣的:(Spring)