gclib实现对 继承类 的增强

mac2024-11-16  5

package com.czh.GClib_Utils; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; import net.sf.cglib.proxy.Callback; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; /*gclib 实现类*/ public class gclib implements MethodInterceptor { private Class targe; //目标类 也可以 传对象 public Object newInstance (Class targe){ this.targe=targe; return Enhancer.create(targe , this); //this 设置回调函数 产生增强对象 } @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { methodProxy.invoke(targe.newInstance(), objects); //传入目标对象 System.out.println("chlibdaili "); return null; } }

测试类

package com.czh.GClib_Utils; import com.czh.domain.impl.userDaoimpl; import com.czh.domain.userDao; public class cglibDemo { public static void main(String[] args) { gclib gclib = new gclib(); userDaoimpl o = (userDaoimpl) gclib.newInstance(userDaoimpl.class); o.eat("S"); o.run(); } }
最新回复(0)