Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AOP exception when calling Around aspects on

Tags:

spring

aop

I am trying to run a aspect on all service methods. but this seems to fail for the methods which have primitive return type. I am getting this error org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type . is it necessary that all methods for used for aspect needs to have non primitive return types? Thanks

  @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
     }


  }

Stacktrace

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)
like image 864
varaJ Avatar asked Oct 29 '13 12:10

varaJ


People also ask

Which exception is thrown if an attempt is made to use a aspect designator which is not supported in Spring AOP?

AspectJ @AfterThrowing Annotation Usage AspectJ @AfterThrowing advice is executed after a join point does not complete normally and end up throwing an exception. @AfterThrowing ( "execution(* com. howtodoinjava.

Which of the following is true about after advice in Spring AOP?

After throwing advice runs when a matched method execution exits by throwing an exception. It is declared using the @AfterThrowing annotation: import org. aspectj.

What is aspect advice Pointcut JoinPoint and advice arguments in AOP?

An important term in AOP is advice. It is the action taken by an aspect at a particular join-point. Joinpoint is a point of execution of the program, such as executing a method or handling an exception. In Spring AOP, a joinpoint always represents a method execution.


1 Answers

Your aspect is wrong. An around aspect MUST have the following signature and must always return the result of the call to proceed.

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

If you don't and have a void method basically every method that this advice applies to return null.

like image 95
M. Deinum Avatar answered Oct 27 '22 04:10

M. Deinum