我愛熊貓

          最新評論

          spring aop 之一傳統方式1

          和前文一樣,aop示例也拿helloworld演示。

          IHelloService 接口:

          public interface IHelloService {
          public void sayHello();
          }

          HelloServiceImpl 實現類

          public class HelloServiceImpl implements IHelloService{
          @Override
          public void sayHello(){
          System.out.println("Hello!");
          }
          }

          IHelloAction 接口

          public interface IHelloAction {
          /*
          * 方便JUnit測試,增加返回值
          */
          public boolean sayHello();
          }

          HelloActionImpl 實現類

          public class HelloActionImpl implements IHelloAction{
          private IHelloService helloService;

          public boolean sayHello(){
          /*
          * 增加try-catch用于JUnit測試
          */
          boolean flag = true;
          try {
          helloService.sayHello();
          } catch (RuntimeException e) {
          e.printStackTrace();
          flag = false ;
          }
          return flag;
          }

          public void setHelloService(IHelloService helloService) {
          this.helloService = helloService;
          }

          }

          spring配置:

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans
          <bean id="helloService" class="cn.com.ultrapower.service.HelloServiceImpl">
          </bean>
          <bean id="helloActionTarget" class="cn.com.ultrapower.action.HelloActionImpl">
          <property name="helloService"><ref bean="helloService"/></property>
          </bean>
          <!--設置一個日志監聽器-->
          <bean id="logAdvisorBefore" class="cn.com.ultrapower.advice.ordinary.LogAdvisorBefore" />
          <!--利用aop實現日志監聽支持-->
          <bean id="helloAction" class="org.springframework.aop.framework.ProxyFactoryBean">
          <property name="proxyInterfaces"><value>cn.com.ultrapower.action.IHelloAction</value></property>
          <!--被代理對象-->
          <property name="target"><ref local="helloActionTarget"/></property>
          <!--攔截器-->
          <property name="interceptorNames">
          <list>
          <value>logAdvisorBefore</value>
          </list>
          </property>
          </bean>
          </beans>

          logAdvisorBefore 是用于在方法執行前監聽,helloAction并不直接指向HelloActionImpl類,而是通過ProxyFactoryBean創建。helloActiond的target屬性引用了helloActionTarget bean,并且增加了proxyInterfaces和interceptorNames兩個屬性。前者指向IHelloAction接口,后者是一list,也就意味著可以增加多個攔截器。如果使用過spring的事務,對上面代碼的方式應該很熟悉。

          logAdvisorBefore 代碼如下:

          public class LogAdvisorBefore implements MethodBeforeAdvice {

          /**
          * method - method being invoked
          * args - arguments to the method
          * target - target of the method invocation. May be null.
          */
          @Override
          public void before(Method method, Object[] args, Object target)
          throws Throwable {
          System.out.println("Log:do something before call");
          System.out.println("Log:call method name:"+method.getName());
          }

          }

          從實現的是MethodBeforeAdvice 接口的字面意思上就可以看出,這個是一個在方法執行前的監聽器(這里是用監聽不使用攔截是因為攔截有阻止執行的功能,而MethodBeforeAdvice 沒有這個功能)

          使用junit測試上面代碼,輸出如下:

          Log:do something before call

          Log:call method name:sayHello

          Hello!

          可見,LogAdvisorBefore 中的before方法是在action.sayHello()方法之前執行。


          posted on 2008-06-07 15:12 flyoo 閱讀(79) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 罗江县| 高台县| 疏附县| 赤峰市| 哈尔滨市| 贺兰县| 阳信县| 海南省| 西乌| 嘉义市| 手游| 故城县| 江西省| 北京市| 高清| 房山区| 东丽区| 青河县| 习水县| 尤溪县| 同江市| 黔西| 从化市| 东至县| 定州市| 洱源县| 项城市| 东乡族自治县| 霍山县| 连平县| 白河县| 天台县| 六枝特区| 溧阳市| 商丘市| 临武县| 大姚县| 横山县| 徐汇区| 扎赉特旗| 黎川县|