Spring2.0 AOP使用心得(一)

          ??????正在做的項目中有一個類似積分的需求,比如用戶在討論區發帖后,需要給一定的積分這樣的需求。因為需求是變化的,同時為了使已經寫好的發帖代碼不被入侵,所以考慮使用Spring2.0 AOP來實現這個功能。
          ??????畢竟是第一次使用Spring2.0的AOP,按照Reference中的介紹,準備使用Annotation來完成對AOP的配置。來看一下我做的步驟:
          ??????一、需要使用Spring2.0的jar包,現在已經發布正式版的2.0了,可以從http://www.springframework.org/? 上下載到最新的2.0版本。加入到項目的classpath中去。
          ????? 二、需要在配置文件中啟用新的spring2.0的schema或者是dtd。???
          ??????1、在Spring的xml配置文件中加入新的schema:
          <beans?xmlns="http://www.springframework.org/schema/beans"
          ????xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
          ????xmlns:aop
          ="http://www.springframework.org/schema/aop"
          ????xmlns:tx
          ="http://www.springframework.org/schema/tx"
          ????xsi:schemaLocation
          ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          ???????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
          ???????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"

          ????default-autowire
          ="byName"?default-lazy-init="true">
          ?????? 2、“如果使用Java 5的話,推薦使用Spring提供的@AspectJ切面支持,通過這種方式聲明Spring AOP中使用的切面。 "@AspectJ"使用了Java 5的注解,可以將切面聲明為普通的Java類。”——Spring reference
          ??????3、為了使用AOP的Annotation,在配置文件中加入<aop:aspectj-autoproxy />。
          ??????4、編寫切面類:
          @Aspect
          public?class?ArticleRemoteAccountsService?{
          ?


          ????
          /**
          ?????*?在發帖成功之后,給用戶銀幣賬戶沖值
          ?????*?
          ?????*?
          @param?arg
          ?????*?
          @throws?AccountsException
          ?????*?
          @throws?InstantiationException
          ?????*?
          @throws?IllegalAccessException
          ?????
          */

          ????@After(
          "execution(*?com.company.ArticleManager.saveArticle(..))"
          ????????????
          +?"?&&?args(arg)")
          ????
          public?void?exSilByPost(Article?arg)?throws?AccountsException,
          ????????????InstantiationException,?IllegalAccessException?
          {

          ????????
          if?(arg.getLastUpdateTime()?==?null
          ????????????????
          &&?arg.getArticleByParentId()?==?null
          ????????????????
          &&?arg.getArticleByRootId()?==?null)?{
          ????????????
          //?TODO 主題帖

          ????????}
          ?else?if?(arg.getLastUpdateTime()?==?null
          ????????????????
          &&?(arg.getArticleByParentId()?!=?null?||?arg
          ????????????????????????.getArticleByRootId()?
          !=?null))?{
          ????????????
          //?TODO? 回帖

          ????????}


          ????}


          }

          這里需要注意的是使用Annotation的Poincut語法,
          execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
          這里就不累訴了。
          同時要注意的如何得到參數的問題,寫法參考如上。
          posted on 2006-10-20 11:02 旱頭憨腦 閱讀(976) 評論(0)  編輯  收藏 所屬分類: j2ee
          主站蜘蛛池模板: 射洪县| 南川市| 宿松县| 崇阳县| 鲜城| 阳山县| 原阳县| 金山区| 桐柏县| 九龙坡区| 许昌市| 怀来县| 乐安县| 达州市| 永春县| 江都市| 肥西县| 乐亭县| 德阳市| 讷河市| 论坛| 东阿县| 长岭县| 贵南县| 株洲县| 六枝特区| 尉氏县| 竹溪县| 麦盖提县| 大姚县| 北宁市| 周宁县| 彰化市| 肥城市| 萨嘎县| 伊宁县| 塘沽区| 乌拉特前旗| 类乌齐县| 海兴县| 青阳县|