问题描述
我想在自定义注释中使用Spring Expression Language.此注释将由自定义方面使用.
I want to use Spring Expression Language in a custom Annotation.This annotation will be consumed by a custom Aspect.
检查一下:
@StatisticEventTrigger(value = TestStatisticEvent.class, expression = "#p1")
public void someOtherMethod(String arg1, Long arg2) {
如您所见,我想使用表达式(在这种情况下)检索某些特定参数.
As you can see, i want to use the expression (in this case) to retrieve some specific argument.
当我有了我的Aspect时,触发一个带注释的方法,我想(以编程方式)评估spring表达式以检索一个值以用于其他业务;)
When I have my Aspect, triggering an annotated method, i would like to evaluate the spring expression (programmatically) to retrieve a value to use for further business stuff ;)
有什么想法吗?到目前为止,谷歌还不是我的朋友!
Any ideas? Google was not my friend so far!
推荐答案
我想出了如何使用SpEL做到这一点.我使用以下表达式:
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]}")
以及以下Java代码:
and following Java code:
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(expressionEvaluationTarget);
Object[] eventPayloadModels = parser.parseExpression(expression).getValue(context);
工作正常;)
更新
这是一个完整的代码示例:
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;
}
}
这篇关于自定义注释中的Spring Expression Language的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!