Spring AOP的动态代理技术

AOP常用的动态代理技术

  • JDK 代理 : 基于接口的动态代理技术

  • cglib 代理:基于父类的动态代理技术

JDK的动态代理

1、目标类接口

package org.example.proxy.jdk;

public interface TargetInterface {
    public void save();
}

2、目标类

package org.example.proxy.jdk;

public class Target implements TargetInterface{

    @Override
    public void save() {
        System.out.println("saving......");
    }
}

3、增强类

package org.example.proxy.jdk;

public class Advice {

    public void before(){
        System.out.println("前置增强......");
    }

    public void after(){
        System.out.println("后置增强......");
    }
}

4、测试

package org.example.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

    public static void main(String[] args) {

        //目标对象
        final Target target = new Target();

        //增强对象
        final Advice advice = new Advice();

        //返回值 就是动态生成的代理对象(代理对象:增强后的对象)
        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(), //目标对象类加载器
                target.getClass().getInterfaces(), //目标对象相同的接口字节码对象数组

                // InvocationHandler:真实对象方法调用处理器,内置invoke方法,其功能:为真实对象定制代理逻辑
                new InvocationHandler() {
                    //调用代理对象的任何方法  实质执行的都是invoke方法
                    //参数1:代理对象
                    //参数2:真实对象的方法
                    //参数3:真实对象方法参数列表
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        advice.before(); //前置增强
                        Object invoke = method.invoke(target, args);//执行目标方法
                        advice.after(); //后置增强
                        return invoke;
                    }
                }
        );

        //调用代理对象的方法
        proxy.save();

    }

}

5、结果

前置增强......
saving......
后置增强......

cglib的动态代理

1、目标类

package org.example.proxy.cglib;

import org.example.proxy.jdk.TargetInterface;

public class Target{

    public void save() {
        System.out.println("saving......");
    }
}

2、增强类

package org.example.proxy.cglib;

public class Advice {

    public void before(){
        System.out.println("前置增强......");
    }

    public void after(){
        System.out.println("后置增强......");
    }
}

3、测试

package org.example.proxy.cglib;

import org.example.proxy.jdk.TargetInterface;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

    public static void main(String[] args) {

        // 目标对象
        final Target target = new Target();

        // 增强对象
        final Advice advice = new Advice();

        // ---基于cglib---

        // 1、创建增强器
        Enhancer enhancer = new Enhancer();
        // 2、设置父类(目标)
        enhancer.setSuperclass(Target.class);
        // 3、设置回调
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                advice.before();  // 执行前置增强
                Object invoke = method.invoke(target, args);  // 执行目标方法
                advice.after();  // 执行后置增强
                return invoke;
            }
        });
        // 4、创建代理对象
        Target proxy = (Target) enhancer.create();

        proxy.save();

    }

}

4、结果

前置增强......
saving......
后置增强......
posted @ 2022-08-10 23:57  Z_Y_L  阅读(15)  评论(0)    收藏  举报