SpringMVC - 基于注解的AOP开发、注解配置AOP详解

基于注解的AOP开发

快速入门

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-clZjXQYq-1650892667576)(spring.assets/image-20220425195412772.png)]

新建接口、目标对象类、通知类

package com.taotao.anno;

/**
 * create by 刘鸿涛
 * 2022/4/24 16:19
 */
@SuppressWarnings({"all"})
public interface TargetInterface {
    public void save();
}

package com.taotao.anno;

/**
 * create by 刘鸿涛
 * 2022/4/24 16:27
 */
@SuppressWarnings({"all"})
public class Target implements TargetInterface {

    @Override
    public void save() {
        System.out.println("正在存储数据....");
    }
}

package com.taotao.anno;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * create by 刘鸿涛
 * 2022/4/24 19:51
 */
@SuppressWarnings({"all"})
public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }

    public void afterReturning(){
        System.out.println("后置增强.....");
    }

    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强......");
    }

    public void after(){
        System.out.println("最终增强...");
    }
}

将目标类和切面类的对象创建权交给Spring

使用注解@Component(“myAspect”)

package com.taotao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

/**
 * create by 刘鸿涛
 * 2022/4/24 19:51
 */
@SuppressWarnings({"all"})
@Component("myAspect")
public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }

    public void afterReturning(){
        System.out.println("后置增强.....");
    }

    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强......");
    }

    public void after(){
        System.out.println("最终增强...");
    }
}

@Component(“target”)

package com.taotao.anno;

import org.springframework.stereotype.Component;

/**
 * create by 刘鸿涛
 * 2022/4/24 16:27
 */
@SuppressWarnings({"all"})
@Component("target")
public class Target implements TargetInterface {

    @Override
    public void save() {
        int i = 1 / 0;
        System.out.println("正在存储数据....");
    }
}

使用注解告知Spring切面类

@Aspect //标注当前MyAspect是一个切面类

注解配置通知方法,并写入切点表达式

package com.taotao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * create by 刘鸿涛
 * 2022/4/24 19:51
 */
@SuppressWarnings({"all"})
@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {

    //配置前置通
    @Before("execution(* com.taotao.anno.*.*(..))")
    public void before(){
        System.out.println("前置增强....");
    }

    public void afterReturning(){
        System.out.println("后置增强.....");
    }

    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强......");
    }

    public void after(){
        System.out.println("最终增强...");
    }
}

在切面类中使用注解织入关系

    //配置前置通
    @Before("execution(* com.taotao.anno.*.*(..))")

开启组件扫描和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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

<!--    组件扫描-->
    <context:component-scan base-package="com.taotao.anno"/>

<!--    aop自动代理-->
    <aop:aspectj-autoproxy/>


</beans>

Spring测试类

package com.taotao;

import com.taotao.anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * create by 刘鸿涛
 * 2022/4/24 20:17
 */
@SuppressWarnings({"all"})

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AnnoTest {

    @Autowired
    private TargetInterface target;

    @Test
    public void test1(){
        target.save();
    }
}

成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JlKH6l5s-1650892667578)(spring.assets/image-20220425202836500.png)]

注解配置AOP详解

注解通知的类型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5R6OA8rf-1650892667580)(spring.assets/image-20220425205259146.png)]

演示环绕通知

package com.taotao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * create by 刘鸿涛
 * 2022/4/24 19:51
 */
@SuppressWarnings({"all"})
@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {

    public void before(){
        System.out.println("前置增强....");
    }

    public void afterReturning(){
        System.out.println("后置增强.....");
    }

    @Around("execution(* com.taotao.anno.*.*(..))")
    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强......");
    }

    public void after(){
        System.out.println("最终增强...");
    }
}

成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ky0TKGQ3-1650892667581)(spring.assets/image-20220425205712559.png)]

切点表达式抽取

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Cg4joP4A-1650892667581)(spring.assets/image-20220425205758234.png)]

演示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JRbcOF9P-1650892667582)(spring.assets/image-20220425210621738.png)]

package com.taotao.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * create by 刘鸿涛
 * 2022/4/24 19:51
 */
@SuppressWarnings({"all"})
@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {

    public void before(){
        System.out.println("前置增强....");
    }

    public void afterReturning(){
        System.out.println("后置增强.....");
    }

    @Around("pointcut()")
    //Proceeding JoinPoint:正在执行的连接点 === 切点
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强...");

        Object proceed = pjp.proceed(); //切点方法

        System.out.println("环绕后增强...");
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强......");
    }

    public void after(){
        System.out.println("最终增强...");
    }

    @Pointcut("execution(* com.taotao.anno.*.*(..))")
    public void pointcut(){}
}

成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xaDMm4Lw-1650892667583)(spring.assets/image-20220425210637765.png)]

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值