The simple steps of aop are as follows, in fact, there are three simple steps:
Preparatory steps: import Maven dependencies, spring configuration files, and read configuration files in main
Aspect class configuration
Entry point configuration
Cut-in notification configuration
Maven dependencies
Common maven dependencies are as follows
<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 configuration file
<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 usage: Method cut-in based on annotations
Method entry requires the following steps
Create facet classes ,
Specify the pointcut configuration (target method) through the pointcut() empty method
Create the cut-in notification configuration in the aspect class , and perform method cut-in on the point-cut point
Aspect class: the class where the cut-in method is located, specified by @Aspect
1. Entry point:
Specify the target method to cut in, configuration syntax
@Pointcut("execution(* com.itheima.aop..*.*(..)))")
symbol |
meaning |
First * |
Indicates the type of return value, * refers to any type |
com.itheima.aop.. |
The package name of the target method.. refers to this package and sub-packages. refers to this package |
the second * |
Indicates the class name, * means all classes. |
The third * |
Indicates the method name, * means all methods. |
(..) |
Indicates parameter type.. Indicates any parameter type |
2. Cut-in notification : cut-in method to each state of the point-cut point
state contains
@After("pointcut()") |
post notification |
After the pointcut method is executed, execute this method |
@Before("entry point()") |
advance notice |
Execute this method before the pointcut method executes |
@AfterReturning("entry point()") |
return notice |
After the pointcut method returns a value, execute this method |
@AfterThrowing("entry point()") |
Exception notification |
Execute this method after an exception occurs in the pointcut method |
@Around("entry point()") |
surround notification |
通过ProceddingJoinPoint参数在目标方法任何位置切入方法 |
通知内常用参数
ProcedingJoinPoint 正在执行的目标方法
JoinPoint 目标方法
aop使用:基于注解进行方法切入
方法切入需要以下几步
创建切面类,
通过<aop:pointcut>指定切入点(目标方法)
通过<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"> |
环绕通知 |
在目标方法任何位置切入方法 |