SpringBoot Aop入门

今天重新学习了一下spring aop的相关知识,结合springBoot对Aop的支持,整理了一下学习成果。这只是简单的入门学习,下面演示下代码。

1. 声明一个aop注解

import java.lang.annotation.*;

/**
 * 声明一个注解
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
    String name();
}

这个注解中有一个属性name,为了后面演示注解式切面的时候使用。

2. 引入springboot aop的pom文件

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

这里我使用的springboot版本是2.1.2.RELEASE,需要在parent中指定其版本号:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/>
</parent>

3.编写被切方法:

在这里写两个类,第一个用来演示注解式切面,第二个用来演示方法切面。

  1. 注解式
import com.xqnode.learning.common.aop.annotation.Action;
import org.springframework.stereotype.Service;

@Service
public class AopAnnotationService {

    @Action(name = "注解式拦截doSomething操作")
    public void doSomething() {}
}

2.方法式:

import org.springframework.stereotype.Service;

@Service
public class AopMethodService {

    public void doSomething() {}
}

4.编写切面类

import java.lang.reflect.Method;

@Aspect
@Component
public class LogAspect {

    @Pointcut("@annotation(com.xqnode.learning.common.aop.annotation.Action)")
    public void annotationPointCut() {
    }

    @Before("annotationPointCut()")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action annotation = method.getAnnotation(Action.class);
        System.out.println("注解式拦截:" + annotation.name());
    }

    @After("execution(* com.xqnode.learning.service.AopMethodService.* (..))")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法式拦截:" + method.getName());
    }
}

使用注解式切面需要声明一个方法annotationPointCut(),指定切面注解的类型,这里我们指定的是前面我编写的注解@Pointcut("@annotation(com.xqnode.learning.common.aop.annotation.Action)"),在AopAnnotationServicedoSomething方法上我对其标记了注解,所以这个注解式切面会作用于这个类的这个方法上。
@Before是表示在方法执行之前执行的代码,这里我们简单打印了一句注解的名称。
@After(“execution(* com.xqnode.learning.service.AopMethodService.* (…))”)中的表达式的意思是对AopMethodService的所有方法都进行切面操作,并且是在方法执行完成之后执行after方法中的代码,这里我打印了所有方法的名称。
在切面类中需要定义切面方法用于响应响应的目标方法,切面方法即为通知方法,通知方法需要用注解标识,AspectJ 支持 5 种类型的通知注解:

@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行 。
@AfterRunning: 返回通知, 在方法返回结果之后执行
@AfterThrowing: 异常通知, 在方法抛出异常之后
@Around: 环绕通知, 围绕着方法执行

5. 验证

我们编写一个测试类验证下结果:

import com.xqnode.learning.service.AopAnnotationService;
import com.xqnode.learning.service.AopMethodService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LearningApplicationTests {

    @Autowired
    AopAnnotationService aopAnnotationService;

    @Autowired
    AopMethodService aopMethodService;

    @Test
    public void contextLoads() {
    	//注解式切面
        aopAnnotationService.doSomething();
        //方法切面1
        aopMethodService.doSomething();
        //方法切面2
        aopMethodService.otherThing();
    }
}

do
可以看到,我们的注解式切面和方法式切面都生效了。注解式的切面比较灵活,而方法式的切面作用的范围广,大家可以根据实际需要选择适合自己的切面方式。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
AOP(Aspect-Oriented Programming)是面向切面编程的一种编程范式,通过在程序的不同阶段植入通用的切面逻辑,使得这些逻辑可重用并可以在不同的模块中引用。其中,aop returning是AOP的一个关键方面,用于在方法执行成功返回之后执行的操作。 aop returning是在被通知的方法成功返回结果之后,执行一些附加操作的一种机制。通常,这些附加操作包括日志记录、性能监测、事务管理等。通过aop returning,我们可以将这些通用操作与原始方法逻辑解耦,并将它们封装在切面中。 在使用aop returning时,我们首先需要定义一个切面(Aspect),其中包含切入点(Pointcut)和通知(Advice)。切入点决定了哪些方法会被植入切面逻辑,通知则定义了方法执行前、后以及返回时的具体操作。 在aop returning中,通知类型为返回通知(AfterReturning Advice)。当被通知的方法成功执行并返回结果时,返回通知即会触发。我们可以在返回通知中编写所需的逻辑,比如记录返回结果、日志输出或是检查业务逻辑等。 通过使用aop returning,我们可以集中处理各个方法执行成功后的附加操作,避免了在每个方法中显式编写这些逻辑的重复性工作。这样可以提高代码的可维护性、可读性和代码复用程度。 总而言之,aop returning是AOP编程中用于在方法执行成功返回之后执行的一种机制。通过使用aop returning,我们可以在方法返回后执行一些附加操作,提高代码的可维护性和代码复用程度,并有效地解耦原始方法和通用操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员青戈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值