SpringBoot高手之路jdk动态代理

作者 : admin 本文共2440个字,预计阅读时间需要7分钟 发布时间: 2024-06-10 共2人阅读

文章目录

  • JDK动态代理
  • 基于jdk的动态代理
  • Aop底层就是基于动态代理实现的
  • 实现代码
    • 先写代理对象工具

JDK动态代理

SpringBoot高手之路jdk动态代理插图

基于jdk的动态代理

业务需求 通过动态代理技术,对service层的方法统计执行时间–创建代理对象

Aop底层就是基于动态代理实现的

jdk动态代理技术是基于接口做的代理

没有接口的类不能做动态代理

代理对象与目标对象是兄弟关系

他们都实现的是同一个接口

实现代码

先写代理对象工具

首先分析代理对象工具类的作用就是用于生成代理对象的,那么需要代理对象就需要先有一个原始对象

通过原始对象创建出代理对象,因为需要根据原始对象的字节码找到代理对象
还有接口

所以生成代理对象就需要将初始对象传入进去

并且实现代理对象需要 是需要接口

如果初始对象没有接口那么就不能实现代理

那么先写接口

写service层的接口与实现类

package com.itheima.service;

/**
 * @author healer
 * @Description DeptService
 * @data 2024-06-08 14:42
 */
public interface DeptService {


    void list();

    void delete();

    void update();

    void insert();
}

package com.itheima.service.impl;

import com.itheima.service.DeptService;

/**
 * @author healer
 * @Description DeptServiceImpl
 * @data 2024-06-08 14:42
 */
public class DeptServiceImpl implements DeptService {


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

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

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

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

package com.itheima.jdkProxy;

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

/**
 * @author healer
 * @Description jdkCreateUtil
 * @data 2024-06-08 14:44
 */
public class JdkProxyUtil {

    public static Object createProxy(Object targetClass) {
        return Proxy.newProxyInstance(targetClass.getClass().getClassLoader(), targetClass.getClass().getInterfaces(), new InvocationHandler() {
            
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                long start = System.currentTimeMillis();
                Object result = method.invoke(targetClass, args);
                long end = System.currentTimeMillis();

                System.out.println("-------->方法耗时:" + (end - start) + "ms");
                return result;
            }
        });
    }
}

package com.itheima.jdkProxy;

import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServiceImpl;

/**

  • @author healer

  • @Description JdkProxyDemo

  • @data 2024-06-08 14:48
    */
    public class JdkProxyDemo {
    public static void main(String[] args) {
    DeptService deptService = new DeptServiceImpl();

     DeptService proxyDeptService = (DeptService) JdkProxyUtil.createProxy(deptService);
     proxyDeptService.update();
    

    }

}

package com.itheima.jdkProxy;

import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServiceImpl;

/**
 * @author healer
 * @Description JdkProxyDemo
 * @data 2024-06-08 14:48
 */
public class JdkProxyDemo {
    public static void main(String[] args) {
        DeptService deptService = new DeptServiceImpl();

        DeptService proxyDeptService = (DeptService) JdkProxyUtil.createProxy(deptService);
        proxyDeptService.update();

    }

}

本站无任何商业行为
个人在线分享 » SpringBoot高手之路jdk动态代理
E-->