本文已收录在Github,关注我,紧跟本系列专栏文章,咱们下篇再续!
public class MethodNameEvaluator {
// 判断方法名是否符合给定的SPEL表达式
public static boolean isMatch(String methodName, String spelExpression) {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("methodName", methodName);
return parser.parseExpression(spelExpression).getValue(context, Boolean.class);
}
public static void main(String[] args) {
String methodName = "getUserById";
// 匹配"get"开头,"Id"结尾方法名
String spelExpression = "#methodName.matches('get.*ById')";
boolean isMatched = isMatch(methodName, spelExpression);
// true
System.out.println(isMatched);
}
}