• aop-动态代理,cglib动态代理,面向切面编程,aop的实现方法


    第三章 aop

    1.动态代理

    实现方式:jdk动态代理,使用jdk中的Proxy,Method,InvocaitonHanderl创建代理对象。
    jdk动态代理要求目标类必须实现接口

    案例结构如下:

    创建需要被代理的对象和其实现方法

    1. /**
    2. 需要被代理的
    3. */
    4. package com.bjpowernode.service;
    5. public interface SomeService {
    6. void doSome();
    7. void doOther();
    8. }
    9. /**
    10. 实现
    11. */
    12. package com.bjpowernode.service.impl;
    13. import com.bjpowernode.service.SomeService;
    14. import com.bjpowernode.util.ServiceTools;
    15. import java.util.Date;
    16. // service类的代码不修改,也能够增加 输出时间, 事务。
    17. public class SomeServiceImpl implements SomeService {
    18. @Override
    19. public void doSome() {
    20. System.out.println("执行业务方法doSome");
    21. int res = 10 + 20;
    22. //更新购买商品的库存, 生成订单, 结算整个购买商品的总价。
    23. }
    24. @Override
    25. public void doOther() {
    26. System.out.println("执行业务方法doOther");
    27. }
    28. }

    创建代理对象

    1. package com.bjpowernode.handler;
    2. import com.bjpowernode.util.ServiceTools;
    3. import java.lang.reflect.InvocationHandler;
    4. import java.lang.reflect.Method;
    5. public class MyIncationHandler implements InvocationHandler {
    6. //目标对象
    7. private Object target; //SomeServiceImpl类
    8. public MyIncationHandler(Object target) {
    9. this.target = target;
    10. }
    11. @Override
    12. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    13. //通过代理对象执行方法时,会调用执行这个invoke()
    14. System.out.println("执行MyIncationHandler中的invoke()");
    15. System.out.println("method名称:"+method.getName());
    16. String methodName = method.getName();
    17. Object res = null;
    18. if("doSome".equals(methodName)){ //JoinPoint Pointcut
    19. ServiceTools.doLog(); //在目标方法之前,输出时间
    20. //执行目标类的方法,通过Method类实现
    21. res = method.invoke(target,args); //SomeServiceImpl.doSome()
    22. ServiceTools.doTrans(); //在目标方法执行之后,提交事务
    23. } else {
    24. res = method.invoke(target,args); //SomeServiceImpl.doOther()
    25. }
    26. //目标方法的执行结果
    27. return res;
    28. }
    29. }

    使用代理对象

    1. package com.bjpowernode;
    2. import com.bjpowernode.handler.MyIncationHandler;
    3. import com.bjpowernode.service.SomeService;
    4. import com.bjpowernode.service.impl.SomeServiceImpl;
    5. import java.lang.reflect.InvocationHandler;
    6. import java.lang.reflect.Proxy;
    7. public class MyApp {
    8. public static void main(String[] args) {
    9. //调用doSome, doOther
    10. // SomeService service = new SomeServiceImpl();
    11. // service.doSome();
    12. // System.out.println("============================================");
    13. // service.doOther();
    14. //使用jdk的Proxy创建代理对象
    15. //创建目标对象
    16. SomeService target = new SomeServiceImpl();
    17. //创建InvocationHandler对象
    18. InvocationHandler handler = new MyIncationHandler(target);
    19. //使用Proxy创建代理
    20. SomeService proxy = (SomeService) Proxy.newProxyInstance(
    21. target.getClass().getClassLoader(),
    22. target.getClass().getInterfaces(),
    23. handler);
    24. //com.sun.proxy.$Proxy0
    25. System.out.println("proxy======"+proxy.getClass().getName());
    26. //通过代理执行方法,会调用handler中的invoke()
    27. proxy.doSome();
    28. System.out.println("==================================================");
    29. proxy.doOther();
    30. }
    31. }
    2. cglib动态代理:

    第三方的工具库,创建代理对象,原理是继承。 通过继承目标类,创建子类。
    子类就是代理对象。 要求目标类不能是final的, 方法也不能是final的

    .动态代理的作用:
    1)在目标类源代码不改变的情况下,增加功能。
    2)减少代码的重复
    3)专注业务逻辑代码
    4)解耦合,让你的业务功能和日志,事务非业务功能分离。

    3.Aop:面向切面编程, 基于动态代理的,可以使用jdk,cglib两种代理方式。

    Aop就是动态代理的规范化, 把动态代理的实现步骤,方式都定义好了,
    让开发人员用一种统一的方式,使用动态代理。

    4. AOP(Aspect Orient Programming)面向切面编程

    Aspect: 切面,给你的目标类增加的功能,就是切面。 像上面用的日志,事务都是切面。
    切面的特点: 一般都是非业务方法,独立使用的。
    Orient:面向, 对着。
    Programming:编程

    oop: 面向对象编程

    怎么理解面向切面编程 ?
    1)需要在分析项目功能时,找出切面。
    2)合理的安排切面的执行时间(在目标方法前, 还是目标方法后)
    3)合理的安全切面执行的位置,在哪个类,哪个方法增加增强功能

    术语:
    1)Aspect:切面,表示增强的功能, 就是一堆代码,完成某个一个功能。非业务功能,
    常见的切面功能有日志, 事务, 统计信息, 参数检查, 权限验证。

    2)JoinPoint:连接点 ,连接业务方法和切面的位置。 就某类中的业务方法
    3)Pointcut : 切入点 ,指多个连接点方法的集合。多个方法
    4)目标对象: 给哪个类的方法增加功能, 这个类就是目标对象
    5)Advice:通知,通知表示切面功能执行的时间。

    1. 说一个切面有三个关键的要素:
    2. 1)切面的功能代码,切面干什么
    3. 2)切面的执行位置,使用Pointcut表示切面执行的位置
    4. 3)切面的执行时间,使用Advice表示时间,在目标方法之前,还是目标方法之后。
    5.aop的实现

    ​ aop是一个规范,是动态的一个规范化,一个标准
    ​ aop的技术实现框架:
    1. spring:spring在内部实现了aop规范,能做aop的工作。
    spring主要在事务处理时使用aop。
    我们项目开发中很少使用spring的aop实现。 因为spring的aop比较笨重。

    1. aspectJ: 一个开源的专门做aop的框架。spring框架中集成了aspectj框架,通过spring就能使用aspectj的功能。
      aspectJ框架实现aop有两种方式:
      1.使用xml的配置文件 : 配置全局事务
      2.使用注解,我们在项目中要做aop功能,一般都使用注解, aspectj有5个注解。
  • 相关阅读:
    【生日快乐】SpringBoot SpringBoot 提高篇(第二篇) 第5章 SpringBoot 日志 5.1 日志介绍 & 5.2 日志框架
    分享微信使用技巧,快来涨姿势啦
    java版工程管理系统Spring Cloud+Spring Boot+Mybatis实现工程管理系统源码
    CentOS 8 的安装(清华大学开源软件镜像站、阿里云镜像、网易镜像下的安装步骤)
    论文回顾:Unmixing-Based Soft Color Segmentation for Image Manipulation
    C++:继承、模板、CRTP:谈谈C++多态设计模式(二)
    【再探】设计模式—备忘录模式与解释器模式
    MySQL数据库——索引(6)-索引使用(覆盖索引与回表查询,前缀索引,单列索引与联合索引 )、索引设计原则、索引总结
    学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
    java: java.lang.OutOfMemoryError: Java heap space报错解决办法
  • 原文地址:https://blog.csdn.net/weixin_48370579/article/details/127685229