AOP 和 前置通知,后置通知

Spring

1、AOP:中文名称面向切面编程

2、英文名称:(Aspect Oriented Programming)

3、正常程序执行流程都是纵向执行流程

  3.1 又叫面向切面编程,在原有纵向执行流程中添加横切面

  3.2 不需要修改原有程序代码

    3.2.1 高扩展性

    3.2.2 原有功能相当于释放了部分逻辑,让职责更加明确

4、面向切面编程是什么??

  4.1 在程序原有纵向流程中,针对某一个或某一些方法添加 通知,形成横切面过程,就叫做面向切面编程。

5、常用概念

  5.1 原有功能:切点,pointcut

  5.2 前置通知:在切点之前执行的功能 before advice

  5.3 后置通知:在切点之后执行的功能 after advice

  5.4 如果切点执行过程中出现异常,会触发异常通知 throws advice

  5.5 所有功能总称叫做切面

  5.6 织入:把切面嵌入到原有功能的过程叫做织入

6、spring提供了2种AOP实现方式

  6.1 Schema-based :

    6.1 每个通知都需要实现接口或类

    6.2 配置spring配置文件时 在<aop:config>配置

  6.2 Aspectj

    6.2.1 每个通知不需要实现接口或类

    6.2.2 配置spring配置文件是在<aop:config>的子标签

    <aop:aspect>中配置

Schema-based实现步骤:

  1、导入包

  2、新建通知类

    2.1 新建前置通知类

      2.1.1 arg0:切点方法对象 Method方法

      2.1.2 arg1:切点方法参数

      2.1.3 arg2:切点在哪个对象中

public class MyBeforeAdvice implements MethodBeforeAdvice{
    @Override
    public void before(Method arg0, Object[] arg1, Object arg2)
            throws Throwable {
        System.out.println("前置通知125");
    }
}

    2.2 新建后置通知类

      2.2.1 arg0:切点方法返回值

      2.2.2 arg1:切点方法对象

      2.2.3 arg2:切点方法参数

      2.2.4 arg3:切点方法所在类的对象

public class MyAfterAdvice implements AfterReturningAdvice{
    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {
        System.out.println("执行的后置通知521");
    }
}

  3、配置spring配置文件

    3.1 引入aop命名空间

    3.2 配置通知类的<bean>

    3.3 配置切面

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"    配置命名空间
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        <!-- 配置通知类对象,在切面中引入 -->
    <bean id="mybefore" class="com.bjsxt.advice.MyBeforeAdvice"></bean>
    <bean id="myafter" class="com.bjsxt.advice.MyAfterAdvice"></bean>
    
        <!-- 配置切面 -->
    <aop:config>
        <!-- 配置切点 -->                                              如果希望匹配任意方法参数 (..)
        <aop:pointcut expression="execution(* com.bjsxt.test.Test.demo2())" id="mypoint"/>      * 通配符,匹配任意方法名,任意类名,任意一级包名
        <!-- 通知 -->
        <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/>
        <aop:advisor advice-ref="myafter" pointcut-ref="mypoint" />
    </aop:config>
        <!-- 配置Demo类,测试使用 -->
        <bean id="test" class="com.bjsxt.test.Test"></bean>
  </beans>

  4、编写测试代码

public class Demo {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        Test test = ac.getBean("test",Test.class);
        test.demo1();
        test.demo2();
        test.demo3();
    }        
}

  5、运行结果

 

好好学习,天天向上。 努力工作,给自己的和家人一个满意的答案。。。。
原文地址:https://www.cnblogs.com/axu521/p/10138102.html