Java 8中的静态代理和动态代理的简单心得

欢迎来到我的第一个博客

个人学习的一点心得,第一次写写的不好见谅

简单做的图片看看就好

看看代码吧

代码1.

public interface CarSales {
    void sell();
}

代码2.

public class QQCar implements CarSales {
    @Override
    public void sell() {
        System.out.println("出售qq汽车排量1.0L");
    }
}

代码3.

public class Benz implements CarSales {
    @Override
    public void sell() {
        System.out.println("出售奔驰S级汽车和迈巴赫");
    }
}

代码4.

public interface Sales {
    void sell();
}

代码5.

public class Owner implements Sales {
    @Override
    public void sell() {
        System.out.println("我是房东,我正在出售我的公寓");
    }
}

代码6.

public class Owner2 implements Sales {
    @Override
    public void sell() {
        System.out.println("我想出售别墅");
    }
}

代码7.

public class SalesInvocationHandler implements InvocationHandler {

    private Object delegate;

    public Object bind(Object delegate){
        this.delegate=delegate;
        return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),delegate.getClass().getInterfaces(), this);
    }

//    public SalesInvocationHandler(Object delegate) {
//        this.delegate = delegate;
//    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(proxy.getClass().getInterfaces()[0]);
        System.out.println("中介1帮你检查信息");
        Object result = method.invoke(delegate, args);
        System.out.println("中介1向你收取中介费");

        return result;

    }
}

代码8.

public class SalesInvocationHandler2 implements InvocationHandler {

    private Object delegate;

    public Object bind(Object delegate){
        this.delegate=delegate;
        return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),delegate.getClass().getInterfaces(), this);
    }



    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(proxy.getClass().getInterfaces()[0]);
        System.out.println("中介2帮你检查房产信息");
        Object result = method.invoke(delegate, args);
        System.out.println("中介2帮你做卫生");
        return result;

    }
}

代码9.

public class SalesInvocationHandler3 implements InvocationHandler {
    private Object delegate;

    public Object bind(Object delegate){
        this.delegate=delegate;
        return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),delegate.getClass().getInterfaces(), this);
    }



    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(proxy.getClass().getInterfaces()[0]);
        System.out.println("中介3帮你买保险");
        Object result = method.invoke(delegate, args);
        System.out.println("中介3请你吃饭");
        return result;

    }
}

代码10.

public class Customer {
    public static void main(String[] args) {
        Sales delegate=new Owner();
//        InvocationHandler handler=new SalesInvocationHandler(delegate);
        Sales proxy= (Sales)new SalesInvocationHandler().bind(delegate);
        proxy.sell();

        Sales delegate2=new Owner2();
        Sales proxy2= (Sales)new SalesInvocationHandler().bind(delegate2);
        proxy2.sell();

        Sales proxy3= (Sales)new SalesInvocationHandler2().bind(delegate);
        proxy3.sell();

        Sales proxy4= (Sales)new SalesInvocationHandler2().bind(delegate2);
        proxy4.sell();

        CarSales car1=new Benz();
        CarSales proxy5= (CarSales) new SalesInvocationHandler().bind(car1);
        proxy5.sell();

        CarSales proxy6= (CarSales) new SalesInvocationHandler3().bind(car1);
        proxy6.sell();

        CarSales car2=new QQCar();
        CarSales proxy7= (CarSales) new SalesInvocationHandler3().bind(car2);
        proxy7.sell();


    }
}

运行结果如下:

中介1帮你检查信息
我是房东,我正在出售我的公寓
中介1向你收取中介费
interface dongtaigaijin.Sales
中介1帮你检查信息
我想出售别墅
中介1向你收取中介费
interface dongtaigaijin.Sales
中介2帮你检查房产信息
我是房东,我正在出售我的公寓
中介2帮你做卫生
interface dongtaigaijin.Sales
中介2帮你检查房产信息
我想出售别墅
中介2帮你做卫生
interface dongtaigaijin.CarSales
中介1帮你检查信息
出售奔驰S级汽车和迈巴赫
中介1向你收取中介费
interface dongtaigaijin.CarSales
中介3帮你买保险
出售奔驰S级汽车和迈巴赫
中介3请你吃饭
interface dongtaigaijin.CarSales
中介3帮你买保险
出售qq汽车排量1.0L
中介3请你吃饭
$-------------------------------------------------------------------------------------------------------------------------------
CGLIB动态代理

代码1.

package cglib_test;

/**
 * @description: 普通类,不用实现接口的
 * @create: 2018-10-18
 **/
public class My85 {
    public void sell(){
        System.out.println("卖面包的");
    }
}

代码2.

package cglib_test;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.logging.Logger;

/**
 * @description: ${description}
 * @create: 2018-10-18
 **/
public class MyMethodInterceptor implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Logger log1 = Logger.getLogger("log1");
        log1.info("这是台湾的面包店");
        System.out.println("这是台湾的面包店");
        return methodProxy.invokeSuper(o,objects);
    }
}

代码3.

package cglib_test;

import net.sf.cglib.proxy.Enhancer;
/**
 * @description: ${description}
 * @create: 2018-10-18
 **/
public class Test {
    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(My85.class);
        enhancer.setCallback(new MyMethodInterceptor());

        My85 my85 = (My85)enhancer.create();
        my85.sell();
    }
}

代码4 (2的改进版本)`.

package cglib_test;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @description: ${description}
 * @create: 2018-10-18
 **/
public class MyMethodInterceptor2 implements MethodInterceptor {
    private Object target;

    public MyMethodInterceptor2(Object target) {
        this.target = target;
    }

    public Object createProxy(){
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(this);
        enhancer.setSuperclass(this.target.getClass());
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("aaaaaaaaaaaaa");//切面a
        method.invoke(this.target,objects);
        System.out.println("bbbbbbbbbbbbb");//切面b
        return null;
    }
}

代码5(3的后续).

package cglib_test;

import net.sf.cglib.proxy.Enhancer;

/**
 * @description: ${description}
 * @create: 2018-10-18
 **/
public class Test {
    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(My85.class);
        enhancer.setCallback(new MyMethodInterceptor());
        My85 my85 = (My85) enhancer.create();
        my85.sell();
        System.out.println("楼上是JDK动态代理.楼下是CGLIB动态代理啊啊啊啊啊");
        My85 my86 = new My85();
        MyMethodInterceptor2 myMethodInterceptor2 = new MyMethodInterceptor2(my86);
        My85 proxy2 = (My85) myMethodInterceptor2.createProxy();
        proxy2.sell();
    }
}

控制台显示结果:

在这里插入图片描述

#-----------------------------------------------------------------------------------------------------------------------------------#

实际中的应用案例:

1/spring中的bean后处理器
2/Mybatis-Spring中使用动态代理
3/Spring动态代理 日志和事务,实现非业务逻辑和业务逻辑分开

动态代理分为两种:

1-jdk的动态代理
1.1 代理对象和目标对象实现了共同的接口
1.2拦截器必须实现InvocationHanlder接口
2-cglib的动态代理
2.1代理对象是目标对象的子类
2.2拦截器必须实现MethodInterceptor接口
2.3hibernate中session.load采用的是cglib实现的

参考文:https://blog.csdn.net/qq_27093465/article/details/53340513

部分材料取材自github
慢慢更新完善这个博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值