Fork me on GitEE

自定義注解+el表達式


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Notification{
@AliasFor("userId")
String userId();
@AliasFor("projectId")
String projectId();
TriggerMethodEnum[] code();
}
@Pointcut("@annotation(Notification)")
public void pointcut() {

}

@Around("pointcut()")
public void around(ProceedingJoinPoint point ) throws Throwable {
String targetName = point.getTarget().getClass().getName();
String simpleName = point.getTarget().getClass().getSimpleName();
String methodName = point.getSignature().getName();
Object[] arguments = point.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String projectId = "";
String userId="";
String[] paramNames = {};
for(Method method:methods){
if(method.getName().equals(methodName)){
projectId = method.getAnnotation(Notification.class).projectId();
userId = method.getAnnotation(Notification.class).userId();
System.out.println("projectId is "+projectId+"userId is "+userId);

paramNames = getParamterNames(method);
}
}
ExpressionParser parser = new SpelExpressionParser();
Expression projectIdExp = parser.parseExpression(projectId);
Expression userIdExp = parser.parseExpression(userId);
EvaluationContext context = new StandardEvaluationContext();
for(int i=0;i<arguments.length;i++){
context.setVariable(paramNames[i],arguments[i]);
}
System.out.println(projectIdExp.getValue(context,String.class));
System.out.println(userIdExp.getValue(context,String.class));
point.proceed();
}
posted @ 2021-06-29 11:38  问道于盲  阅读(137)  评论(0编辑  收藏  举报