码迷,mamicode.com
首页 > 编程语言 > 详细

spring中使用动态代理(AOP)

时间:2019-07-14 19:46:30      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:property   owa   proxy   多个   write   code   for   target   tor   

spring是整合了BGLIB和JDK两种动态代理

示例:使用CGLIB代理

public class MyCar {

    private String color = "blue";

    public void run() {
        System.out.println("我的汽车跑起来了" + color);
    }
}

测试

public class SpringProxy {

    public static void main(String[] args) {
       //将代理类的class文件保存到本地
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "E:\\Java4IDEA\\comm_test\\com\\sun\\proxy");
        ProxyFactory proxyFactory = new ProxyFactory(new MyCar());
        //添加前后置通知
        addAdivce(proxyFactory);
        proxyFactory.setProxyTargetClass(true);
        MyCar proxy = (MyCar) proxyFactory.getProxy();
        proxy.run();
    }
}

使用JDK代理

被代理的对象需要实现接口

public interface Car {
    void run();
}

调用

public static void main(String[] args) throws Exception {
    System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
    ProxyFactory proxyFactory = new ProxyFactory(new MyCar());
    addAdivce(proxyFactory);
    //        proxyFactory.setProxyTargetClass(true);
    Car proxy = (Car) proxyFactory.getProxy();
    proxy.run();
}

如果想添加前后置通知 如下

 private static void addAdivce(ProxyFactory proxyFactory) {
        proxyFactory.addAdvice(new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                Object aThis = invocation.getThis();
                System.out.println("MethodInterceptor前置:"+aThis);
                //执行被代理对象的方法,返回方法的返回值
                Object proceed = invocation.proceed();
                System.out.println("MethodInterceptor后置");
                return proceed;
            }
        });

        //可以添加多个方法前置或者后置通知
        proxyFactory.addAdvice(new AfterReturningAdvice() {
            @Override
            public void afterReturning(Object returnValue, Method method,
                                       Object[] args, Object target) throws Throwable {
                System.out.println("AfterReturningAdvice后置通知");

            }
        });

        proxyFactory.addAdvice(new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                System.out.println("MethodBeforeAdvice前置通知");
            }
        });
    }

JDK生成的动态类

public final class $Proxy0 extends Proxy implements Car, SpringProxy, Advised, DecoratingProxy {
    ...
    public final void run() throws  {
        try {
            super.h.invoke(this, m3, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }    
}

源码与JDK的代理和CGLB的代理源码大同小异,可以自行查阅

spring中使用动态代理(AOP)

标签:property   owa   proxy   多个   write   code   for   target   tor   

原文地址:https://www.cnblogs.com/qiaozhuangshi/p/11185099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!