自定义注释中的Spring表达式语言

时间:2013-12-03 12:15:25

标签: java spring aop spring-el

我想在自定义注释中使用Spring Expression Language。 此注释将由自定义Aspect使用。

检查出来:

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

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

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

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

1 个答案:

答案 0 :(得分:2)

我想通过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;
}

}