Spring Expression Language in custom annotation

前端 未结 1 901
一生所求
一生所求 2020-12-31 11:56

I want to use Spring Expression Language in a custom Annotation. This annotation will be consumed by a custom Aspect.

Check this out:

@StatisticEvent         


        
相关标签:
1条回答
  • 2020-12-31 12:52

    I figured it out how to do that with SpEL. I used following expression:

    @EventTrigger(value = EventTestModel.class, expression = "new Object[]{arguments[1], arguments[2]}")
    

    and following Java code:

    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext(expressionEvaluationTarget);
    Object[] eventPayloadModels = parser.parseExpression(expression).getValue(context);
    

    Works fine ;)

    UPDATE

    Here is a complete code example now:

    import com.example.AbstractEvent;
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    import org.apache.log4j.Logger;
    import org.springframework.expression.ExpressionParser;
    import org.springframework.expression.spel.standard.SpelExpressionParser;
    import org.springframework.expression.spel.support.StandardEvaluationContext;
    import org.springframework.util.StringUtils;
    
    public class AnnotationEventTriggerAspect implements MethodInterceptor {
    private static final Logger LOG = Logger.getLogger(AnnotationEventTriggerAspect.class);
    
    private IEventHandler eventHandler;
    
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Invoking event event trigger for method: " + invocation.getMethod().getDeclaringClass() + "." + invocation.getMethod().getName());
        }
    
        EventTrigger annotation = invocation.getMethod().getAnnotation(EventTrigger.class);
        Class<? extends AbstractEvent> eventClass = annotation.value();
    
        if (eventClass == null) {
            throw new IllegalArgumentException("No event class provided for event event trigger annotation at method " + invocation.getMethod().getDeclaringClass() + "." + invocation.getMethod());
        }
    
        AbstractEvent event = eventClass.newInstance();
        String expression = annotation.expression();
    
        Object result = invocation.proceed();
        Object expressionEvaluationTarget = invocation;
        Object eventPayloadModels = invocation.getArguments();
    
        if (annotation.useResult()) {
            expressionEvaluationTarget = result;
            eventPayloadModels = new Object[]{result};
        }
    
        if (StringUtils.hasText(expression)) {
            ExpressionParser parser = new SpelExpressionParser();
            StandardEvaluationContext context = new StandardEvaluationContext(expressionEvaluationTarget);
            eventPayloadModels = parser.parseExpression(expression).getValue(context);
        }
    
        if (eventPayloadModels != null) {
            if (eventPayloadModels.getClass().isArray()) {
                for (Object arg : (Object[]) eventPayloadModels) {
                    event.addPayloadModel(arg);
                }
            } else {
                event.addPayloadModel(eventPayloadModels);
            }
        }
    
        eventHandler.post(event);
    
        return result;
    }
    
    public IEventHandler getEventHandler() {
        return eventHandler;
    }
    
    public void setEventHandler(IEventHandler eventHandler) {
        this.eventHandler = eventHandler;
    }
    

    }

    0 讨论(0)
提交回复
热议问题