AOP的理解

本文介绍了AOP(面向切面编程)的基本概念、名词术语及其实现原理。AOP通过代理和反射实现,用于解决代码中重复性的处理,如日志记录、权限验证等。切面(@Aspect)包含切点(@Pointcut),通知(@Before、@After等)定义切入时机。AOP的实现包括注解定义、扫描切面、动态生成代理类以及拦截目标类的调用。
摘要由CSDN通过智能技术生成

 

1、AOP基本概念

       为什么会诞生AOP呢?实际上在我们的面相对象的编程中,我们通常都是将世界抽象成类,但是如果类中有重复的事情需要处理,比如日志、权限认证或者一些其他的东西,那么我们就得入侵代码并且添加相应的类,在每个需要调用的类的地方调用。举例说明如下:

       我们现在有如下的三个类,我们有新需求想在3个类的方法调用前和后都实现日志的打印,那么

1、传统最笨的处理方法就是在每个类中添加是、两个方法,一个是调用前打印,一个是调用后打印!!

2、单独申请一个日志类,然后写两个方法,根据传入值的不同做不同的处理,那么我们还是要在每个类中都进行修改,申请类,调用相关的方法。

3、使用代理,因为我们知道代理是可以持有当前类的示例并且对其进行相关的增强。JDK的动态代理是需要实现公共接口才行,Cglib需要继承类。

Class A{
    ...
}


class B{
    ...
}


class C{
    ...
}

 那么其实由方法3就引申出来了AOP,AOP的实现原理是代理+反射,说到这就要提到两个相关的概念,过滤器是函数的回调,拦截器是反射。执行顺序是:

Tomcat----->过滤器------>Servlet------>拦截器------>Controller----->AOP可以实现更加细粒度的分析,最细的粒度可以到方法

2、AOP基本名词概念

1、切面:添加注解@Aspect,就是包含各种切点,实现切入的一个类

2、连接点:Join Point:对某方法实现切入的具体点,所有的AOP都是围绕着这个点进行展开的

3、切点:添加注解@Pointcut,连接点的集合,实际上这个好像才是标注在方法上面的注解

4、通知:切入切点的时机,分为:
       4.1、@Before:在切点的方法调用前切入

      4.2、@After:在切点的方法调用后切入

     4.3、@Around:环绕型通知非常强大,会在方法执行前就切入,可以实现参数的修改,切入会一只执行到方法的结束,可以修改返回值

     4.4、@AfterReturning:切入点在方法返回返回值之后

     4.5、@AfterThrowing:在抛出异常的时候执行

那么有几个比较容易混乱的执行顺序如下:

 : @Around环绕通知
 : @Before通知执行
 : @Before通知执行结束
 : @Around环绕通知执行结束
 : @After后置通知执行了!
 : @AfterReturning第一个后置返回通知的返回值:18

 

 

 

3、AOP实现原理

AOP的实现原理是反射,具体实现步骤:

3.1、定义相关的注解

3.2、在BeanFactory的时候对BeanDefinition进行扫面,那么我们就可以在BeanDefinition加载的时候就扫描得到需要切入的类和方法了。

3.3、动态生成代理类,对扫描到的类进行增强

3.4、对该类的该方法的调用实现拦截,使得每次调用都是使用的代理类的相应方法。

下面是手写示例:

3.1、注解的定义:

package com.aop.demo.annotation;

import java.lang.annotation.*;

/*

 */
@Documented
@Inherited
//@Target({ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface After {
    String value() default "";
}
package com.aop.demo.annotation;

import com.sun.javafx.sg.prism.NodeEffectInput;

import java.lang.annotation.*;

@Documented
@Inherited
//@Target(?)
@Retention(RetentionPolicy.RUNTIME)
public @interface Around {
    String value() default "";
}

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/*
    这个注解是定义的切面

 */
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Aspect {

}
package com.aop.demo.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Before {
    String value() default "";

}
import com.aop.demo.selector.CustomizedImportSelector;
import org.springframework.context.annotation.Import;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/*
  AOP开启的开关注解
  这里加了Import注解,终于想通了,我们的注解是可以组合的嘛
  启动类的@SpringBootApplication就是组合注解

 */
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Import(CustomizedImportSelector.class)
public @interface EnableAspectAutoProxy {


}
package com.aop.demo.annotation;

import org.springframework.context.annotation.Import;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/*
    切点

 */
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Pointcut {
    String value() default "";
}

3.2、扫描得到切面及切入点

package com.aop.demo.processor;

import com.aop.demo.JunAspectUtil;
import com.aop.demo.annotation.Aspect;
import com.aop.demo.holder.ProxyBeanHolder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableL
Spring AOP(面向切面编程)是Spring框架中的一个重要模块,用于实现横切关注点的模块化。它通过在程序运行期间动态地将代码织入到应用程序的特定位置,从而实现对横切关注点的处理。 在Spring AOP中,通过定义切面(Aspect)和连接点(Join Point)来实现对横切关注点的处理。切面定义了在何处以及如何进行横切关注点的处理,而连接点则表示在应用程序中可以插入切面的位置。 Spring AOP的核心概念是切面、连接点、通知(Advice)、切点(Pointcut)和引入(Introduction): 1. 切面(Aspect):切面是一个模块化的单元,它包含了一组通知和切点。通知定义了在何时、何地以及如何进行横切关注点的处理,而切点定义了在应用程序中哪些连接点可以插入切面。 2. 连接点(Join Point):连接点是在应用程序中可以插入切面的位置。例如,方法调用、方法执行、异常抛出等都可以作为连接点。 3. 通知(Advice):通知定义了在连接点上执行的操作。常见的通知类型有前置通知(Before)、后置通知(After)、返回通知(After Returning)和异常通知(After Throwing)等。 4. 切点(Pointcut):切点是一个表达式,用于定义哪些连接点可以插入切面。通过切点表达式,可以选择性地将切面应用于特定的连接点。 5. 引入(Introduction):引入允许在现有的类中添加新的方法和属性。 Spring AOP的优点在于它能够将横切关注点与业务逻辑分离,提高了代码的可维护性和可重用性。同时,它也提供了一种非侵入式的方式来实现横切关注点的处理,不需要修改原有的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值