gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:55:52 26 4
gpt4 key购买 nike

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

检查一下:

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

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

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

有什么想法吗?到目前为止,Google 不是我的 friend !

最佳答案

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

关于java - 自定义注释中的 Spring 表达式语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20350961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com