Spring aop之动态代理是个啥?

4 篇文章 0 订阅

Spring aop之动态代理

准备

一个生产者

package cn.doubly.proxy;

/**
 * 一个生产者
 */
public class Producer implements IProducer {

    /**
     * 销售产品
     * @param money
     */
    public void saleProduct(float money){
        System.out.println("销售产品,拿到了"+money);
    }

    /**
     * 售后
     * @param money
     */
    public void afterService(float money){
        System.out.println("产品售后,并拿到钱"+money);
    }

}

一个消费者

package cn.doubly.proxy;

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

/**
 * 消费者
 */
public class Client {
    public static void main(String[] args) {
        Producer producer = new Producer();
        producer.saleProduct(10000);
    }
}

以上就构成了简单的消费者与生产者之间的关系。代理,则可以为生产者售卖产品给消费者。以下是代理的特征:

特点:字节码随用随创建,随用随加载

作用:不修改源码的基础上对方法增强

分类

    基于接口的动态代理

    基于子类的动态代理

Proxy代理-基于接口的动态代理

/**
 * 消费者
 */
public class Client {
    public static void main(String[] args) {
        final Producer producer = new Producer();

        IProducer proxyProducer = (IProducer)Proxy.newProxyInstance(producer.getClass().getClassLoader(), producer.getClass().getInterfaces(), new InvocationHandler() {
            /**
             * 作用:执行被代理对象的任何接口方法都会经过该方法
             * @param proxy 代理对象的引用,一般不用
             * @param method 当前执行的方法
             * @param args 当前执行方法所需的参数
             * @return 被代理对象有相同的返回值
             * @throws Throwable
             */
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                Object returnObj = null;

                //提供增强方法
                if ("saleProduct".equals(method.getName())) {
                    float money = (Float) args[0];
                    System.out.println("抽成20%:" + money * 0.2f);
                    returnObj = method.invoke(producer, money * 0.8f);
                }

                return returnObj;
            }
        });

        proxyProducer.saleProduct(10000);
    }

}
Connected to the target VM, address: '127.0.0.1:33106', transport: 'socket'
抽成20%:2000.0
销售产品,拿到了8000.0
Disconnected from the target VM, address: '127.0.0.1:33106', transport: 'socket'

Process finished with exit code 0

问题:如果类没有实现任何接口,这种代理无法使用

Enhancer代理-基于子类的动态代理

不能是final修饰的最终类,因为无法创建子类

引入依赖

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.1_3</version>
</dependency>
public class Client {
    public static void main(String[] args) {
        final Producer producer = new Producer();

        Producer cglibProducer = (Producer)Enhancer.create(producer.getClass(), new MethodInterceptor() {
            public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                Object res = null;
                if("saleProduct".equals(method.getName())){
                    float money = (Float)args[0];
                    System.out.println("抽成20%:"+money*0.2);
                    res = method.invoke(producer,money * 0.8f);
                }
                return res;
            }
        });

        cglibProducer.saleProduct(10000);
    }

}

应用

  • 连接池的Close方法,把连接放回连接池

  • 解决全栈中文乱码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值