ClassFilter源码分析

master
linlei 2024-04-10 10:34:59 +08:00
parent 05d31a0d79
commit fcb71f1c3d
9 changed files with 247 additions and 0 deletions

View File

@ -197,6 +197,7 @@
+ Spring AOP
- [AopProxy](spring-aop/spring-aop-aopProxy/README.md)创建和管理AOP代理对象。<img src="https://img.shields.io/badge/Level-%E7%AE%80%E5%8D%95-0099ff"></img>
- [ClassFilter](spring-aop/spring-aop-classFilter/README.md)用于指定Spring AOP切面应拦截的目标类。<img src="https://img.shields.io/badge/Level-%E7%AE%80%E5%8D%95-0099ff"></img>
- [Pointcut](spring-aop/spring-aop-pointcut/README.md):定义切入点,匹配被拦截的方法。<img src="https://img.shields.io/badge/Level-%E7%AE%80%E5%8D%95-0099ff"></img>
- [Advice](spring-aop/spring-aop-advice/README.md)AOP核心接口定义切面通知行为。<img src="https://img.shields.io/badge/Level-%E4%B8%80%E8%88%AC-%23FF6347"></img>
- [Advisor](spring-aop/spring-aop-advisor/README.md):用于将通知和切点结合,实现切面编程的横切关注点。<img src="https://img.shields.io/badge/Level-%E7%AE%80%E5%8D%95-0099ff"></img>

View File

@ -15,6 +15,8 @@
<module>spring-aop-advisorAdapter</module>
<module>spring-aop-targetSource</module>
<module>spring-aop-aopProxy</module>
<module>spring-aop-classFilter</module>
<module>spring-aop-methodMatcher</module>
</modules>
<modelVersion>4.0.0</modelVersion>

View File

