直接在Google搜索(别问怎么上Google,那只是一个传说)
“[org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut”,
找到:http://forum.springsource.org/showthread.php?t=62460
查看,最后说到:
One more bug was present in xml
changed appContext.xml to
网上这个问题是解决的,猜想:
XML配置没有提供参数的支持,若切面里的方法有参数,就会报错。
徒弟呀,你认真看看接口BookService的定义:
void
listBook();
String getBookName(Integer bookId);
void
saveBook(String bookName);
void
updateBoook(String bookName, Integer bookId);
是不是发现很多方法都有参数呀?应该怎么办呢?
师傅,是不是根据注解的方式,在xml中配置不同切点解决问题?我们试试吧:
<
bean
id
=
"bookInterceptorTwo"
class
=
"com.netease.lee.service.BookInterceptorTwo"
/>
<
aop:config
>
<
aop:aspect
id
=
"bookAop"
ref
=
"bookInterceptorTwo"
>
<
aop:pointcut
id
=
"booAnyCut"
expression
=
"execution (* com.netease.lee.service..*.*(..))"
/>
<
aop:pointcut
id
=
"bookVoidOneArgCut"
expression
=
"execution (
void
com.netease.lee.service..*.*(..)) and args(bookName)"
/>
<
aop:pointcut
id
=
"bookVoidTwoArgCut"
expression
=
"execution (void com.netease.lee.service..*.*(..)) and args(bookName,bookId)"
/>
<
aop:pointcut
id
=
"bookReturnStringCut"
expression
=
"execution (java.lang.String com.netease.lee.service..*.*(..))"
/>
<
aop:before
pointcut-ref
=
"bookVoidOneArgCut"
method
=
"doBefore"
arg-names
=
"bookName"
/>
<
aop:after-throwing
pointcut-ref
=
"booAnyCut"
method
=
"doAfterThrowing"
throwing
=
"e"
/>
<
aop:after-returning
pointcut-ref
=
"bookReturnStringCut"
method
=
"doAfterReturning"
returning
=
"result"
/>
<
aop:after
pointcut-ref
=
"booAnyCut"
method
=
"doAfter"
/>
<
aop:around
pointcut-ref
=
"booAnyCut"
method
=
"doAround"
/>
aop:aspect
>
aop:config
>
BookServiceImpl修改如下:
@Service
(
"bookService"
)
public
class
BookServiceImpl
implements
BookService {
@Resource
private
BookDAO
bookDAO
;
public
void
listBook() {
bookDAO
.listBook();
}
@Override
public
String getBookName(Integer bookId) {
return
bookDAO
.getBookName(bookId);
}
@Override
public
void
saveBook(String bookName) {
bookDAO
.saveBook(bookName);
}
@Override
public
void
updateBoook(String bookName, Integer bookId) {
throw
new
RuntimeException(
"更新发生异常"
);
//bookDAO.updateBook(bookName, bookId);
}
}
运行测试用例,通过:
搞定,收工,不如去毛家吧
,员工卡还可以打折
,GO,GO,GO!!!
补充:
execution的完整表达式如下:
execution(修饰符? 返回类型 声明类型? 方法名称(参数类型) 异常类型?)
除了返回类型、方法名称和参数类型是必须的之外,其他都是可以选的。
一些execution的示例如下:
执行任意的public方法:
execution(public * * (..))
执行任何以set开头的方法:
execution(* set*(..))
执行任何至少一个参数,并且第一个参数类型为String的方法:
execution(* *(java.lang.String,..))
执行service包或者子包的任意接口或类的任意方法:
execution (* com.netease.lee.service..*.*(..))
执行可能会抛出IOException的方法:
execution(* *(..) throws java.io.Exception)
提示
:
具体可以参考:Spring Framework
第 6 章 使用Spring进行面向切面编程(AOP)6.2. @AspectJ支持
全文完
关注我的微博,请使用网易通行证,通过邀请链接登陆,就可以直接关注我。