国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Spring AOP之Hello World

2019-11-18 12:59:56
字體:
來源:轉載
供稿:網友

  我們使用一個簡單的例子來演示一下SPRing中的AOP,這是一個log的例子,實際上log是一個對于AOP來說很不好的例子,這里我們只為說明Spring AOP的使用。
  
  1.首先我們來創建一個自己的interceptor
  這個類必須繼續org.aopalliance.intercept. MethodInterceptor接口。Spring的AOP框架就是參照aopalliance這個標準實現的,所以我們的MyInterceptor要繼續這個標準中的接口。
  這個接口只有一個要求實現的方法:
  public Object invoke(MethodInvocation methodInvocation) throws Throwable;
  下面是我們的MyIntercptor:
  
  public class MyInterceptor implements MethodInterceptor {
  private final Log logger = LogFactory.getLog(getClass());
  
  public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  logger.info("Beginning method (1): " +
  methodInvocation.getMethod().getDeclaringClass() + "." +
  methodInvocation.getMethod().getName() + "()");
  long startTime = System.currentTimeMillis();
  try{
  Object result = methodInvocation.proceed();
  return result;
  }finally{
  logger.info("Ending method (1): " +
  methodInvocation.getMethod().getDeclaringClass() + "." +
  methodInvocation.getMethod().getName() + "()");
  logger.info("Method invocation time (1): " +
  (System.currentTimeMillis() - startTime) + " ms.");
  }
  }
  }
  
  對于上面的代碼需要說明的是下面兩行代碼:
  Object result = methodInvocation.proceed();
  return result;
  整個程序的流程是這樣的:
  1,先是執行在Object result = methodInvocation.proceed();前面的代碼;
  2,接著執行Object result = methodInvocation.proceed();,它把執行控制權交給了interceptor stack(攔截器棧)內的下一個interceptor,假如沒有了就交給真正的業務方法;
  3,然后執行return result;之前的代碼;
  4,最后執行return result;,它把控制權交回它之上的interceptor,假如沒有了就退出interceptor stack。
  
  2.寫出我們的業務對象及其接口
  為了方便我們的業務接口只有一個hello方法:
  
  public interface BusinessInterface {
  public void hello();
  }
  
  業務對象的代碼如下:
  
  public class BusinessInterfaceImpl implements BusinessInterface{
  public void hello() {
  System.out.println("hello Spring AOP.");
  }
  }
  
  3.接下來,我們來看看如何使用我們的寫的interceptor
  我們把業務對象作為AOP的target:
  <bean id="businessTarget" class="com.rst.spring.testaop.BusinessInterfaceImpl"/>
  接著在bean定義中聲明interceptor:
  <bean id="myInterceptor" class="com.rst.spring.testaop.MyInterceptor"/>
  最后,我們來聲明真正的業務對象,通過使用它的接口以及Spring的ProxyFactoryBean:
  
  <bean id="businessBean"
    class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
  <value>com.rst.spring.testaop.BusinessInterface</value>
  </property>
  <property name="interceptorNames">
  <list>
  <value>myInterceptor</value>
  <value>businessTarget</value>
  </list>
  </property>
  </bean>
  
  這里需要說明兩點:
  proxyInterfaces:就是我們的業務對象的實際接口;
  interceptorNames:定義了所有interceptors的執行順序,其中業務對象的target作為list的最后一個。記著一定要把業務對象的target放到list中,否則你的業務對象就不會工作。
  
  4.最后,寫我們的測試類
  ClassPathResource resource =
  new ClassPathResource("com/rst/spring/testaop/aop_bean.xml");
  XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
  BusinessInterface businessBean =
  (BusinessInterface) beanFactory.getBean("businessBean");
  businessBean.hello();
  
  一切正常就可以在log上看到相應的信息了。
  以下是附件源代碼的執行效果:
  2004-09-08 16:04:51,210 INFO - Beginning method (1): interface com.rst.spring.testaop.BusinessInterface.hello()
  2004-09-08 16:04:51,210 INFO - Beginning method (2): interface com.rst.spring.testaop.BusinessInterface.hello()
  hello Spring AOP.
  2004-09-08 16:04:51,210 INFO - Ending method (2): interface com.rst.spring.testaop.BusinessInterface.hello()
  2004-09-08 16:04:51,210 INFO - Ending method (1): interface com.rst.spring.testaop.BusinessInterface.hello()
  2004-09-08 16:04:51,210 INFO - Method invocation time (1): 0 ms.
  源代碼需要spring.jar, aopallience.jar, commons-logging.jar。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凌海市| 江津市| 米林县| 马山县| 通河县| 通山县| 手游| 永定县| 永定县| 南和县| 景洪市| 霞浦县| 安新县| 澄江县| 临江市| 屏南县| 谢通门县| 牙克石市| 博乐市| 托克托县| 柳河县| 高要市| 洞头县| 旅游| 洪湖市| 长兴县| 衡山县| 阜城县| 凤凰县| 湛江市| 呼伦贝尔市| 黄陵县| 平塘县| 三都| 土默特左旗| 额济纳旗| 云浮市| 南丰县| 新晃| 嵊州市| 雷州市|