[SPRING]setCallback(Callback) in the type Enhancer is not applicable for the arguments (CGLibProxy)


如下代码报错:

The method setCallback(Callback) in the type Enhancer is not applicable for the arguments (CGLibProxy)


package com.xxx.aopProxy;



import java.lang.reflect.Method;

//注意此处引入的头文件应该为springframework下的包, 否则会报上面的错误.

//遇到类似奇怪的错误, 请注意检查头文件的包含关系.

//import org.springframework.cglib.proxy.MethodInterceptor;

import org.aopalliance.intercept.MethodInterceptor;


import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodProxy;


public class CGLibProxy implements MethodInterceptor{
    private ProductDao productDao;
    public CGLibProxy(ProductDao productDao) {
        super();
        this.productDao = productDao;
    }  
    public ProductDao createProxy(){
        // 使用CGLIB生成代理:
        // 1.创建核心类:
        Enhancer enhancer = new Enhancer();
        // 2.为其设置父类:
        enhancer.setSuperclass(productDao.getClass());
        // 3.设置回调:
        enhancer.setCallback(this);
        // 4.创建代理:
        return (ProductDao) enhancer.create();
    }  
public Object intercept(Object proxy, Method method, Object[] args,MethodProxy methodProxy) throws Throwable {
        if("add".equals(method.getName())){
            System.out.println("日志记录==============");
            Object obj = methodProxy.invokeSuper(proxy, args);
            return obj;
        }
        return methodProxy.invokeSuper(proxy, args);
    }
}

猜你喜欢

转载自blog.csdn.net/hui85206/article/details/79165614
今日推荐