Cglib代理 Posted on 2021-03-16 | In java基础 1234567891011121314151617181920212223242526272829303132333435//被代理类public class PersonService { public void eat(){ System.out.println("吃饭"); }}//代理类public class Cglib implements MethodInterceptor{ private Enhancer enhancer = new Enhancer(); public Object getProxy(Class clazz){ enhancer.setSuperclass(clazz); enhancer.setCallback(this); return enhancer.create(); } @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("拿筷子" + method); Object invoke = methodProxy.invokeSuper(o, objects); System.out.println("洗碗" + method); return invoke; }}//mainpublic class Main { public static void main(String[] args) { Cglib ct = new Cglib(); PersonService proxy = (PersonService) ct.getProxy(PersonService.class); proxy.eat(); }} Donate comment here Donate WeChat Pay