zoukankan      html  css  js  c++  java
  • Spring-05 -AOP [面向切面编程] -Schema-based 实现aop的步骤

    .AOP [知识点详解]

    AOP:中文名称面向切面编程
    英文名称:(Aspect Oriented Programming)
    正常程序执行流程都是纵向执行流程
    3.1 又叫面向切面编程,在原有纵向执行流程中添加横切面
    
    3.2 不需要修改原有程序代码
    
    3.2.1 高扩展性
    
    3.2.2 原有功能相当于释放了部分逻辑.让职责更加明确.
    
    面向切面编程是什么?
    4.1 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面过程就叫做面向切面编程.
    
    常用概念
    5.1 原有功能: 切点, pointcut
    5.2 前置通知: 在切点之前执行的功能. before advice
    
    5.3 后置通知: 在切点之后执行的功能, after advice
    
    5.4 如果切点执行过程中出现异常,会触发异常通知.throws advice
    
    5.5 所有功能总称叫做切面.
    
    5.6 织入: 把切面嵌入到原有功能的过程叫做织入
    
    5.6.1 spring 提供了 2 种AOP 实现方式
    
    5.6.2 Schema-based
    
    5.6.3 每个通知都需要实现接口或类
    
    5.6.4 配置spring 配置文件时在<aop:config>配置
    
    5.6.5 AspectJ
    
    5.6.6 每个通知不需要实现接口或类
    
    5.6.7 配置spring 配置文件是在<aop:config>的子标签 ,<aop:aspect>中配置

    .Schema-based 实现步骤

    1. 导入jar

    2. 新建通知类

      1.1 新建前置通知类

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

      1.1.2 arg1: 切点方法参数

      1.1.3 arg2:切点在哪个对象中

    import org.springframework.aop.MethodBeforeAdvice;
    import java.lang.reflect.Method;
    public class MyBeforeAdvice implements MethodBeforeAdvice {
        @Override
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("+++++输出执行前置通知");
        }
    }

        2 新建后置通知类

      2.1.1 arg0: 切点方法返回值

      2.1.2 arg1:切点方法对象

      2.1.3 arg2:切点方法参数

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

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

    3.配置spring 配置文件

      1.1 引入aop 命名空间

      1.2 配置通知类的<bean>

      1.3 配置切面

      1.4 * 通配符,匹配任意方法名,任意类名,任意一级包名

      1.5  如果希望匹配任意方法参数 (..)

    <?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/sc hema/beans
    http://www.springframework.org/schema/beans/spring-be ans.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.Demo.demo2())" id="mypoint"/>
    <!-- 通知 -->
    <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/>
    <aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/>
    </aop:config>
    <!-- 配置Demo 类,测试使用 -->
    <bean id="demo" class="com.bjsxt.test.Demo"></bean>
    </beans>

    4.编写测试代码

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

    运行结果:

     

  • 相关阅读:
    STM32的DMA
    STM32串口接收不定长数据原理与源程序(转)
    推挽与开漏
    开关量输入检测与输出的电路设计(转)
    理解一下单片机的I2C和SPI通信
    电阻桥的作用(转)
    为什么工业上用4到20毫安电流传输数据(转)
    Keil的标题“礦ision3" 的改变(转)
    epplus动态合并列数据
    npm脚本编译代码
  • 原文地址:https://www.cnblogs.com/zhazhaacmer/p/10101772.html
Copyright © 2011-2022 走看看