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

JAVA基础_动态代理的基本API

时间:2018-12-15 00:18:15      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:als   out   junit   over   使用   void   interface   jdk动态代理   +++   

  • JDK动态代理 
    • 代理类中使用的方法需要声明在接口中
    • 需要得到目标类的对象
  • Cglib包中的动态代理
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.Test;

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

/**
 * 动态代理
 *
 * @author zmh
 */
public class DynamicProxyTest {

    /**
     * JDK自带动态代理
     */
    @Test
    public void test01() {
        UserService userService = new UserServiceImpl();
        InvocationHandler invocationHandler = new MyInvocationHandler(userService);
        UserService userServiceProxy = (UserService) Proxy.newProxyInstance(
                userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), invocationHandler);
        System.out.println(userServiceProxy.method01(1));
        System.out.println(userServiceProxy.method02(2));
//        System.out.println(userServiceProxy.method03(2));
    }

    /**
     * Cglib动态代理
     */
    @Test
    public void test02() {
        CglibProxy cglibProxy = new CglibProxy();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(UserServiceImpl.class);
        enhancer.setCallback(cglibProxy);
        UserService userService = (UserService) enhancer.create();
        userService.method01(1);
        userService.method02(2);
    }


}

/**
 * Cglib动态代理
 */
class CglibProxy implements MethodInterceptor {

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("++++++before " + methodProxy.getSuperName() + "++++++");
        System.out.println(method.getName());
        Object result = methodProxy.invokeSuper(obj, args);
        System.out.println("++++++before " + methodProxy.getSuperName() + "++++++");
        return result;
    }
}

/**
 * JDK动态代理
 */
class MyInvocationHandler implements InvocationHandler {
    private Object target;

    MyInvocationHandler() {
        super();
    }

    MyInvocationHandler(Object target) {
        super();
        this.target = target;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] args) throws Throwable {
        if ("getName".equals(method.getName())) {
            System.out.println("++++++before " + method.getName() + "++++++");
            Object result = method.invoke(target, args);
            System.out.println("++++++after " + method.getName() + "++++++");
            return result;
        } else {
            Object result = method.invoke(target, args);
            return result;
        }
    }

}

/**
 * 接口
 */
interface UserService {
    String method01(int id);

    Integer method02(int id);
}

interface AdminService {
    String method03(int id);

    Integer method04(int id);
}


/**
 * 实现类
 */
class UserServiceImpl implements UserService, AdminService {

    @Override
    public String method01(int id) {
        System.out.println("------method01------");
        return "method01-" + id;

    }

    @Override
    public Integer method02(int id) {
        System.out.println("------method02------");
        return id;
    }

    @Override
    public String method03(int id) {
        System.out.println("------method03------");
        return "method03-" + id;
    }

    @Override
    public Integer method04(int id) {
        System.out.println("------method04------");
        return id;
    }
}

JAVA基础_动态代理的基本API

标签:als   out   junit   over   使用   void   interface   jdk动态代理   +++   

原文地址:https://www.cnblogs.com/houhou87/p/10122185.html

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