yuyee

          使用Annotation來完成spring aop

          spring AOP中,可以通過annotation來簡化XML上的配置,可以很靈活的使切面切入到自己想要的方法上,而不需要象aop:config里一定要切入到特定命名方法上
          使用annotation來表示pointcut,需要在xml里配置org.springframework.aop.support.annotation.AnnotationMatchingPointcut,
          如:<bean name="timeoutPointcut"
          class="org.springframework.aop.support.annotation.AnnotationMatchingPointcut">
          <constructor-arg index="0"
          value="com.hengtian.fxfundsystem.aop.annotation.UseAOPAnnotation">
          </constructor-arg>
          <constructor-arg index="1"
          value="com.hengtian.fxfundsystem.aop.annotation.TimeoutAnnotation" />
          </bean>
          第一個參數是標在目標類上的annotation,第二天參數是標注在方法上的Annotation
          因此,首先要在自己的工程里定義2個annotation,一個為TimeoutAnnotation,一個為UseAOPAnnotation
          @Target({ElementType.TYPE })//表示類標注
          @Retention(RetentionPolicy.RUNTIME)
          @Inherited
          public @interface UseAOPAnnotation {

          }
          @Target({ElementType.METHOD})//表示方法標注
          @Retention(RetentionPolicy.RUNTIME)
          public @interface TimeoutAnnotation {
          /**
          * 默認值(-1):表示不啟用超時
          */
          public static final long DefaultValue = -1;

          /**
          * 超時時間,單位是ms
          * @return
          */
          long value() default DefaultValue;

          }
          在需要AOP的類上使用
          @UseAOPAnnotation
          @Component("StrongDuck")  
          public class StrongDuck {
          @TimeoutAnnotation(70000)
          public void say(){
          System.out.println("I am a strong duck");
          }
          }
          timeout通知:
          public class TimeoutAdvice implements MethodInterceptor {

          /*
          * (non-Javadoc)
          * @see
          * org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept
          * .MethodInvocation)
          */
          public Object invoke(final MethodInvocation methodInvocation)
          throws Throwable {

          Method targetMethod = methodInvocation.getMethod();
          TimeoutAnnotation timeoutAnnotation = targetMethod
          .getAnnotation(TimeoutAnnotation.class);
          if (timeoutAnnotation != null) {
          long waitTime = timeoutAnnotation.value();
          if (waitTime != TimeoutAnnotation.DefaultValue) {
          Future<?> task = ThreadManager.ExecutorService
          .submit(encapsulateSyncToAsync(methodInvocation));
          return getAsyncResult(waitTime, task);
          }

          }
          return methodInvocation.proceed();
          }
          XML中配置攔截器:
          <bean id="timeoutInterceptor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
          <property name="advice" ref="timeoutAdvice" />
          <property name="pointcut" ref="timeoutPointcut" />
          </bean>
          <bean
          class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
          <property name="proxyTargetClass"><!-- 基于類的代理 -->
          <value>true</value>
          </property>
          </bean>
          這樣,spring就會為你生成aop代理類實現AOP


          posted on 2010-10-25 16:52 羔羊 閱讀(1833) 評論(0)  編輯  收藏 所屬分類: aop

          主站蜘蛛池模板: 河津市| 溆浦县| 胶南市| 古浪县| 正安县| 建阳市| 台湾省| 会东县| 大同市| 南阳市| 平罗县| 呼和浩特市| 周宁县| 田东县| 油尖旺区| 宜川县| 永福县| 南部县| 普宁市| 攀枝花市| 纳雍县| 龙里县| 鸡西市| 巴马| 盐津县| 宁乡县| 岱山县| 碌曲县| 新建县| 皮山县| 濮阳县| 扶风县| 寿阳县| 土默特右旗| 综艺| 嘉荫县| 武夷山市| 通海县| 广河县| 承德市| 临安市|