cglib 使用

 

 

1.目标类

public class CglibDao {

    public CglibDao() {
        select();
    }

    public void update() {
        System.out.println("CglibDao.update()");
    }

    public void select() {
        System.out.println("CglibDao.select()");
    }
}

2.代理类1

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

import java.lang.reflect.Method;

public class CglibDaoProxy implements MethodInterceptor {

    public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("before method invoke");
        methodProxy.invokeSuper(object, args);
        System.out.println("after method invoke");
        return object;
    }
}

3.代理类2

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

import java.lang.reflect.Method;
import java.util.Calendar;

public class CglibDaoTwoProxy implements MethodInterceptor {
    public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("startTime : " + Calendar.getInstance().getTimeInMillis());
        methodProxy.invokeSuper(object, args);
        System.out.println("endTime : " + Calendar.getInstance().getTimeInMillis());
        return object;
    }
}

4.回调过滤器

import org.springframework.cglib.proxy.CallbackFilter;

import java.lang.reflect.Method;

public class CglibFilter implements CallbackFilter {
    public int accept(Method method) {
        if ("select".equals(method.getName())) {
            return 0;
        }
        return 1;
    }
}

5.Main

import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.NoOp;

public class Main {
    public static void main(String[] args) {
        cglibTest1();
        System.out.println();
        cglibTest2();
        System.out.println();
        cglibTest3();
    }

    public static void cglibTest1() {
        CglibDaoProxy proxy = new CglibDaoProxy();

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibDao.class);
        enhancer.setCallback(proxy);

        CglibDao dao = (CglibDao) enhancer.create();
        dao.update();
        dao.select();
    }

    public static void cglibTest2() {
        CglibDaoTwoProxy twoProxy = new CglibDaoTwoProxy();
        CglibDaoProxy proxy = new CglibDaoProxy();

        CglibFilter filter = new CglibFilter();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibDao.class);
        enhancer.setCallbacks(new Callback[]{proxy, twoProxy, NoOp.INSTANCE});
        enhancer.setCallbackFilter(filter);

        CglibDao dao = (CglibDao) enhancer.create();
        dao.update();
        dao.select();
    }

    public static void cglibTest3() {
        CglibDaoProxy proxy = new CglibDaoProxy();

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibDao.class);
        enhancer.setCallback(proxy);

        enhancer.setInterceptDuringConstruction(true);
        CglibDao dao = (CglibDao) enhancer.create();

        enhancer.setInterceptDuringConstruction(false);
        CglibDao filterConstructionDao = (CglibDao) enhancer.create();
    }
}

 

6.result

before method invoke
CglibDao.select()
after method invoke
before method invoke
CglibDao.update()
after method invoke
before method invoke
CglibDao.select()
after method invoke

before method invoke
CglibDao.select()
after method invoke
startTime : 1594098797700
CglibDao.update()
endTime : 1594098797709
before method invoke
CglibDao.select()
after method invoke

before method invoke
CglibDao.select()
after method invoke
CglibDao.select()

Process finished with exit code 0

 

你可能感兴趣的:(Java)