1. 程式人生 > >Cglib動態代理實現Superclass has no null constructors but no arguments were given錯誤處理

Cglib動態代理實現Superclass has no null constructors but no arguments were given錯誤處理

因為Spring通過Cglib生成代理類物件時,並沒有將目標物件的建構函式的引數及其型別進行設定,導致了Cglib在生成代理類物件時,會使用預設的建構函式生成,結果目標物件類沒有預設建構函式,Cglib生成子類時,也沒有加入預設建構函式,所以,異常的發生成為必然。 

解決方法:

1.目標物件,定義一個無引數建構函式,通過生產setter或者getter方法注入.

package cn.mesie.proxy;

public class CglibUserDao {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void demand(String demandName) {
        System.out.println(name + " implemented demand:" + demandName);
    }
}