java Cglib的应用

2018-01-13 11:15:40来源:http://jiandequn.iteye.com作者:jiandequn人点击

分享
java中简单使用CGLIB实现AOP的切面编程
package ppfuns.cglib;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;import java.lang.reflect.Method;/** * Created with IntelliJ IDEA. * Date: 2018/1/12 * Time: 11:11 * To change this template use File | Settings | File Templates. */public class EnhancerDemo {    public static void main(String[] args) {        Enhancer enhancer = new Enhancer();        enhancer.setSuperclass(EnhancerDemo.class);        enhancer.setCallback(new MethodInterceptorImpl());        EnhancerDemo demo = (EnhancerDemo) enhancer.create();        demo.test();        System.out.println(demo);    }    public void test(){        System.out.println("正在测试:"+EnhancerDemo.class.getName());    }}class MethodInterceptorImpl implements MethodInterceptor{    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {        System.out.println("before test....."+method.getName());        Object t =  methodProxy.invokeSuper(o,objects);        System.out.println("after test ...."+method.getName());        return t;    }}

结果:
before test.....test正在测试:ppfuns.cglib.EnhancerDemoafter test ....testbefore test.....toStringbefore test.....hashCodeafter test ....hashCodeafter test ....toStringppfuns.cglib.EnhancerDemo$$EnhancerByCGLIB$$fdf1bbc4@6ae40994Process finished with exit code 0

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台