动态代理-使用方式

  • 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;
    }
}

转载于:https://my.oschina.net/u/3868736/blog/2218715

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值