单个方面的多个切入点和建议 - Spring

时间:2015-11-16 08:44:42

标签: spring aop

通常@order注释用于方面级别。但是如果aspect有多个切入点和建议,那么在哪里使用@order注释。

例如,考虑以下2个切入点,

@Pointcut("within(dao..*)")
public void creditPointcut(){}

@Pointcut("execution(* dao.*.get*(..))")
public void getDetailsPointcut(){}

及以下2条建议,

@AfterReturning(value="getDetailsPointcut()", returning= "acct")
public void afterReturning(JoinPoint point,Object acct){
   System.out.println(((Account)acct).getAccountNo());
}       

@Around("creditPointcut()") 
public void around(ProceedingJoinPoint point) throws Throwable{
    LOGGER.info("Method name: "+point.getSignature().getName()+" started");
            point.proceed();            
}

我收到以下错误,

Nov 16, 2015 2:21:51 PM aop.LoggerAspect around
INFO: Method name: credit started
Hibernate: update dactrainning.account set balance=balance+? where accountno=?
1000 rs credited into account :1
Nov 16, 2015 2:21:51 PM aop.LoggerAspect around
INFO: Method name: getAccountDetails started
Hibernate: select account0_.accountno as accountno0_, account0_.name as name0_, account0_.balance as balance0_ from dactrainning.account account0_ where account0_.accountno=?
Exception in thread "main" java.lang.NullPointerException
    at aop.LoggerAspect.afterReturning(LoggerAspect.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:603)
    at org.springframework.aop.aspectj.AspectJAfterReturningAdvice.afterReturning(AspectJAfterReturningAdvice.java:58)
    at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:51)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy11.getAccountDetails(Unknown Source)
    at service.impl.AccountServiceImpl.getAccountDetails(AccountServiceImpl.java:31)
    at client.DI.main(DI.java:18)

这可以用@order解决吗?

1 个答案:

答案 0 :(得分:0)

解释如下:

  1. 您的查询返回空结果。
  2. 因此,方法null会返回getAccountNo()
  3. 最后,您的建议会尝试在null对象上调用NullPointerException
  4. 抛出template<typename T> std::unique_ptr<T[], std::function<void(T *)>> make_T(X *ptr, std::allocator<T> alloc, std::size_t size) { auto deleter = [](T *p, std::allocator<T> alloc, std::size_t size) { for (int i = 0; i < size; ++i) { alloc.destroy(&p[i]); } alloc.deallocate(p, sizeof(T) * size); }; return {ptr, std::bind(deleter, std::placeholders::_1, alloc, size)}; } int main(int argc, const char * argv[]) { std::allocator<X> alloc = std::allocator<X>(); X *p = alloc.allocate(5); for (int i = 0; i < 5; ++i) { alloc.construct(&p[i], i + 1); } auto ptr = make_T(p, alloc, 5); return 0; }