gpt4 book ai didi

spring - 调用 Around 方面时的 AOP 异常

转载 作者:IT老高 更新时间:2023-10-28 13:46:14 25 4
gpt4 key购买 nike

我正在尝试在所有服务方法上运行一个方面。但这对于具有原始返回类型的方法似乎失败了。我收到此错误 org.springframework.aop.AopInvocationException: Null return value from advice does not match original return type 。用于方面的所有方法是否都需要具有非原始返回类型?谢谢

  @Aspect
@Component
public class ServiceAspect
{
private static final Logger LOG = Logger.getLogger(ServiceAspect.class);

@Pointcut("execution(* com.xxx.service..*.*(..))")

public void perform()
{

}

@Around("perform()")
public void performTimeCal(ProceedingJoinPoint joinPoint)
{
try
{
Date start = DateUtils.newDate();
String processingTime = null;
joinPoint.proceed();

processingTime = DateUtils.computeDateDifference(start,
DateUtils.newDate());
LOG.info("STAT: " + getSimpleClassName(joinPoint) + "."
+ joinPoint.getTarget() + "(): " + processingTime);
}
catch (Throwable e)
{
e.printStackTrace();
}

}

private String getSimpleClassName(ProceedingJoinPoint joinPoint)
{
if (joinPoint.getThis() != null)
{
return joinPoint.getThis().getClass().getSimpleName();
}
return "";

}

}

Service Method

@Service
public PropertyService
{
public boolean isMessageProcessingEnabled()
{
// DAO CODE
}


}

堆栈跟踪

org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public boolean com.xxxxxxx.service.admin.PropertyService.isMessageProcessingEnabled()
at org.springframework.aop.framework.CglibAopProxy.processReturnType(CglibAopProxy.java:349)
at org.springframework.aop.framework.CglibAopProxy.access$000(CglibAopProxy.java:82)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:633)
at com.xxxxxxx.service.admin.ApplicationPropertyService$$EnhancerByCGLIB$$1a4e8f96.isMessageProcessingEnabled(<generated>)
at com.xxxxxxx.processBoardMessages(BoardMessageProcessor.java:136)
at com.xxxxxxx.processMessage(BoardMessageProcessor.java:95)
at com.xxxxxxx.service.BrdServiceTest.testMessage(BoardServiceTest.java:126)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)

最佳答案

你的方面是错误的。环绕方面必须具有以下签名,并且必须始终返回调用结果才能继续。

public Object aroundMethodName(ProceedingJoinPoint pjp, [other attributes) {
Object result = pjp.proceed();
return result;
}

如果你没有并且有一个 void 方法,那么这个建议适用的每个方法基本上都会返回 null

关于spring - 调用 Around 方面时的 AOP 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19657924/

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