首页 > 其他 > 详细

自定義注解+el表達式

时间:2021-06-29 13:38:17      阅读:13      评论:0      收藏:0      [点我收藏+]

@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();
}

自定義注解+el表達式

原文:https://www.cnblogs.com/blindjava/p/14949295.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!