1. SpringAOP 动态代理实现的两种方式

package com.sinosoft.demo;

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;

/**
 * @ClassName: SpringAOPTest
 * @Description: Spring AOP 面向切面编程
 * @author: Created by xushuyi <a href="xu_shuyi1002@163.com">Contact author</a>
 * @date: 2019/7/17 9:29
 * @Version: V1.0
 */
public class SpringAOPTest {

    /**
     * 两种方式
     * 1. Java默认的jdk动态代理
     * 2. 第三方代码生成库的cglib动态代理
     */
}


/**
 * 测试Java 中默认的jdk动态代理
 */
class MyJdkProxy implements InvocationHandler {

    /**
     * 需要被代理的目标对象
     */
    private Object proxyObject;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("jdk动态代理,监听开始!");
        Object result = method.invoke(proxyObject, args);
        System.out.println("jdk动态代理,监听结束!");
        return result;
    }

    /**
     * 获取代理的实例对象
     *
     * @param targetObject
     * @return
     */
    private Object getProxyInstance(Object targetObject) {
        this.proxyObject = targetObject;
        return Proxy.newProxyInstance(
                proxyObject.getClass().getClassLoader(),
                proxyObject.getClass().getInterfaces(),
                this::invoke
        );
    }

    public static void main(String[] args) {
        MyJdkProxy myJdkProxy = new MyJdkProxy();
        UserService userService = (UserService) myJdkProxy.getProxyInstance(new UserServiceImpl());
        userService.addUser("徐书一");
    }

}

class UserServiceImpl implements UserService {
    @Override
    public void addUser(String userName) {
        System.out.println("调用了新增的方法,传入的参数为:" + userName);
    }
}

interface UserService {
    void addUser(String userName);
}


/**
 * 测试cglib动态代理
 */
class CglibProxy implements MethodInterceptor {

    /**
     * 被代理对象
     */
    private Object proxyObject;

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib动态代理,监听开始!");
        Object result = method.invoke(proxyObject, objects);
        System.out.println("cglib动态代理,监听结束!");
        return result;
    }

    /**
     * 获取代理的实例
     *
     * @param targetObject
     * @return
     */
    private Object getCglibProxyInstance(Object targetObject) {
        this.proxyObject = targetObject;
        Enhancer enhancer = new Enhancer();
        // 设置父类,因为cglib是针对指定的类派生出一个子类,所以需要指定父类
        enhancer.setSuperclass(targetObject.getClass());
        // 设置回调
        enhancer.setCallback(this);
        // 创建并返回代理对象所派生的子类对象
        return enhancer.create();
    }

    public static void main(String[] args) {
        CglibProxy cglibProxy = new CglibProxy();
        AgentServiceImpl agentService = (AgentServiceImpl) cglibProxy
                .getCglibProxyInstance(new AgentServiceImpl());
        agentService.addAgent("小苗");
    }
}

class AgentServiceImpl {

    public String addAgent(String name) {
        System.out.println("新增代理人:" + name);
        return name;
    }
}

 

posted @ 2019-07-17 10:12  xu_shuyi  阅读(367)  评论(0编辑  收藏  举报