ClassFilter优化

master
linlei 2024-04-26 15:14:08 +08:00
parent 4fdff4ada6
commit 38cf77a510
4 changed files with 43 additions and 27 deletions

View File

@ -113,44 +113,60 @@ TypePatternClassFilter ..> ClassFilter
### 七、最佳实践 ### 七、最佳实践
使用不同类型的类过滤器在 Spring AOP 中的使用方式。我们创建了四种不同的类过滤器实例,并测试它们是否匹配了特定的类。通过打印输出结果,展示了每个类过滤器的匹配情况,从而说明了它们在过滤目标类方面的作用 使用不同类型的类过滤器AnnotationClassFilter、TypePatternClassFilter、RootClassFilter以及基于 AspectJ 表达式的切点AspectJExpressionPointcut来匹配目标类并输出匹配结果
```java ```java
public class ClassFilterDemo { public class ClassFilterDemo {
public static void main(String[] args) { public static void main(String[] args) {
// 创建 AnnotationClassFilter 实例,匹配带有 MyAnnotation 注解的类 // 创建 AnnotationClassFilter 实例,匹配带有 MyAnnotation 注解的类
ClassFilter filter1 = new AnnotationClassFilter(MyAnnotation.class); ClassFilter annotationClassFilter = new AnnotationClassFilter(MyClassAnnotation.class);
System.out.println("AnnotationClassFilter 是否匹配 MyService 类:" + filter1.matches(MyService.class)); System.out.println("annotationClassFilter matches =" + annotationClassFilter.matches(MyService.class));
// 创建 TypePatternClassFilter 实例,匹配指定类名的类 // 创建 TypePatternClassFilter 实例,匹配指定类名的类
ClassFilter filter2 = new TypePatternClassFilter("com.xcs.spring.MyService"); ClassFilter typePatternClassFilter = new TypePatternClassFilter("com.xcs.spring.MyService");
System.out.println("TypePatternClassFilter 是否匹配 MyService 类:" + filter2.matches(MyService.class)); System.out.println("typePatternClassFilter matches =" + typePatternClassFilter.matches(MyService.class));
// 创建 RootClassFilter 实例,匹配指定类的根类 // 创建 RootClassFilter 实例,匹配指定类的根类
ClassFilter filter3 = new RootClassFilter(MyService.class); ClassFilter rootClassFilter = new RootClassFilter(MyService.class);
System.out.println("RootClassFilter 是否匹配 MySubService 的根类:" + filter3.matches(MySubService.class)); System.out.println("rootClassFilter matches = " + rootClassFilter.matches(MySubService.class));
// 创建 AspectJExpressionPointcut 实例,根据 AspectJ 表达式匹配类和方法 // 创建 AspectJExpressionPointcut 实例,根据 AspectJ 表达式匹配类和方法
AspectJExpressionPointcut filter4 = new AspectJExpressionPointcut(); AspectJExpressionPointcut aspectJExpressionPointcut = new AspectJExpressionPointcut();
filter4.setExpression("execution(* com.xcs.spring.MyService.*(..))"); aspectJExpressionPointcut.setExpression("execution(* com.xcs.spring.MyService.*(..))");
System.out.println("AspectJExpressionPointcut 是否匹配 MyService 类:" + filter4.matches(MyService.class)); System.out.println("aspectJExpressionPointcut matches = " + aspectJExpressionPointcut.matches(MyService.class));
} }
} }
``` ```
`MyService` 类被 `@MyClassAnnotation` 注解修饰。
```java
@MyClassAnnotation
public class MyService {
}
```
`MyClassAnnotation` 注解,应用于类级别的元素。
```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyClassAnnotation {
}
```
运行结果,四种不同类型的类过滤器都成功地匹配了相应的目标类。 运行结果,四种不同类型的类过滤器都成功地匹配了相应的目标类。
```java ```java
AnnotationClassFilter 是否匹配 MyService 类true annotationClassFilter matches =true
TypePatternClassFilter 是否匹配 MyService 类true typePatternClassFilter matches =true
RootClassFilter 是否匹配 MySubService 的根类true rootClassFilter matches = true
AspectJExpressionPointcut 是否匹配 MyService 类true aspectJExpressionPointcut matches = true
``` ```
### 八、常见问题 ### 八、常见问题
1. **匹配准确性** 1. **匹配准确性**
+ 可能会出现由于匹配规则不准确导致无法正确匹配目标类的情况。在使用 `ClassFilter` 时,需要确保定义的匹配规则能够准确地选择出目标类,否则可能会导致切面不正确地应用或不应用于预期的类。 + 可能会出现由于匹配规则不准确导致无法正确匹配目标类的情况。在使用 `ClassFilter` 时,需要确保定义的匹配规则能够准确地选择出目标类,否则可能会导致切面不正确地应用或不应用于预期的类。
2. **匹配范围不一致** 2. **匹配范围不一致**

View File

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

View File

@ -7,5 +7,5 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
public @interface MyAnnotation { public @interface MyClassAnnotation {
} }

View File

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