1、使用aop需要加入maven依赖
<!--与aop相关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
2、aop拦截指定方法
拦截getById方法:
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;
@Aspect
@Component
public class UserServiceAspect {
//拦截指定的方法
@Pointcut("execution(* com.zengqingfa.demo.service.impl.UserServiceImpl.getById(..))")
private void getByIdPointCut() {
}
@Around("getByIdPointCut()")
public Object getByIdAround(ProceedingJoinPoint pjp) throws Throwable {
//获取参数
Object[] args = pjp

本文介绍了如何在Spring AOP中使用AOP进行方法拦截,详细讲解了如何拦截指定的方法以及带有自定义注解的方法。首先,通过添加Maven依赖引入AOP支持。接着,展示如何拦截名为`getById`的方法。然后,重点讨论了如何针对带有`AspectAnnotation`注解的方法进行拦截,并在`isStart`参数为true时记录执行时间。最后,提到了使用表达式进行更复杂的拦截配置。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



