apache.common.dbutils的BeanHandler的“缺陷”?

来源:互联网 发布:裁决网络 编辑:程序博客网 时间:2024/05/21 08:57

先看一下BUG信息:

异常:
 16:46:57.472 ERROR - DbUtilsExecutor-find:Cannot create com.keydak.gateguard.database.bean.gategd.GateController: com.keydak.gateguard.database.bean.gategd.GateController Query: SELECT * FROM t_gg_gate_controller WHERE controller_id = 
 



apache.common.dbutils利用BeanHandler<T>对JDBC返回的resultSet进行处理,从而获得bean.


BeanHandler<T>利用泛化所有类型的bean都支持处理,内部源码采用了


(该类的功能网上搜索一下可以会的官方的接口文档)

下面这一段是核心部分

/** * Factory method that returns a new instance of the given Class.  This * is called at the start of the bean creation process and may be * overridden to provide custom behavior like returning a cached bean * instance. * @param <T> The type of object to create * @param c The Class to create an object from. * @return A newly created object of the Class. * @throws SQLException if creation failed. */protected <T> T newInstance(Class<T> c) throws SQLException {    try {        return c.newInstance();    } catch (InstantiationException e) {        throw new SQLException(            "Cannot create " + c.getName() + ": " + e.getMessage());    } catch (IllegalAccessException e) {        throw new SQLException(            "Cannot create " + c.getName() + ": " + e.getMessage());    }}

可以看到整个泛化过程都依赖于反射技术,也就是说,Bean必须具备有被反射的条件。


我们看到了这一句

c.newInstance();


利用反射调用了无参构造函数,也就是说Bean必须要有无参构造函数。这里如果Bean没有声明无参构造函数就会创建Bean失败了。


解决问题的方法:

Dbutils要用源码包,单步调试可以发现问题。


优化Dbutils的想法:在异常信息进行详细描述。





0 0
原创粉丝点击