第五章 Spring进阶-注解方式实现AOP(3)-全文完

直接在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
<aop:config> <aop:aspect ref="simpleProfiler"> <aop:pointcut id="theExceution" expression="execution(* com.dj.spring.StudentService.getAnotherStudent(String, int)) and args(name, age)"/> <aop:around pointcut-ref="theExceution" method="profile"/> </aop:aspect> </aop:config>
    网上这个问题是解决的,猜想: 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)" />  
        <!--这个将拦截到:
        void saveBook(String bookName)
            -->
           
            < aop:pointcut  
id = "bookVoidTwoArgCut"   expression = "execution (void com.netease.lee.service..*.*(..)) and args(bookName,bookId)" />  
        <!-- 这个将拦截到:
            void updateBoook(String bookName, Integer bookId)
             -->
           
        < aop:pointcut  
id = "bookReturnStringCut"   expression = "execution (java.lang.String com.netease.lee.service..*.*(..))" />  
        <!--表示只拦截返回String的方法:
        String getBookName(Integer bookId);
            -->
        < 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);
  }
}
运行测试用例,通过:
wps_clip_image1
    搞定,收工,不如去毛家吧 ,员工卡还可以打折 ,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支持
全文完
关注我的微博,请使用网易通行证,通过邀请链接登陆,就可以直接关注我。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值