sprin-aop 面向切面编程实例

package com.bjpowernode.ba03;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import java.util.Date;

/**

  • @Aspect:是aspectj框架中的注解。

  • 作用:表示当前类是切面类

  • 切面类:是用来给业务方法增加啊功能的类,在这个类中有切面代码

  • 位置:在类定义上方
    /
    @Aspect
    public class MyAspect {
    /
    *

    • 定义方法,方法是实现切面功能 方法定义要求
    • 1.公共方法 2.方法没有返回值 3.方法名称自定义 4.方法可以有参数,也可以没参数 如果有参数,参数不是自定义,有几个参数类型可以使用
      */

    /**

    • @Before:前置通知注解
    • 属性:value 是切入点表达式,表示切面的功能执行的位置
    • 位置:方法的上面
    • 特点:在目标方法之前执行,不回改变目标方法的执行 也不回影响目标方法的执行
      /
      /
      *
    • JointPoint:业务方法,要加入切面功能的业务方法
    • 作用是:可以在通知方法中获取方法执行时的信息,例如方法名称,方法的参数
    •  如果你的切面功能中需要用到方法信息,就加入Join Point
      
    •      这个JoinPoint参数是框架赋予的,必须是第一个位置加入
      

    */
    // @Before(value = "execution(public void com.bjpowernode.ba02.SomeServiceImpl.doSome(String,Integer))")
    // public void myBefore(JoinPoint joinPoint){
    // //获取方法的完整定义
    // System.out.println("方法签名(定义)="+joinPoint.getSignature());
    // System.out.println("方法名称(定义)="+joinPoint.getSignature().getName());
    // Object args[]=joinPoint.getArgs();
    // for (Object arg:args
    // ) {
    // System.out.println("参数="+arg);
    // }
    // //切面要执行的功能
    // System.out.println("前置通知 切面功能:在目标执行前输出执行时间"+new Date());
    // }

    /**

    • @AfterReturning 后置通知
    •  属性一:切入点表达式 属性二:returning 自定义的变量,表示目标方法的返回值 子弟工艺变量名必须跟通知方法的形参名一样。
      
    •  实用位置:在方法定义上面
      
    •  特点:1.在目标方法之后执行,能够获取到目标方法的返回值,可以根据返回值做不同处理的功能 3.可以修改返回值
      
    •  在后置通知中修改返回结果过,不会印象程序结果
      

    /
    // @AfterReturning(value = "execution(
    *..SomeServiceImpl.doOther(..))",returning = "res")
    // public void myAfertReturing(Object res){
    // //Obeject res:是目标 方法执行后的返回值,根据返回值做切面处理功能
    // System.out.println("后置通知:在慕白方法之后执行的返回值是:"+res);
    // if (res.equals("abcd")){
    // //任意增加其他功能
    // res="无敌臭弟弟";
    // }else{
    //
    // }
    // }
    // public void myAfertReturing2(Object res){
    // //Obeject res:是目标 方法执行后的返回值,根据返回值做切面处理功能
    // System.out.println("后置通知:在慕白方法之后执行的返回值是:"+res);
    // //如果修改了res的内容,属性等,是不是最后会影响调用的结果过
    //
    // }

    /**

    • 环绕通知
    • 1.public 2.必须有一个返回值 3.方法名称自定义 4.方法有参数
    • 注解叫@Around
    •  属性:value 切入点名称
      
    •  位置:方法定义上面
      
    • 特点:
    •  1.他是功能最强的通知
      
    •  2.在目标方法前和后都有增强功
      
    •  3.控制目标方法是否被执行
      
    •  4.修改原来的目标方法执行结果,影响最后的调用结果
      
    • 参数:ProceedingJoinPoint 就等同于 Method
    •  作用执行目标方法
      
    • 返回值:目标方法的执行结果,可以被修改
      
    • 环绕通知:经常做事务管理,在目标方法之前开启事务,执行目标方法,在目标方法之后提交事务
      /
      @Around(value = "execution(
      *..SomeServiceImpl.doFirst(..))")
      public Object myAround(ProceedingJoinPoint proceedingJoinPoint)throws Throwable{
      //环绕通知的功能
      //目标方法调用
      System.out.println("环绕通知:在目标犯法之前,输出时间"+new Date());
      Object resule=null;
      resule= proceedingJoinPoint.proceed();//等同于Merhod.invoke();
      System.out.println("环绕通知之后:在目标方法之后,提交事务");
      //在目标方法前或者后加功能
      return resule;
      }
      }
原文地址:https://www.cnblogs.com/cengzhuquan/p/14619895.html