日期:2014-05-20  浏览次数:20897 次

编写aop:around时出现的异常org.springframework.aop.AopInvocationException:

?Exception in thread "main"org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public boolean org.spring.aop.User.login(java.lang.String,java.lang.String)
at org.springframework.aop.framework.CglibAopProxy.processReturnType(CglibAopProxy.java:351)
at org.springframework.aop.framework.CglibAopProxy.access$000(CglibAopProxy.java:83)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646)
at org.spring.aop.User$$EnhancerBySpringCGLIB$$db4a87bb.login(<generated>)

at lee.UserApp.main(UserApp.java:14)

在编写aop代码时,出现了上述的异常。我使用的是around类型的通知方法:我的aop代码如下:

?

package org.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class LoginService {
	public void enter(String account,String password){
		System.out.println("some one ....");
		System.out.println("账号:"+account);
		System.out.println("密码:"+password);
	}
	
public void leave(ProceedingJoinPoint joinpoint,String account,String password) throws Throwable{
		System.out.println("-------------------调用通知之前,around-------------");
		Object obj = joinpoint.proceed();
		System.out.println("------------------调用通知之后,around--------------");
		System.out.println("Is s ucceeded?" + obj);
		obj=false;
//		return obj;
	}
}
	public boolean login(String account,String password){
		System.out.println("--------login-------------");
		return this.account.equals(account)&&
				this.password.equals(password);
	}

出错的原因是因为:org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public boolean org.spring.aop.User.login(java.lang.String,java.lang.String)

即:在调用around的时候,原方法的返回值(也就是你要织入的方法)与通知的返回值不一致。

从代码中看出:我是void 没有返回值,而我的login的方法是由返回值的!

所以修改方法是:aop代码中leave方法的返回值类型改为与织入方法返回值一样的类型。

我上面的代码修改方法是:把void 改为 Object。