Spring-aop的基本使用

aop的简单步骤如下,其实就简单三步:

准备步骤:导入Maven依赖,spring配置文件,main中读取配置文件

  1. 切面类配置

  1. 切入点配置

  1. 切入通知配置

Maven依赖

常用maven依赖如下

        <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.21</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.3.21</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.21</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.21</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.21</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.21</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.3.21</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.42</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

ioc配置文件

<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-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 https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--    使用基于注解的包扫描器-->
    <context:component-scan base-package="com.itheima"/>
    <!--    开启基于注解的aop-->
    <aop:aspectj-autoproxy/>

aop使用:基于注解进行方法切入

方法切入需要以下几步

  1. 创建切面类,

  1. 通过pointcut()空方法指定切入点配置(目标方法)

  1. 创建切面类内的切入通知配置,对切入点进行方法切入

切面类:切入方法所在类,通过@Aspect指定

1.切入点:

指定要切入的目标方法,配置语法

@Pointcut("execution(* com.itheima.aop..*.*(..)))")

符号

含义

第一个 *

表示返回值的类型,*指任何类型

com.itheima.aop..

目标方法所在包名 ..指此包及子包下 .指此包下

第二个 *

表示类名,*即所有类。

第三个 *

表示方法名,*即所有方法。

(..)

表示参数类型 ..表示任何参数类型

2.切入通知:向切入点的各个状态下切入方法

状态包含

@After("切入点()")

后置通知

在切入点方法执行完后,执行此方法

@Before("切入点()")

前置通知

在切入点方法执行之前,执行此方法

@AfterReturning("切入点()")

返回通知

在切入点方法返回值后,执行此方法

@AfterThrowing("切入点()")

异常通知

在切入点方法发生异常后,执行此方法

@Around("切入点()")

环绕通知

通过ProceddingJoinPoint参数在目标方法任何位置切入方法

通知内常用参数

ProcedingJoinPoint 正在执行的目标方法

JoinPoint 目标方法

aop使用:基于注解进行方法切入

方法切入需要以下几步

  1. 创建切面类,

  1. 通过<aop:pointcut>指定切入点(目标方法)

  1. 通过<aop:before>等 创建切面类内的切入通知,对切入点进行方法切入

<!--    1.创建切面类-->
<bean id="aopTestObject" class="com.itheima.aop.AopTestObject"></bean>

<aop:config>
    <!--        2.切入点配置-->
    <aop:pointcut id="pointcut01" expression="execution(* com.itheima.aop..*.*(..))"/>

    <aop:aspect ref="aopTestObject">
        <!--            3.切入通知配置-->
        <aop:after method="需要指定切面类内的方法" pointcut-ref="pointcut01"/>
        <aop:before method="需要指定切面类内的方法" pointcut-ref="pointcut01"/>

    </aop:aspect>
</aop:config>

1.切入点:

指定要切入的目标方法,配置语法

"execution(* com.itheima.aop..*.*(..)))"

符号

含义

第一个 *

表示返回值的类型,*指任何类型

com.itheima.aop..

目标方法所在包名 ..指此包及子包下 .指此包下

第二个 *

表示类名,*即所有类。

第三个 *

表示方法名,*即所有方法。

(..)

表示参数类型 ..表示任何参数类型

2.切入通知:向切入点的各个状态下切入方法

状态包含

<aop:after pointcut-ref="切入点id">

后置通知

在切入点方法执行完后,执行此方法

<aop:before pointcut-ref="切入点id">

前置通知

在切入点方法执行之前,执行此方法

<aop:after-returning pointcut-ref="切入点id">

返回通知

在切入点方法返回值后,执行此方法

<aop:after-throwing pointcut-ref="切入点id">

异常通知

在切入点方法发生异常后,执行此方法

<aop:around pointcut-ref="切入点id">

环绕通知

在目标方法任何位置切入方法

其他提示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值