introduction為對(duì)象動(dòng)態(tài)的加入原先所沒(méi)有的職責(zé)。

          例子:
          系統(tǒng)中已經(jīng)有如下的類:
          接口
          package?cn.blogjava.introduction;

          public?interface?ISome?{
          ????
          public?void?doSome();
          }

          package?cn.blogjava.introduction;

          public?class?Some?implements?ISome?{

          ????
          public?void?doSome()?{
          ????????System.out.println(
          "do?some");

          ????}

          }

          希望給Some類增加doOther()方法,可以通過(guò)IntroductionInterceptor來(lái)完成任務(wù)。
          首先定義接口
          package?cn.blogjava.introduction;

          public?interface?IOther?{
          ????
          public?void?doOther();
          }

          實(shí)現(xiàn)IntroductionInterceptor接口:
          package?cn.blogjava.introduction;

          import?org.aopalliance.intercept.MethodInvocation;
          import?org.springframework.aop.IntroductionInterceptor;

          public?class?OtherIntroduction?implements?IOther,?IntroductionInterceptor?{

          ????
          public?void?doOther()?{
          ????????System.out.println(
          "do?other");

          ????}

          ????
          public?Object?invoke(MethodInvocation?methodInvocation)?throws?Throwable?{
          ????????
          if(implementsInterface(methodInvocation.getMethod().getDeclaringClass()))?{
          ????????????
          return?methodInvocation.getMethod().invoke(this,?methodInvocation.getArguments());
          ????????}?
          else?{
          ????????????
          return?methodInvocation.proceed();
          ????????}
          ????}

          ????
          public?boolean?implementsInterface(Class?aclass)?{
          ????????
          return?aclass.isAssignableFrom(IOther.class);
          ????}

          }

          測(cè)試類:
          package?cn.blogjava.introduction;

          import?org.springframework.context.ApplicationContext;
          import?org.springframework.context.support.FileSystemXmlApplicationContext;

          public?class?SpringAOPDemo?{

          ????
          public?static?void?main(String[]?args)?{
          ????????ApplicationContext?context?
          =?
          ????????????
          new?FileSystemXmlApplicationContext("beans-config.xml");
          ????????
          ????????ISome?some?
          =?(ISome)context.getBean("proxyFactoryBean");
          ????????some.doSome();
          ????????
          ????????((IOther)some).doOther();

          ????}

          }
          posted on 2006-08-18 15:20 knowhow 閱讀(786) 評(píng)論(0)  編輯  收藏 所屬分類: Framework

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 温州市| 博爱县| 北辰区| 界首市| 贵南县| 竹北市| 金山区| 大田县| 明溪县| 界首市| 农安县| 兰州市| 那坡县| 东港市| 旬邑县| 天峨县| 崇文区| 钟山县| 河西区| 都江堰市| 沁阳市| 平罗县| 吉安县| 电白县| 乌苏市| 桂阳县| 定结县| 吴堡县| 望都县| 响水县| 镇赉县| 嫩江县| 宝山区| 伊川县| 南昌市| 张家港市| 平顺县| 辽中县| 调兵山市| 平定县| 故城县|