自定義注解+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();
}

自定義注解+el表達式

上一篇:项目更改版本号之后打包失败(resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced)


下一篇:常见Cpu 100%的原因