本文介绍: 它允许在运行生成代理类,无需事先定义代理类,从而在不修改原有代码的情况下对方法进行增强。动态代理的优势在于避免了手动创建大量代理类的繁琐工作,使代码更加简洁和易维护。我的理解是本身serviceserviceImpl结构,新增一个代理对象proxy代理对象去直接访问serviceImpl,在proxy进行事务的增强操作,所以代理对象实现接口需要实现InovacationHandler接口,并用反射调用invoke方法实现类似于泛型一样的效果

代码地址

https://github.com/cmdch2017/JDKproxy.git/

我的理解

我的理解是本身serviceserviceImpl结构,新增一个代理对象proxy,代理对象去直接访问serviceImpl,在proxy进行事务的增强操作,所以代理对象实现接口如何实现动态呢?需要实现InovacationHandler接口,并用反射调用invoke方法实现类似于泛型一样的效果

CHATGPT回答

“JDK动态代理是通过Proxy类和InvocationHandler接口实现的。它允许在运行时生成代理类,无需事先定义代理类,从而在不修改原有代码的情况下对方法进行增强。通过实现InvocationHandler接口,我们可以目标方法执行前后插入自定义逻辑比如事务处理。动态代理的优势在于避免了手动创建大量代理类的繁琐工作,使代码更加简洁和易维护。”

核心代码

客户端

public class TestStudent {
    public static void main(String[] args) {
//        testQuery(1);
        testQueryObject(1);
    }
//这里是动态代理,多实现了一个InvocationHandler
    private static void testQueryObject(int id) {
        DaoTransaction transaction=new DaoTransaction();
        StudentServiceImpl studentService=new StudentServiceImpl();
        TransactionHandler transactionHandler=new TransactionHandler(studentService,transaction);
        StudentService proxyInstance=(StudentService)Proxy.newProxyInstance(StudentServiceImpl.class.getClassLoader(),StudentServiceImpl.class.getInterfaces(),transactionHandler);
        Student student=proxyInstance.query(id);
        System.out.println("id:"+student.getId()+",name:"+student.getName());
    }
//这里静态代理
    private static void testQuery(int id) {
        DaoTransaction transaction=new DaoTransaction();
        StudentServiceImpl studentService=new StudentServiceImpl();
        ProxyStudent proxyStudent=new ProxyStudent(studentService,transaction);
        Student student=proxyStudent.query(id);
        System.out.println("id:"+student.getId()+",name:"+student.getName());
    }
}

动态代理学生

public class TransactionHandler implements InvocationHandler {
    private DaoTransaction daoTransaction;
    private Object object;

    public TransactionHandler(Object object, DaoTransaction daoTransaction) {
        this.object = object;
        this.daoTransaction = daoTransaction;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(object,args);
    }
}
@Data
public class Student {
    private int id;
    private String name;
}

静态代理学生

public class ProxyStudent implements StudentService {
    private StudentServiceImpl studentService;
    private DaoTransaction daoTransaction;

    public ProxyStudent(StudentServiceImpl studentService, DaoTransaction daoTransaction) {
        this.studentService = studentService;
        this.daoTransaction = daoTransaction;
    }

    @Override
    public Student query(int id) {
        daoTransaction.startTransaction();
        Student student=studentService.query(id);
        daoTransaction.endTransaction();
        return student;
    }
}
public class StudentServiceImpl implements StudentService {

    @Override
    public Student query(int id) {
        System.out.println("执行查询");
        Student student=new Student();
        student.setId(id);
        student.setName("lst");
        return student;
    }
}
public interface StudentService {
    Student query(int id);
}

public class DaoTransaction {
    public void startTransaction() {
        System.out.println("开启事务");
    }

    public void endTransaction() {
        System.out.println("关闭事务");
    }
}

原文地址:https://blog.csdn.net/weixin_43914278/article/details/134684091

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_12401.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注