feat(youlai-laboratory-spring): 自定义切点匹配规则

自定义切点匹配规则
This commit is contained in:
zc 2022-03-28 22:58:15 +08:00
parent e6e47dca73
commit 1ab1d47a60
3 changed files with 77 additions and 12 deletions

View File

@ -3,8 +3,13 @@ package com.youlai.laboratory.spring.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.Method;
/**
* 切点的使用
*
@ -134,6 +139,8 @@ public class MyPointcut {
* @return
*/
public boolean Ibean(){
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("bean(pointcutService)");
return doMatches("bean(pointcutService)");
}
@ -141,7 +148,52 @@ public class MyPointcut {
private boolean doMatches(String expression){
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(expression);
return pointcut.matches(PointcutService.class);
try {
System.out.println("PointcutService.m1:"+expression+":"+pointcut.matches(PointcutService.class.getMethod("m1"), PointcutService.class));
System.out.println("PointcutService.m2:"+expression+":"+pointcut.matches(PointcutService.class.getMethod("m2"), PointcutService.class));
System.out.println("PointcutService.m3:"+expression+":"+pointcut.matches(PointcutService.class.getMethod("m3", String.class), PointcutService.class));
System.out.println("PointcutService.m4:"+expression+":"+pointcut.matches(PointcutService.class.getMethod("m4"), PointcutService.class));
System.out.println("-----------------------------------------------------");
return true;
} catch (NoSuchMethodException e) {
e.printStackTrace();
return false;
}
}
public void staticMethodMatcherPointcut(){
StaticMethodMatcherPointcut matcherPointcut = new StaticMethodMatcherPointcut() {
@Override
public boolean matches(Method method, Class<?> targetClass) {
MergedAnnotations annotations = MergedAnnotations.from(method);
//方法上是否有@transactional注解
if(annotations.isPresent(Transactional.class)){
return true;
}
annotations = MergedAnnotations.from(targetClass);
//类上是否有@transactional注解
if(annotations.isPresent(Transactional.class)){
return true;
}
annotations = MergedAnnotations.from(targetClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
//父类或实现的接口上是否有@transactional注解
if(annotations.isPresent(Transactional.class)){
return true;
}
return false;
}
};
try {
System.out.println("PointcutService.m1:"+matcherPointcut.matches(PointcutService.class.getMethod("m1"), PointcutService.class));
System.out.println("PointcutService.m2:"+matcherPointcut.matches(PointcutService.class.getMethod("m2"), PointcutService.class));
System.out.println("PointcutService.m3:"+matcherPointcut.matches(PointcutService.class.getMethod("m3", String.class), PointcutService.class));
System.out.println("PointcutService.m4:"+matcherPointcut.matches(PointcutService.class.getMethod("m4"), PointcutService.class));
System.out.println("-----------------------------------------------------");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}

View File

@ -1,11 +1,14 @@
package com.youlai.laboratory.spring.aop;
import org.springframework.transaction.annotation.Transactional;
/**
* 测试匹配的切点
*
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @Date 2022/3/27 0027 21:23
*/
public class PointcutService {
public void m1(){
@ -17,4 +20,14 @@ public class PointcutService {
return "OK";
}
public void m3(String args3){
System.out.println("已匹配包路径:com.youlai.laboratory.spring.aop,类名:PointcutService 方法名:m3,参数一个String,返回无");
}
@Transactional
public void m4(){
System.out.println("已匹配包路径:com.youlai.laboratory.spring.aop,类名:PointcutService 方法名:m4,参数无,返回无,一个@Transactional注解");
}
}

View File

@ -29,17 +29,17 @@ public class AopTests {
void execution(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyPointcut.class,PointcutService.class);
MyPointcut myPointcut = context.getBean(MyPointcut.class);
System.out.println(myPointcut.execution());
System.out.println(myPointcut.within());
System.out.println(myPointcut.thisI());
System.out.println(myPointcut.target());
System.out.println(myPointcut.args());
System.out.println(myPointcut.Itarget());
System.out.println(myPointcut.Iargs());
System.out.println(myPointcut.Iwithin());
System.out.println(myPointcut.Iannotation());
System.out.println(context.getBean("pointcutService"));
System.out.println(myPointcut.Ibean());
myPointcut.execution();
myPointcut.within();
myPointcut.thisI();
myPointcut.target();
myPointcut.args();
myPointcut.Itarget();
myPointcut.Iargs();
myPointcut.Iwithin();
myPointcut.Iannotation();
myPointcut.Ibean();
myPointcut.staticMethodMatcherPointcut();
}