Spring——AOP

AOP概念

AOP(Aspect Oriented Programming)面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容。

AOP的作用

AOP的核心作用是:在程序运行期间,不修改代码的同时为程序增强功能。将必不可少的公共功能做成切面,随着程序运行切入到代码中运行。编写业务时只关注于核心功能 ,不再考虑事务、日志等公共功能,减轻了编码负担,更专注于业务。

AOP的术语

  • 切面

对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为切面

  • 连接点(joinpoint)

被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

  • 切入点(pointcut)

对连接点进行拦截的定义

  • 通知(advice)

所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

  • 目标对象(target)

代理的目标对象,将切面应用到目标对象并导致代理对象创建的过程

  • 引入\织入(introduction、weave)

在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

AOP配置

引入依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.5</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>

编写通知类

/**
 * 日志输出通知类
 */
public class LogAdvise {

    public void beforeLog(){
        System.out.println("方法开始执行!");
    }

    public void afterLog(){
        System.out.println("方法后置执行!");
    }

    public void afterReturning(){
        System.out.println("方法返回了数据");
    }

    public void afterThrowing(){
        System.out.println("方法抛出了异常");
    }

    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around方法名:" + joinPoint.getSignature().getName());
        System.out.println("around --前置");
        //原来方法
        joinPoint.proceed();
        System.out.println("around --后置");
    }
}
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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置包的扫描-->
    <context:component-scan base-package="com.blb.aop_demo"></context:component-scan>
    <!--配置通知类-->
    <bean id="logAdvise" class="com.blb.aop_demo.util.LogAdvise"></bean>
    <!--配置切面-->
    <aop:config>
        <!--配置切入点-->
        <aop:pointcut id="pc" expression="execution(* com.blb.aop_demo.service.*Service.*(..))"/>
        <!--配置切面 ref是通知类的bean-->
        <aop:aspect id="aspect1" ref="logAdvise">
            <!--前置通知 method是对应的通知方法 pointcut-ref是切入点-->
            <aop:before method="beforeLog" pointcut-ref="pc"></aop:before>
            <!--后置-->
            <aop:after method="afterLog" pointcut-ref="pc"></aop:after>
            <!--后置返回-->
            <aop:after-returning method="afterReturning" pointcut-ref="pc"></aop:after-returning>
            <!--后置抛异常-->
            <aop:after-throwing method="afterThrowing" pointcut-ref="pc"></aop:after-throwing>
            <!--环绕-->
            <aop:around method="around" pointcut-ref="pc"></aop:around>
        </aop:aspect>
    </aop:config>
</beans>

配置详解

aop:pointcut 是切入点配置
其中的核心是expression,通过表达式控制切面应用的范围
语法:

execution(访问修饰符 返回值 包名.类名.方法名(参数类型,参数类型…))

通配符:

代表任意长度的字符
代替子包或任意参数

常用AOP相关注解

  • @Aspect 切面,配置到切面类上
  • @PointCut(“表达式”) 配置切入点,加在方法上
  • @Before 配置前置通知方法
  • @After 配置后置通知方法
  • @Around 配置环绕通知方法
  • @AfterReturning 配置后置返回值通知方法
  • @AfterThrowing 配置后置抛出异常通知方法
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值