我愛熊貓

          最新評論

          spring aop 之二 xml方式

          前篇介紹的是使用注釋方式實現aop,本篇將介紹使用xml方式實現。

          先看xml的配置:

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          http://www.springframework.org/schema/aop

          <bean id="helloService" class="cn.com.ultrapower.service.HelloServiceImpl">
          </bean>
          <bean id="helloAction" class="cn.com.ultrapower.action.HelloActionImpl">
          <property name="helloService"><ref bean="helloService"/></property>
          <property name="name" value="yoo"></property>
          </bean>
          <bean id="logAdvisor" class="cn.com.ultrapower.advice.schemabased.LogAdvisor">
          </bean>
          <aop:config>
          <aop:aspect ref="logAdvisor">
          <aop:pointcut id="someMethod" expression="execution(* cn.com.ultrapower.action.IHelloAction.*(..))"/>
          <aop:after-returning pointcut-ref="someMethod" method="after"/>
          <aop:before pointcut-ref="someMethod" method="after"/>
          </aop:aspect>
          </aop:config>
          </beans>
          這里定義了兩個普通的bean:helloService和helloAction。另外定義了一個用于監聽的logAdvisor。

          特殊的是<aop:config>...</aop:config>中的監聽器配置。定義了切面,切點,前置方法和后置方法。

          然后再看看logAdvisor的代碼:

          public class LogAdvisor {

          public void before() {
          System.out.println("Log:before method!");
          }

          public void after() {
          System.out.println("Log:after method!");
          }
          }

          很簡單,就兩個普通方法,before用于前置打印信息,after用于后置打印信息。

          posted on 2008-06-07 20:54 flyoo 閱讀(80) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 保康县| 河西区| 承德市| 广安市| 五常市| 花莲县| 千阳县| 陆川县| 英超| 金湖县| 西林县| 桦南县| 郴州市| 凉山| 灵石县| 黑河市| 龙游县| 琼海市| 开远市| 永州市| 阳春市| 桂林市| 通江县| 白玉县| 万安县| 林西县| 历史| 镇安县| 剑阁县| 无棣县| 喀喇沁旗| 安塞县| 繁昌县| 青龙| 哈尔滨市| 呼和浩特市| 科尔| 满洲里市| 泸州市| 墨竹工卡县| 吴川市|