自定义注解中的弹簧表达式语言

2022-09-04 23:04:08

我想在自定义注释中使用Spring表达式语言。此注释将由自定义方面使用。

看看这个:

@StatisticEventTrigger(value = TestStatisticEvent.class, expression = "#p1")
public void someOtherMethod(String arg1, Long arg2) {

如您所见,我想使用表达式(在本例中)来检索一些特定的参数。

当我有我的Aspect时,触发一个带注释的方法,我想计算spring表达式(以编程方式)来检索一个值,用于进一步的业务内容;)

有什么想法吗?到目前为止,谷歌还不是我的朋友!


答案 1

我想出了如何使用SpEL做到这一点。我使用了以下表达式:

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

和以下 Java 代码:

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

工作正常;)

更新

下面是一个完整的代码示例:

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;
}

}


答案 2

推荐