AOP基本理论与使用(基于配置、注解方式)

AOP(面向切面编程)是一种编程范式,用于在不修改源代码的情况下扩展功能,通过横向抽取机制替代传统纵向继承体系。适用于性能监控、事务管理等场景,基于动态代理实现,包括JDK动态代理和CGLib。SpringAOP通过Aspectj实现,支持配置文件和注解两种方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AOP(Aspect Oriented Programing)面向切面
扩展功能不通过修改源代码实现。AOP采用的横向的抽取机制,取代了传统的纵向继承体系写重复性代码。

AOP适用场景:性能监控,事务管理,安全检查,缓存等等

动态代理:JDK动态代理 包路径 java.lang.reflet
创建LogImpl平级的对象,通过invocationHandler辅助类实现
实现的不是真正的对象,真正的代理对象在运行时产生

AOP的实现是基于动态代理实现
有两种实现方式:
第一种:在有接口的情况下,通过jdk动态代理实现
第二种:在没有接口的情况下,通过CGLib代理方式创建类的子类,
通过子类调用杜父类的方法完成增强(添加日志功能)

AOP的术语
Joinpoint(连接点):指的是被拦截的点,在spring中指的是方法,
在类中的方法可以被增强,这些方法称之为连接点

Pointcut(切入点):对Joinpoint进行拦截定义在类中很多的方法是可以被增强的,
比如:在User类中add()方法,实际增强的点称之为切入点

Advice(增强/通知): 所谓的增强是指拦截到Joinpoint之后所做的事情就是通知
例如:在拦截到User类中的add方法可以添加日志功能,日志功能就是增强
通知存在5种:前置通知、后置通知、异常通知、最终通知、环绕通知
前置通知:在方法之前执行
后置通知:在方法之后执行
异常通知:在方法出现异常通知
最终通知:在后置通知之后执行
环绕通知:在方法之前和之后执行

Aspect(切面):是切入点和通知的结合
将增强应用到切入点的过程

AOP的使用:
在SpringAOP的使用是通过Aspectj实现
Aspectj是一个基于JAVA语言的AOP框
Aspectj不是spring中的一部分
spring中实现AOP需要结合Aspectj一起使用

Spring中AOP实现有两种方式:
基于配置文件实现
基于注解实现

AOP使用:
1.引入依赖

<!--AOP相关jar包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.0</version>
    </dependency>
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>

2.配置全局配置文件,引入aop的约束

<?xml version="1.0" encoding="UTF-8"?>
 
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd">
 
     <!--
     配置切入点:实际被增强的方法称之为切入点
     如何配置:通过表达式来配置切入点
     常用表达式:
     execution(<访问限定符> <返回类型> <方法名>(<参数>) <异常>)
 
     1execution(* com.tulun.bean6.Student.Study(..)) 表示类中的某一个方法
     2execution(* com.tulun.bean6.Student.*(..)) 表示某一个类中的所有的方法
     3execution(* *.*(..))   表示所有
 
     例:
     -匹配所有类public方法 execution(public *.*(..))
     -匹配指定包下所有类方法 execution(* com.tulun.bean.*(..)) (不包含子包)
     - execution(* com.tulun.bean..*(..)) (包含包、子包下所有类)
     -匹配指定类所有方法 execution(* com.tulun.bean.Book.*(..))
     -匹配实现特定接口所有类方法 execution(* com.tulun.bean.Book+.*(..))
     -匹配所有com开头的方法 execution(* com*(..))
     -->
 
</beans>   

基于配置方式实现

被增强类

public class Student {
    public void Study() {
        //业务逻辑
        System.out.println("Student.Study...");
    }
}

增强类

/**
 * 增强
 */
public class Log {
    //前置增强
    public void before() {
        //日志功能
        System.out.println("Log.before...");
    }

    //环绕通知
    public void Around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知之前执行");
        //真正调用切入点方法
        joinPoint.proceed();
        System.out.println("环绕通知之后执行");
    }
}

在配置文件中配置增强处理

 <!--student交给spring管理-->
    <bean id="student" class="com.tulun.bean6.Student"/>
    <!--增强类-->
    <bean id="log" class="com.tulun.bean6.Log"/>



    <!--配置AOP操作-->
    <aop:config>
        <!--配置切入点:使用execution表达式-->
        <aop:pointcut id="pointcutq" expression="execution(* com.tulun.bean6.Student.Study(..))"/>

        <!--
        配置切面:将增强应用到切入点的过程
        ref:指定增强类的引用
        -->
        <aop:aspect ref="log" >
            <!--
            配置增强类型method
            aop:before:前置通知标签
            aop:around:环绕通知标签
            aop:after-returning:最终通知标签
            aop:after:后置通知标签
            aop:after-throwing:异常通知标签
            method属性:表示增强类中的方法名
            pointcut-ref:切入点对应引用
            -->
            <!--<aop:after method="before" pointcut-ref="pointcutq"/>-->
            <aop:around method="Around" pointcut-ref="pointcutq"/>
        </aop:aspect>
    </aop:config>

基于注解实现

1、将对象交给spring管理

 <!--student交给spring管理-->
    <bean id="student" class="com.tulun.bean.Student"/>
    <!--增强类-->
    <bean id="log" class="com.tulun.bean.Log"/>

2、在配置文件中打开AOP注解配置

<!--开启AOP注解配置-->
 <aop:aspectj-autoproxy/>

3、在增强类中添加注解

//在增强类上添加@Aspect注解
@Aspect
public class Log {
    //配置前置通知使用@Before注解,在该注解中配置切入点表示式
    @Before(value = "execution(* com.tulun.bean.Student.Study(..))")
    public void before() {
        //日志功能
        System.out.println("Log.before...");
    }
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值