@ -0,0 +1,167 @@
## ClassFilter
- [ClassFilter](#ClassFilter)
- [一、基本信息](#一基本信息)
- [二、知识储备](#二知识储备)
- [三、基本描述](#三基本描述)
- [四、主要功能](#四主要功能)
- [五、接口源码](#五接口源码)
- [六、主要实现](#六主要实现)
- [七、最佳实践](#七最佳实践)
- [八、与其他组件的关系](#八与其他组件的关系)
- [九、常见问题](#九常见问题)
### 一、基本信息
✒️ **作者** - Lex 📝 **博客** - [掘金](https://juejin.cn/user/4251135018533068/posts) 📚 **源码地址** - [github](https://github.com/xuchengsheng/spring-reading)
### 二、知识储备
1. **过滤器模式**
+ `ClassFilter` 的实现类,每个实现类负责不同的过滤逻辑,然后将这些过滤器应用到切面中。当 Spring AOP 拦截到方法调用时,会通过这些过滤器来决定是否应该应用切面的通知。
2. **AspectJ 表达式语言**
+ 了解 AspectJ 表达式语言的基本语法和规则,这是 Spring AOP 中定义切点的一种方式,可以用来匹配方法调用的目标。
### 三、基本描述
`ClassFilter` 接口是 Spring AOP 框架中的一个关键组件用于定义切面Aspect应该拦截哪些类的规则。允许我们根据具体的条件来判断传入的类是否应该被拦截。通过实现该接口可以灵活地定义过滤器以匹配特定的类或者类的集合从而精确地控制切面的作用范围。
### 四、主要功能
1. **指定切面拦截的类**
+ 允许我们定义规则,确定哪些类应该被应用切面。通过实现 `matches(Class<?> clazz)` 方法,可以根据特定的条件来判断传入的类是否应该被拦截。
2. **过滤器功能**
+ 作为过滤器模式的一种应用,`ClassFilter` 接口允许我们定义过滤器,以匹配特定的类或者类的集合。这样可以灵活地控制切面的作用范围,只针对符合条件的类应用切面逻辑。
3. **精确定义切面作用范围**
+ 通过 `ClassFilter` 接口,可以实现非常灵活的切面选择逻辑,例如只拦截某个特定包下的类、只拦截实现了某个接口的类等,从而精确地定义切面的作用范围。
### 五、接口源码
`ClassFilter` 接口是一个过滤器,用于限制某个切点或引入的匹配范围到一组指定的目标类。通过实现 `matches(Class<?> clazz)` 方法,可以确定切面是否应该应用到给定的目标类上。
```java
/**
* 过滤器,用于限制一个切点或引入的匹配到一组给定的目标类。
*
* <p>可以作为 {@link Pointcut} 的一部分或者用于整个 {@link IntroductionAdvisor} 的定位。
*
* <p>这个接口的具体实现通常应该提供 {@link Object#equals(Object)} 和 {@link Object#hashCode()} 的适当实现,
* 以便允许在缓存场景中使用过滤器,例如,在 CGLIB 生成的代理中。
*
* @author Rod Johnson
* @see Pointcut
* @see MethodMatcher
*/
@FunctionalInterface
public interface ClassFilter {
/**
* 是否应该应用到给定的接口或目标类?
* @param clazz 候选目标类
* @return 是否应该将通知应用到给定的目标类
*/
boolean matches(Class<?> clazz);
/**
* 匹配所有类的 ClassFilter 的规范实例。
*/
ClassFilter TRUE = TrueClassFilter.INSTANCE;
}
```
### 六、主要实现
1. **AnnotationClassFilter**
- 根据注解匹配类的过滤器,用于选取带有指定注解的类。
2. **TypePatternClassFilter**
+ 根据类型模式匹配类的过滤器,用于匹配满足指定类型模式的类。
3. **RootClassFilter**
+ 匹配指定类的根类的过滤器。
4. **AspectJExpressionPointcut**
+ 主要用于基于 AspectJ 表达式匹配目标类。
### 七、最佳实践
使用不同类型的类过滤器在 Spring AOP 中的使用方式。我们创建了四种不同的类过滤器实例,并测试它们是否匹配了特定的类。通过打印输出结果,展示了每个类过滤器的匹配情况,从而说明了它们在过滤目标类方面的作用。
```java
public class ClassFilterDemo {
public static void main(String[] args) {
// 创建 AnnotationClassFilter 实例,匹配带有 MyAnnotation 注解的类
ClassFilter filter1 = new AnnotationClassFilter(MyAnnotation.class);
System.out.println("AnnotationClassFilter 是否匹配 MyService 类:" + filter1.matches(MyService.class));
// 创建 TypePatternClassFilter 实例,匹配指定类名的类
ClassFilter filter2 = new TypePatternClassFilter("com.xcs.spring.MyService");
System.out.println("TypePatternClassFilter 是否匹配 MyService 类:" + filter2.matches(MyService.class));
// 创建 RootClassFilter 实例,匹配指定类的根类
ClassFilter filter3 = new RootClassFilter(MyService.class);
System.out.println("RootClassFilter 是否匹配 MySubService 的根类:" + filter3.matches(MySubService.class));
// 创建 AspectJExpressionPointcut 实例,根据 AspectJ 表达式匹配类和方法
AspectJExpressionPointcut filter4 = new AspectJExpressionPointcut();
filter4.setExpression("execution(* com.xcs.spring.MyService.*(..))");
System.out.println("AspectJExpressionPointcut 是否匹配 MyService 类:" + filter4.matches(MyService.class));
}
}
```
运行结果,四种不同类型的类过滤器都成功地匹配了相应的目标类。
```java
AnnotationClassFilter 是否匹配 MyService 类true
TypePatternClassFilter 是否匹配 MyService 类true
RootClassFilter 是否匹配 MySubService 的根类true
AspectJExpressionPointcut 是否匹配 MyService 类true
```
### 八、与其他组件的关系
1. **Pointcut**
+ 用于定义切点应该匹配哪些目标类。`Pointcut` 是定义切面影响范围的关键组件,可以包含一个或多个 `ClassFilter`
2. **Advisor**
+ 用于限制通知器的作用范围。`Advisor` 是切面的一部分,包含切点和通知器。通过在 `Advisor` 中使用 `ClassFilter`,可以指定通知器应该应用于哪些目标类。
3. **IntroductionAdvisor**
+ 引介通知器允许向目标类引入新的接口和属性。`ClassFilter` 可以用于 `IntroductionAdvisor` 中,以确定哪些目标类应该接受引介。
4. **ProxyFactory**
+ 在使用 Spring AOP 时,可以通过 `ProxyFactory` 或其他类似的工厂来创建代理对象。`ProxyFactory` 可以通过设置 `ClassFilter` 来控制哪些目标类应该被代理,以及哪些不应该。
5. **AspectJExpressionPointcut**
+ 基于 AspectJ 表达式匹配类和方法的一种切点实现。它也可以使用 `ClassFilter` 来限制匹配的目标类。
### 九、常见问题
1. **匹配准确性**
+ 可能会出现由于匹配规则不准确导致无法正确匹配目标类的情况。在使用 `ClassFilter` 时,需要确保定义的匹配规则能够准确地选择出目标类,否则可能会导致切面不正确地应用或不应用于预期的类。
2. **匹配范围不一致**
+ 在定义 `ClassFilter` 时,可能会出现匹配范围不一致的情况。例如,一个切面匹配了目标类的所有方法,而另一个切面只匹配了部分方法。在这种情况下,可能会出现不一致的行为,需要确保所有切面的匹配范围是一致的。

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.xcs.spring</groupId>
<artifactId>spring-aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-aop-classFilter</artifactId>
</project>

View File

@ -0,0 +1,28 @@
package com.xcs.spring;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.TypePatternClassFilter;
import org.springframework.aop.support.RootClassFilter;
import org.springframework.aop.support.annotation.AnnotationClassFilter;
public class ClassFilterDemo {
public static void main(String[] args) {
// 创建 AnnotationClassFilter 实例,匹配带有 MyAnnotation 注解的类
ClassFilter filter1 = new AnnotationClassFilter(MyAnnotation.class);
System.out.println("AnnotationClassFilter 是否匹配 MyService 类:" + filter1.matches(MyService.class));
// 创建 TypePatternClassFilter 实例,匹配指定类名的类
ClassFilter filter2 = new TypePatternClassFilter("com.xcs.spring.MyService");
System.out.println("TypePatternClassFilter 是否匹配 MyService 类:" + filter2.matches(MyService.class));
// 创建 RootClassFilter 实例,匹配指定类的根类
ClassFilter filter3 = new RootClassFilter(MyService.class);
System.out.println("RootClassFilter 是否匹配 MySubService 的根类:" + filter3.matches(MySubService.class));
// 创建 AspectJExpressionPointcut 实例,根据 AspectJ 表达式匹配类和方法
AspectJExpressionPointcut filter4 = new AspectJExpressionPointcut();
filter4.setExpression("execution(* com.xcs.spring.MyService.*(..))");
System.out.println("AspectJExpressionPointcut 是否匹配 MyService 类:" + filter4.matches(MyService.class));
}
}

View File

@ -0,0 +1,11 @@
package com.xcs.spring;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
}

View File

@ -0,0 +1,5 @@
package com.xcs.spring;
@MyAnnotation
public class MyService {
}

View File

@ -0,0 +1,4 @@
package com.xcs.spring;
public class MySubService extends MyService{
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.xcs.spring</groupId>
<artifactId>spring-aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-aop-methodMatcher</artifactId>
</project>