Spring源码之AOP操作案例

94 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第11天,点击查看活动详情

1 AspectJ 引入

Spring 框架一般都是基于 AspectJ 实现 AOP 操作, AspectJ不是Spring的组成部分,独立AOP框架, 把两者一起使用,进行AOP操作

2 AspectJ操作的实现方式

1 基于xml配置文件实现

2 基于注解方式实现

3 切点表达式

1 说明

切点表达式: 知道对那个类中的那个方法进行增强

2 语法

execution([权限修饰符] [返回类型] [类全路径] [方法名称] ([参数列表]) )

例如:

对 com.cf.dao.BookDao 类里面的 add 进行增强

execution(* com.cf.dao.BookDao.add(..))

4 AspectJ 注解方式

1 创建普通类

public class User {
 public void add() {
 System.out.println("add.......");
 }
}

2 增强类

// 增强的类
public class UserProxy {
 public void before() {
 // 前置通知
 System.out.println("before......");
 }
}

3 通知配置

1 在spring配置文件中,开启扫描

<?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 
http://www.springframework.org/schema/context/spring-context.xsd 
 http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd">
 <!-- 开启注解扫描 -->
 <context:component-scan base-package="com.cf.spring5.aopanno"></context:component-scan>

2 使用注解创建User和UserProxy对象

普通类

// 被增强的类
@Component
public class User{
    // ...
}

增强类

// 增强的类
@Component
public class UserProxy{
    // ...
}

3 在增强类上添加注解@Aspect

// 增强的类
@Component
@Aspect // 生成代理对象
public class UserProxy{
    // ...
}

4 在spring配置文件中开启生成代理对象

<!-- 开启 Aspect 生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

4 配置不同类型的通知

在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置

// 增强的类
@Component
@Aspect // 生成代理对象
public class UserProxy {
 // 前置通知
 // @Before 注解表示作为前置通知
 @Before(value = "execution(* com.cf.spring5.aopanno.User.add(..))")
 public void before() {
 System.out.println("before.........");
 }
 // 后置通知(返回通知)
 @AfterReturning(value = "execution(* 
com.cf.spring5.aopanno.User.add(..))")
 public void afterReturning() {
 System.out.println("afterReturning.........");
 }
 // 最终通知
 @After(value = "execution(* com.cf.spring5.aopanno.User.add(..))")
 public void after() {
 System.out.println("after.........");
 }
 // 异常通知
 @AfterThrowing(value = "execution(* 
com.cf.spring5.aopanno.User.add(..))")
 public void afterThrowing() {
 System.out.println("afterThrowing.........");
 }
 // 环绕通知
 @Around(value = "execution(* com.cf.spring5.aopanno.User.add(..))")
 public void around(ProceedingJoinPoint proceedingJoinPoint) throws 
Throwable {
 System.out.println("环绕之前.........");
 // 被增强的方法执行
 proceedingJoinPoint.proceed();
 System.out.println("环绕之后.........");
 }
}

5 相同切入点提取

// 相同切入点抽取
@Pointcut(value = "execution(* com.cf.spring5.aopanno.User.add(..))")
public void pointdemo() {
}

// 前置通知
// @Before 注解表示作为前置通知
@Before(value = "pointdemo()")
public void before() {
 System.out.println("before.........");
}

6 设置增强类优先级

在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越高.

@Component
@Aspect
@Order(1)
public class PersonProxy{
}

7 全注解开发

创建配置类,不需要额外xml配置文件

@Configuration
@ComponentScan(basePackages = {"com.cf"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {
}

5 AspectJ 配置文件

1 创建增强类和被增强类

1 被增强类

public class Book{
    // ...
}

2 增强类

public class BookProxy{
    // ...
}

2 在spring配置文件中创建类对象

<!--创建对象-->
<bean id="book" class="com.cf.spring5.aopxml.Book"></bean>
<bean id="bookProxy" class="com.cf.spring5.aopxml.BookProxy"></bean>

3 在spring配置文件中配置切入点

<!--配置 aop 增强-->
<aop:config>
 <!--切入点-->
 <aop:pointcut id="p" expression="execution(* 
com.cf.spring5.aopxml.Book.buy(..))"/>
 <!--配置切面-->
 <aop:aspect ref="bookProxy">
 <!--增强作用在具体的方法上-->
 <aop:before method="before" pointcut-ref="p"/>
 </aop:aspect>
</aop:config>