AOP介绍

1.重要概念

1.1 通知

通知:就是会在目标方法执行前后 执行的方法

try{
    try{
        //@Before
        method.invoke(..);
    }finally{
        //@After
    }
    //@AfterReturning
}catch(){
    //@AfterThrowing
}

在这里插入图片描述
举例,
以下这个方法就是通知,目标方法是UserDao类的addUser(),在addUser执行之后执行了log方法,所以log方法是后置通知,通过在方法上加上@After注解来表示。

@After(value="execution(* cn.xh.dao.UserDao.addUser(..))")
 public void log(){
 System.out.println("记录日志");
 }

切入点

切入点:需要增强的目标方法

如果只是一个具体的方法需要增强,通过类名和方法名找到它就可以了。
但是往往真实的需求中很多方法需要同样的通知进行增强

execution(* cn.xh.dao.UserDao.addUser(…)就是用来描述需要应用通知的方法的。
这里的含义是cn.xh.dao包UserDao类中的参数任意,返回值任意的addUser方法。

连接点

连接点:就是可以应用通知进行增强的方法。
一旦连接点被增强,就成为切入点。

//addUser被增强,所以也是切入点
public void addUser(){
 System.out.println("添加用户");
 }
 //连接点
 public void updateUser(){
 System.out.println("修改用户");
 }
 //连接点
 public void deleteUser(){
 System.out.println("删除用户");
 }
//三个方法都是连接点。

切面

切面:是切入点和通知的结合

织入

织入:就是通过动态代理对目标对象方法进行增强的过程。

2.示例

package com.tutorialspoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
@Aspect
public class Logging {
   /** Following is the definition for a pointcut to select
    *  all the methods available. So advice will be called
    *  for all the methods.
    */
   @Pointcut("execution(* com.tutorialspoint.*.*(..))")
   private void selectAll(){}
   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   @Before("selectAll()")
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }
   /** 
    * This is the method which I would like to execute
    * after a selected method execution.
    */
   @After("selectAll()")
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }
   /** 
    * This is the method which I would like to execute
    * when any method returns.
    */
   @AfterReturning(pointcut = "selectAll()", returning="retVal")
   public void afterReturningAdvice(Object retVal){
      System.out.println("Returning:" + retVal.toString() );
   }
   /**
    * This is the method which I would like to execute
    * if there is an exception raised by any method.
    */
   @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架中的一个重要特性,它提供了一种声明式的方式来进行系统级别的横切关注点(如日志、事务管理、安全检查等)的处理,而无需在每个具体业务模块中分散地编写这些代码。在Spring AOP中,关注点被分离到独立的“切面”(Aspect),这些切面可以跨越多个目标对象(即被通知的对象)。 Spring AOP的核心概念包括以下几个方面: 1. **通知(Advice)**:在特定的执行点(Pointcut)上执行的一段代码,可以是前置通知(Before)、后置通知(After)、环绕通知(Around)、异常通知(Throwing)和最终通知(After Returning/Throwing)。 2. **切点(Pointcut)**:定义了通知何时以及在哪里执行的规则,通常基于方法签名、类型匹配或注解。 3. **连接点(Join Point)**:通知被应用的地方,比如方法执行前、执行后或抛出异常时。 4. **代理(Proxy)**:Spring通过AOP框架为切面创建的代理对象,可以在运行时动态地拦截目标方法的调用并执行通知。 5. **切面(Aspect)**:封装了共享的关注点,由一组通知组成,可以被多个目标对象共享。 使用Spring AOP可以带来以下好处: - **代码复用**:将跨模块的业务逻辑抽取出来,避免代码冗余。 - **可维护性**:关注点集中管理,使得业务逻辑更清晰。 - **灵活性**:可以动态添加或移除切面,适应需求变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值