jdk动态代理和cglib代理

cglib代理

package com.qst.spring.dyanmicProxy.cglib;

public class MyAspect {
      public void check(){

          System.out.println("check user.......");
      }
      public void log(){

          System.out.println("logging。。。。。。");
      }
}
package com.qst.spring.dyanmicProxy.cglib;

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

import java.lang.reflect.Method;

public class MyProxy<T> implements MethodInterceptor {

    private  T byProxy;
    private  MyAspect myAspect;

    public MyProxy(T byProxy, MyAspect myAspect) {
        this.byProxy = byProxy;
        this.myAspect = myAspect;
    }
    public T create(){
        //通过Enhancer创建代理类 ---Proxy
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(byProxy.getClass());
        enhancer.setCallback(this);

        T proxy = (T)enhancer.create();
        return proxy;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        // Method method 调用的被代理对象方法
        myAspect.check();
        method.invoke(byProxy,objects);
        myAspect.log();
        return null;
    }
}

package com.qst.spring.dyanmicProxy.cglib;

public class UserDaoImpl  {


    public void save(String name) {
        System.out.println("save user......"+name);
    }


    public void delete() {
        System.out.println("delete user.......");
    }
}

package com.qst.spring.dyanmicProxy.cglib;

public class UserService {
    public void save(){
        System.out.println("save ...");
    }
}

package com.qst.spring.dyanmicProxy.cglib;

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

public class Test {

    public static void main(String[] args) {
        //被代理对象
        UserDaoImpl userDaoImpl =new UserDaoImpl();
        UserService userService = new UserService();
        MyAspect myAspect = new MyAspect();

        MyProxy myProxy = new MyProxy(userService,myAspect);

        UserService service = (UserService) myProxy.create();

        service.save();
    }
}

jdk动态代理

package com.qst.spring.dyanmicProxy.jdk;

public class MyAspect {
      public void check(){
          System.out.println("check user.......");
      }
      public void log(){
          System.out.println("logging。。。。。。");
      }
}

package com.qst.spring.dyanmicProxy.jdk;

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

public class MyInvocationHandler implements InvocationHandler {

    private  UserDao userDao;
    private  MyAspect myAspect;

    public MyInvocationHandler(UserDao userDao,MyAspect myAspect) {
        this.userDao = userDao;
        this.myAspect= myAspect;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 //     method 就是调用的被代理对象的方法
        //String methodName =method.getName();
 //     Object 被代理对象
        myAspect.check();
        method.invoke(userDao,args);//userDaoImpl.save(参数)
        myAspect.log();
        //System.out.println("----------"+methodName);
        return null;
    }
}

package com.qst.spring.dyanmicProxy.jdk;

public interface UserDao {

    public void save(String name);
    public void delete(String name);
}

package com.qst.spring.dyanmicProxy.jdk;

import java.sql.SQLOutput;

public class UserDaoImpl implements UserDao{

    @Override
    public void save(String name) {
        System.out.println("save user......"+name);
    }

    @Override
    public void delete(String name) {
        System.out.println("delete user......."+name);
    }
}

package com.qst.spring.dyanmicProxy.jdk;

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

public class Test {
    public static void main(String[] args) {
        //被代理类
        UserDao userDao = new UserDaoImpl();
        //切面
        MyAspect myAspect = new MyAspect();
        //jdk动态代理是在运行时生成代理类
        //代理类是和被代理类实现相同的接口
//        ClassLoader loader, 被代理对象类加载器 UserDao.class-->虚拟机
          ClassLoader classLoader = userDao.getClass().getClassLoader();

//        Class<?>[] interfaces, 被代理对象的实现接口
         Class<?>[] interfaces =userDao.getClass().getInterfaces();
//        InvocationHandler h
        InvocationHandler handler = new MyInvocationHandler(userDao,myAspect);
        UserDao proxy = (UserDao) Proxy.newProxyInstance(classLoader,interfaces,handler);
        proxy.save("lp");
        proxy.delete("king");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值