Spring三大核心思想之AOP(面向切面编程)

Spring三大核心思想之AOP(面向切面编程)

学习Spring三大核心思想之AOP之前建议先学习:
?1、Srping之IOC思想及使用
?2、Spring核心思想AOP的底层设计模式之代理模式

? 程序员小王的博客:程序员小王的博客
? 欢迎点赞 ? 收藏 ⭐留言 ?
? 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕
?java自学的学习路线:java自学的学习路线

一、什么是AOP(面向切面编程)?

AOP 为 Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式运行期 动态代理 实现程序功能的统一维护的一种技术。

AOP (面向切面编程)是 OOP(面向对象) 的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程 的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

二、AOP 的作用及其优势

作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强

优势:减少重复代码,提高开发效率,并且便于维护

三、AOP 的底层实现

实际上,AOP 的底层是通过 Spring 提供的的动态代理技术实现 的。在运行期间,Spring通过动态代理技术动态的生成代理对象,代理对象方法执行时进行增强功能的介入,在去调用目标对象的方法,从而完成功能的增强。

  • 动态代理的作用:

    1)在目标类源代码不改变的情况下,增加功能。

    2)减少代码的重复

    3)专注业务逻辑代码

    4)解耦合,让你的业务功能和日志,事务非业务功能分离。

代理设计模式详细笔记代理模式详细笔记,想学好Spring的AOP思想先理解代理模式

java设计模式之代理设计模式笔记详细内容:

  1. 代理模式是结构型模式其中的一种

  2. 现在开发中存在的问题

  3. 什么是代理模式,为什么需要使用代理模式

  4. 静态代理及实现

  5. 什么是动态代理

  6. JDK 动态代理和cglib动态代理的使用及区别

  7. 三种动态代理的对比及优缺点

  8. 代理模式的使用场景

四、AOP 相关概念

Spring 的 AOP 实现底层就是对动态代理的代码进行了封装 ,封装后我们只需要对需要关注的部分进行代码编写,并通过配置的方式完成指定目标的方法增强。

在正式讲解 AOP 的操作之前,我们必须理解 AOP 的相关术语,常用的术语如下:

1、切面(Aspect)

一个切面就是一个代理对象= 要为那些类生成代理+要添加的额外功能是那些

2、切入点(pointcut):将来要为那些类生成代理对象

3、通知/ 增强(advice):就是要添加的额外功能

  • 生活案例:给面包之间涂果酱

给面包之间涂果酱

注意:切面(Aspect)=切入点(pointcut)+通知点(advice,额外功能)

4、AOP中的(面向切面编程)通知

前置通知:目标方法之前的额外功能 MethodBeforeAdvice

环绕通知:它是spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方式 MethodInterceptor(Interceptor [ɪntəˈsɛptə]拦截器)

后置通知:目标方法之后的额外功能 AfterReturningAdvice(returning[rɪˈtɜːrnɪŋ])

异常通知:执行异常的额外功能 ThrowsAdvice

最终通知:一定执行的额外功能

5、Target(目标对象):代理的目标对象

6、代理(Proxy[ˈprɑːksi]):一个类被AOP注入增强后,就产生一个结果代理类

7、连接点(joinPoint):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法(额外功能),因为spring只支持方法类型的连接点

五、切面编程步骤

1、五种通知类

AOP 切面=切入点+通知

  1. 前置通知:MethodBeforeAdvice

  2. 后置通知:AfterReturnAdvice

  3. 环绕通知:MethodInterceptor

  4. 异常通知:ThrowsAdvice(throw [θroʊz])

  5. 最终通知

  • 通知的配置语法:
<aop : 通知类型 method=“切面类中方法名” pointcut=“切点表达式"> </aop:通知类型>
  • 切点表达式的抽取

当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。

<aop:config>
    <!--引用myAspect的Bean为切面对象-->
    <aop:aspect ref="myAspect">
        <aop:pointcut id="myPointcut" expression="execution(* com.tjcu.aop.*.*(..))"/>
        <aop:before method="before" pointcut-ref="myPointcut"></aop:before>
    </aop:aspect>
</aop:config>
  • aop织入的配置
<aop:config>
    <aop:aspect ref=“切面类”>
        <aop:before method=“通知方法名称” pointcut=“切点表达式"></aop:before>
    </aop:aspect>
</aop:config>
  • 切点表达式的写法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))

2、AOP的开发步骤

1、开发目标类

2、开发通知类,确定额外功能

3、管理通知类

4、配置切入点 确定要为那些类添加额外功能

5、将目标类和切面类的对象创建权交给 spring

6、组装切面 切入点+通知(额外功能)

3、AOP编程所需要的依赖

引入依赖
   spring-aop
   spring-expression
   spring-aspects

六、AOP实现前置通知案例

1、目标接口类

public interface CityService {
    public void login();
    public  void add(String name);
}

2、目标实现类(核心功能)

/**
 * @author 王恒杰
 * @Description:目标对象
 */
public class CityServiceImpl implements CityService{
    @Override
    public void login() {
        //前置通知:System.out.println("嘻嘻哈哈");

        //执行核心的业务逻辑 调用Dao
        System.out.println("登录调用Dao");
    }

    @Override
    public void add(String name) {
        //前置通知:System.out.println("嘻嘻哈哈");
        //执行核心的业务逻辑 调用Dao
        System.out.println("添加调用Dao");
    }
}

3、前置通知(额外功能)动态代理代码

/**
 * @author 王恒杰
 * @Description: 通知类。额外功能
 */
