我想出了如何使用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;
}
}