05-Spring的AOP编程之基于注解开发

Posted on 2021-06-20 22:28  萌栈师  阅读(44)  评论(0编辑  收藏  举报

Spring的AOP基于注解的开发

  • 首先在AOP配置文件中,开启注解的aop开发
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  • 写注解增强类 需要加上注解@Aspect
@Component
@Aspect  /*表示告诉spring当前类是切面类*/
public class MyAspect2 {

    @Before("execution(* com.yd.service.impl.UserServiceImpl.add(..))")
    public void check(){
        System.out.println("----之前校验身份");
    }

    @AfterReturning("execution(* com.yd.service.impl.UserServiceImpl.delete(..))")
    public void back(){
        System.out.println("----之后增强");
    }

    /**
     * 接收原方法返回值的后置增强方法
     * @param obj 切入点的返回值
     */
    @AfterReturning(value = "execution(* com.yd.service.impl.UserServiceImpl.deleteReturn(..))",returning = "obj")
    public void backReturn(Object obj){
        System.out.println("后置接收切入点返回值:"+obj);
    }

    /**
     * 环绕增强
     * @param point 连接点
     * @throws Throwable
     */
    @Around(value = "execution(* com.yd.service.impl.UserServiceImpl.deleteAround(..))")
    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("----之前增强");
        //执行切入点的方法
        point.proceed();
        System.out.println("----之后增强");
    }

    /**
     * 环绕增强,接收切入点的传入的参数
     * @param point
     * @throws Throwable
     */
    @Around("execution(* com.yd.service.impl.UserServiceImpl.update(..))")
    public void aroundParam(ProceedingJoinPoint point)throws Throwable{
        System.out.println("---之前增强");
        //获取切入点的参数
        Object[] args = point.getArgs();
        point.proceed();
        System.out.println("---之后增强 切入点参数1:"+args[0]+" 参数2:"+args[1]);
    }
    /**
     * 环绕增强,接收切入点的传入的参数,切接收返回
     * @param point
     * @throws Throwable
     */
    @Around("execution(* com.yd.service.impl.UserServiceImpl.updateReturn(..))")
    public void aroundReturn(ProceedingJoinPoint point)throws Throwable{
        System.out.println("---之前增强");
        //获取切入点的参数
        Object[] args = point.getArgs();
        String str = (String)point.proceed();
        System.out.println("---之后增强 切入点参数1:"+args[0]+" 参数2:"+args[1]+" 返回值:"+str);
    }

    /**
     * 切入点有异常
     * @param e 异常
     */
    @AfterThrowing(value = "execution(* com.yd.service.impl.UserServiceImpl.selectException(..))",throwing = "e")
    public void afterCatch(Exception e){
        System.out.println(e.getMessage());
        System.out.println("---捕获异常");
    }

    //@After("execution(* com.yd.service.impl.UserServiceImpl.selectException(..))")
    @After("execution(* com.yd.service.impl.UserServiceImpl.selectFin(..))")
    public void finallyDo(){
        System.out.println("finally,总会执行...");
    }

}
  • 用注解时,@After与@AfterThrowing同时增强同一个异常方法时,先执行@After,再执行@AfterThrowing

    • 与xml增强最后文档中的方式二结果一致
  • 补充:强制使用Cglib的动态代理,在开启aop注解下,加上

<?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 definitions here -->
    <!--开启aop注解-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <!--强制使用Cglib的动态代理-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

Copyright © 2024 萌栈师
Powered by .NET 9.0 on Kubernetes