public class MyBeforeAdvice implements MethodBeforeAdvice {
    /**
     * 额外功能书写的方法
     * 参数1:代理对象当前调用的方法
     * 参数2:当前代理对象调用的方法的参数
     * 参数3:目标对象(被代理的对象)
     * @param method
     * @param objects
     * @param o
     * @throws Throwable
     */
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
     System.out.println("嘻嘻哈哈");
    }
}

4、将目标类和切面类的对象创建权交给 spring

<!--管理目标对象-->
    <bean class="before.CityServiceImpl" id="cityService"></bean>
    <!--管理通知类 动态代理实现AOP-->
    <bean id="myBeforeAdvice" class="before.MyBeforeAdvice"></bean>

5、在 spring.xml 中配置织入关系(前置功能)

<!--组装切面-->
    <aop:config>
    <!--配置切入点
    id:切入点的唯一标识
    expression:切入点表达式,为那些类添加额外功能
     execution() 切入点表达式的一种 精确到要添加到额外功能的方法
     *:通配
     空格:
     before.CityServiceImpl.*:所有方法
     (..):所有参数
    -->
        <aop:pointcut id="pc1" expression="execution(* before.CityServiceImpl.*(..))"/>
    <!--组装切面=切入点+通知
      advice-ref:通知的id
      pointcut-ref:切入点的id
    -->
        <aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="pc1"></aop:advisor>
    </aop:config>

6、测试代码

   @Test
    public void testMethodBeforeAdvice() {
        //启动工厂
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("before/spring.xml");
        //获取组件 目标对象就是代理对象
        CityService cityService = (CityService) ctx.getBean("cityService");
        
        //目标对象就是代理对象  class com.sun.proxy.$Proxy4
        System.out.println(cityService.getClass());    
        
        //调用方法,通过代理类调用目标类
        cityService.add("123");
    }

注意: 获取组件时,目标对象就是代理对象

七、Spring中的环绕通知案例

1、dao层接口

public interface StudentDao {
    /**
     * 登录
     * @param name
     */
    public void login(String name);

    /**
     * 分页
     * @param name
     * @return
     */
    public String pageShow(String name);
}

2、dao层实现类

public class StudentDaoImpl implements StudentDao{
    @Override
    public void login(String name) {
        //循环10000次
        for (int i = 0; i < 10000; i++) {

        }
        System.out.println("数据库实现登录");

    }

    @Override
    public String pageShow(String name) {
        //循环10000次
        for (int i = 0; i < 10000; i++) {

        }
        System.out.println("数据库实现分页");
        return name;

    }
}

3、目标接口类

public interface StudentService {
    /**
     * 登录
     * @param name
     */
    public void login(String name);

    /**
     * 分页
     * @param name
     * @return
     */
    public String pageShow(String name);
}

4、目标实现类

public class StudentServiceImpl implements StudentService{
    private StudentDao studentDao;

    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public void login(String name) {
        System.out.println("登录日志");
      studentDao.login(name);
    }

    @Override
    public String pageShow(String name) {
        System.out.println("分页日志");
        String s = studentDao.pageShow(name);
        return s;
    }
}

5、环绕通知

  • 核心方法: Object proceed = methodInvocation.proceed(); 放行
public class StudentAroundAdvice implements MethodInterceptor {
    /**
     * 参数 内部封装者当前的代理对象 方法的参数,执行方法等
     *
     * @param methodInvocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        //1.控制事务
        System.out.println("控制事务");
        //method Invocation 方法调用
        System.out.println("当前调用方法的名字" + methodInvocation.getMethod().getName());
        //arguments参数 methodInvocation:方法调用
        System.out.println("当前的参数为:" + methodInvocation.getArguments()[0]);

        System.out.println("--------------");

        //记录当前的时间 单位毫秒
        long begin = System.currentTimeMillis();
        System.out.println("调用查询的数据库");

        //放行,执行目标方法 proceed:继续做proceed
        Object proceed = methodInvocation.proceed();


        //记录结束的时间 单位毫秒
        long end = System.currentTimeMillis();
        System.out.println("dao执行所用时间" + (end - begin));

        return proceed;
    }
}

6、将目标类和切面类的对象创建权交给 spring

  <!--管理dao组件-->
    <bean id="studentDao" class="com.tjcu.dao.StudentDaoImpl"></bean>

    <!--管理Service组件/目标对象-->
    <bean id="studentService" class="com.tjcu.service.StudentServiceImpl">
        <!--注入值-->
        <property name="studentDao" ref="studentDao"></property>
    </bean>

    <!--管理通知组件-->
    <bean id="studentAroundAdvice" class="com.tjcu.advice.StudentAroundAdvice"></bean>

7、在 applicationContext.xml 中配置织入关系,aop相关配置

 <!--aop相关配置  切面=切点+环绕通知-->
    <aop:config>
        <!--切入点 execution:[eksɪˈkjuːʃn]执行-->
        <aop:pointcut id="pointcut" expression="execution(* com.tjcu.service.StudentServiceImpl.*(..))"/>
        <!--组装切面  advisor[ [ædˈvaɪzər]]:顾问  advice-ref:通知  pointcut-ref:切入点-->
        <aop:advisor advice-ref="studentAroundAdvice" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>

8、测试代码

  @Test
    public void AroundAdviceTest() {
        //启动工厂
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/tjcu/Spring/ApplicationContext.xml");

        //获取组件 目标对象就是代理对象
        StudentService studentService = (StudentService) context.getBean("studentService");

        //调用方法,通过代理类调用目标类
        studentService.pageShow("通过代理类调用调用目标类");
    }

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>