25-Spring-Aop注解开发

基于注解的切面配置

1、配置xml,开启注解

2、定义切入点:前置

3、定义切入点:后置

4、定义切入点:环绕

5、定义切入点:异常

 

1、配置xml,开启注解

  //aop注解激活的
  <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

 //配置包扫描
  <!-- 告诉spring 注解在哪个位置 -->
  <context:component-scan base-package="com.nuc"/>

2、定义切入点:前置

package com.nuc.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class AdviceAop {
    //切入点
    @Pointcut("execution(* com.nuc..*.*(..))")
    public  void pointcut(){}

    //前置
    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法" +"方法入参"+ Arrays.toString(jp.getArgs()));
    }

    //后置
    /*
    @AfterReturning(pointcut="pointcut()",returning="result")
    public void afterReturning(JoinPoint jp,Object result){
        System.out.println("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法"
                +"   方法返回值"+result);
    }
    */

    //环绕
    /*
    @Around("pointcut()")
    public  void around(){
    }
     */
    //异常
    /*
    @AfterThrowing(value = "pointcut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
    }
     */
}

3、定义切入点:后置

package com.nuc.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class AdviceAop {
    //切入点
    @Pointcut("execution(* com.nuc.serveriml.StudentDaoMapperServiceIml.getAllStudent(..))")
    public  void pointcut(){}

    //前置
    /*
    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法" +"方法入参"+ Arrays.toString(jp.getArgs()));
    }
     */

    //后置
    @AfterReturning(pointcut="pointcut()",returning="result")
    public void afterReturning(JoinPoint jp,Object result){
        System.out.println("后置 afterReturning ");
        System.out.println("方法返回的结果是 result="+result);
    }


    //环绕
    /*
    @Around("pointcut()")
    public  void around(){
    }
     */
    //异常
    /*
    @AfterThrowing(value = "pointcut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
    }
     */

}

4、定义切入点:环绕

package com.nuc.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class AdviceAop {
    //切入点
    @Pointcut("execution(* com.nuc.serveriml.StudentDaoMapperServiceIml.getAllStudent(..))")
    public  void pointcut(){}

    //前置
    /*
    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法" +"方法入参"+ Arrays.toString(jp.getArgs()));
    }
     */

    //后置
    /*
        @AfterReturning(pointcut="pointcut()",returning="result")
        public void afterReturning(JoinPoint jp,Object result){
            System.out.println("后置 afterReturning ");
            System.out.println("方法返回的结果是 result="+result);
        }
     */


    //环绕

    @Around("pointcut()")
    public  Object  around(ProceedingJoinPoint proceedingJoinPoint){
        Object result = null;
        try {
            // 前置通知
            String methodName = proceedingJoinPoint.getSignature().getName();
            System.out.println("1、方法的名字:"+methodName);
            System.out.println("1、这里是  前置通知");
            // 调用目标方法
            result = proceedingJoinPoint.proceed();
            // 后置通知
            System.out.println("2、方法的名字:"+methodName);
            System.out.println("2、这里是  后置通知");

        } catch (Throwable e) {
            // 这里是异常通知
            System.out.println("3、这里是  异常通知");
            e.printStackTrace();
        }
        System.out.println("4 、这里是一个  后置返回通知");
        return result;
    }

    //异常
    /*
    @AfterThrowing(value = "pointcut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
    }
     */

}

5、定义切入点:异常

package com.nuc.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class AdviceAop {
    //切入点
    @Pointcut("execution(* com.nuc.serveriml.StudentDaoMapperServiceIml.getAllStudent(..))")
    public  void pointcut(){}

    //前置
    /*
    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法" +"方法入参"+ Arrays.toString(jp.getArgs()));
    }
     */

    //后置
    /*
        @AfterReturning(pointcut="pointcut()",returning="result")
        public void afterReturning(JoinPoint jp,Object result){
            System.out.println("后置 afterReturning ");
            System.out.println("方法返回的结果是 result="+result);
        }
     */


    //环绕
    /*
    @Around("pointcut()")
    public  Object  around(ProceedingJoinPoint proceedingJoinPoint){
        Object result = null;
        try {
            // 前置通知
            String methodName = proceedingJoinPoint.getSignature().getName();
            System.out.println("1、方法的名字:"+methodName);
            System.out.println("1、这里是  前置通知");
            // 调用目标方法
            result = proceedingJoinPoint.proceed();
            // 后置通知
            System.out.println("2、方法的名字:"+methodName);
            System.out.println("2、这里是  后置通知");

        } catch (Throwable e) {
            // 这里是异常通知
            System.out.println("3、这里是  异常通知");
            e.printStackTrace();
        }
        System.out.println("4 、这里是一个  后置返回通知");
        return result;
    }
    */

    //异常

    @AfterThrowing(value = "pointcut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        System.out.println("这里是 异常通知");
        System.out.println(ex);
    }


}

 

上一篇:spring aop|切入点pointcut表达式编写方案示例


下一篇:Spring学习_aop