AOP核心

AOP概念

面相关切面编程,利用AOP可以对业务的各个部分进行隔离,使得业务逻辑各部分之间耦合度降低,提高开发效率。

AOP的底层原理是动态代理:JDK动态代理和CGLIB动态代理

  • 连接点:类里面的那些方法可以被增强,这些方法称为连接点

  • 切入点:实际被真正增强的方法,称为切入点

  • 通知(增强):实际增强的逻辑部分称为通知(增强);通知有多种类型:

    • 前置通知
    • 后置通知
    • 环绕通知
    • 异常通知
    • 最终通知
  • 切面:把通知应用到切入点的过程

AOP准备

Spring框架里面一般基于AspectJ实现AOP操作

AspectJ :不是Spring的组成部分,它是独立的AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作

基于AspectJ实现AOP操作有两种方式实现:XML配置和使用注解

引入AOP依赖aspectJ
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
</dependency>
切入点表达式

切入点表达式:需要对哪个类里面的哪个方法进行增强

语法结构:execution([权限修饰符][返回类型][类路径][方法名称]([参数列表]))

举例说明,如:

//标识对UserService#logout方法进行增强
execution(* com.xxx.service.UserService.logout(..))  
//标识对UserService的所有方法进行增强
execution(* com.xxx.service.UserService.*(..))  
//标识对com.xxx.service的所有类的所有方法进行增强
execution(* com.xxx.service.*.*(..))  
AspectJ注解

基于AspectJ注解实例

@Aspect // 表明切面
@Component // 注入IOC
@Slf4j
public class XxxAspect {
    
    @Before(value="execution(* com.xxx.service.UserService.*(..))")
    public void before(){
        log.info("方法执行之前");
    }
    
    @Around(value="execution(* com.xxx.service.UserService.*(..))")
    public void around(){
        log.info("方法执行中");
    }
    
    
    @After(value="execution(* com.xxx.service.UserService.*(..))")
    public void after(){
        log.info("方法执行之后");
    }
    
    @AfterReturning(value = "execution(* com.xxx.service.UserService.*(..))")
    public void afterRet(JoinPoint joinPoint, Object result) throws Throwable {
        log.info("方法返回值之后");
    }
    
    @AfterThrowing(value = "execution(* com.xxx.service.UserService.*(..))")
    public void afterThrow(JoinPoint joinPoint, Object result) throws Throwable {
        log.info("方法执行抛出异常");
    }
}

相同切入点可以进行抽取

@Aspect // 表明切面
@Component // 注入IOC
@Slf4j
public class XxxAspect {
   // 相同切入点抽取
    @Pointcut(value="execution(* com.xxx.service.UserService.*(..))")
    public void commonPoincut(){
        
    }
    
    @AfterReturning(value = "commonPoincut()")
    public void afterRet(JoinPoint joinPoint, Object result) throws Throwable {
        log.info("方法返回值之后");
    }
    
    @AfterThrowing(value = "commonPoincut()")
    public void afterThrow(JoinPoint joinPoint, Object result) throws Throwable {
        log.info("方法执行抛出异常");
    }
}

多个增强类对同一个方法做增强,可以通过Order设置优先级,数值越小越先执行

@Aspect // 表明切面
@Component // 注入IOC
@Slf4j
@Order(1) 
public class XxxAspect1 {
    
    @AfterReturning(value = "execution(* com.xxx.service.UserService.*(..))")
    public void afterRet(JoinPoint joinPoint, Object result) throws Throwable {
        log.info("方法返回值之后");
    }
}

@Aspect // 表明切面
@Component // 注入IOC
@Slf4j
@Order(2)
public class XxxAspect2 {
    
    @AfterReturning(value = "execution(* com.xxx.service.UserService.*(..))")
    public void afterRet(JoinPoint joinPoint, Object result) throws Throwable {
        log.info("方法返回值之后");
    }
}

// XxxAspect1中的afterRet会先执行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值