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
          主站蜘蛛池模板: 招远市| 思南县| 盐山县| 桓仁| 商洛市| 巴青县| 抚松县| 建湖县| 洪湖市| 明溪县| 上犹县| 鄂托克前旗| 吉安市| 敖汉旗| 承德县| 姚安县| 当阳市| 宣武区| 湛江市| 靖宇县| 高州市| 鄂托克旗| 丹巴县| 曲阳县| 文山县| 张家港市| 类乌齐县| 滨州市| 新营市| 界首市| 周至县| 延吉市| 民权县| 城口县| 铜梁县| 怀化市| 咸宁市| 汉寿县| 上饶县| 松阳县| 津南区|