proxy动态代理

CglibProxy

package com.lv.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by lvyanghui
 * 2018/11/1 15:05
 */
public class CglibProxy implements MethodInterceptor{

    private Object obj;

    public Object newProxy(Object obj){

        this.obj = obj;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(obj.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }
    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

        System.out.println("CglibProxy前的操作");

        System.out.println("准备执行Method:" + method);

        Object result = method.invoke(obj, args);

        System.out.println("执行结束Method:" + method);

        System.out.println("CglibProxy后的操作");

        return result;
    }
}

JdkProxy

package com.lv.proxy;

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

/**
 * Created by lvyanghui
 * 2018/11/1 11:10
 */
public class JdkProxy implements InvocationHandler{

    private Object obj;

    public Object newProxy(Object obj){
        this.obj = obj;

        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("JdkProxy前的操作");

        System.out.println("准备执行Method:" + method);

        Object result = method.invoke(obj, args);

        System.out.println("执行结束Method:" + method);

        System.out.println("JdkProxy后的操作");

        return result;
    }
}

JdkInterfaceProxy

package com.lv.proxy;

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

/**
 * Created by lvyanghui
 * 2018/11/1 11:10
 */
public class JdkInterfaceProxy implements InvocationHandler{

    private Class<Subject> obj;

    public Object newProxy(Class<Subject> obj){
        this.obj = obj;

        return Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[]{obj}, this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("JdkInterfaceProxy前的操作");

        System.out.println("准备执行Method:" + method);

        //Object result = method.invoke(obj, args);

        System.out.println("执行结束Method:" + method);

        System.out.println("JdkInterfaceProxy后的操作");

        return null;
    }
}

Subject

package com.lv.proxy;

/**
 * Created by lvyanghui
 * 2018/11/1 11:08
 */
public interface Subject {

    public void rent();

    public void hello(String str);
}

RealSubject

package com.lv.proxy;

/**
 * Created by lvyanghui
 * 2018/11/1 11:09
 */
public class RealSubject implements Subject{
    @Override
    public void rent() {
        System.out.println("I want to rent my house");
    }

    @Override
    public void hello(String str) {

        System.out.println("hello: " + str);
    }
}

AopTest

package com.lv.proxy;

/**
 * Created by lvyanghui
 * 2018/11/1 11:12
 */
public class AopTest {

    public static void main(String[] args) {


        //jdk代理
        Subject realSubject = new RealSubject();

        JdkProxy jdkProxy = new JdkProxy();
        Subject jdkSubject = (Subject)jdkProxy.newProxy(realSubject);

        System.out.println("jdk aop开始");
        System.out.println(jdkSubject.getClass().getName());

        jdkSubject.rent();

        jdkSubject.hello("world");
        System.out.println("jdk aop结束");

        //jdk接口代理
        JdkInterfaceProxy jdkInterfaceProxy = new JdkInterfaceProxy();
        Subject jdkInterfaceSubject = (Subject)jdkInterfaceProxy.newProxy(Subject.class);

        System.out.println("jdk接口开始");
        System.out.println(jdkInterfaceSubject.getClass().getName());

        jdkInterfaceSubject.rent();

        jdkInterfaceSubject.hello("world");
        System.out.println("jdk接口结束");

        //cglib代理
        CglibProxy cglibProxy = new CglibProxy();
        Subject cglibSubject = (Subject)cglibProxy.newProxy(realSubject);

        System.out.println("cglib aop开始");
        System.out.println(cglibSubject.getClass().getName());

        cglibSubject.rent();

        cglibSubject.hello("world");
        System.out.println("cglib aop结束");

    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值