2013-10-29 6 views
8

Sto provando a eseguire un aspetto su tutti i metodi di servizio. ma questo sembra fallire per i metodi che hanno un tipo di ritorno primitivo. Sto ottenendo questo errore org.springframework.aop.AopInvocationException: il valore di ritorno Null dal consiglio non corrisponde al tipo di ritorno primitivo. è necessario che tutti i metodi utilizzati per l'aspetto necessitino di tipi di ritorno non primitivi? GrazieEccezione AOP quando si chiama Around Aspetti su

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

risposta

12

Il tuo aspetto è sbagliato. Un aspetto attorno allo DEVE avere la seguente firma e deve sempre restituire il risultato della chiamata per procedere.

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

Se non fare e avere un metodo void praticamente ogni metodo che questo consiglio vale per tornare null.

+0

E i metodi con tipo di ritorno primitivo? il codice sopra funzionerà anche per quello? Devo modificare i metodi per restituire oggetti anziché primitivi? Grazie – varaJ

+1

No ... Devi restituire qualcosa. Ma una primitiva non può essere "null", che è anche ciò che l'eccezione ti dice (e stai forzando "null" a causa del tipo di ritorno "void" che è illegale per una primitiva). –