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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

SpringAOP之HelloWorld

2019-11-18 13:47:11
字體:
供稿:網(wǎng)友

  我們使用一個(gè)簡單的例子來演示一下SPRing中的AOP,這是一個(gè)log的例子,實(shí)際上log是一個(gè)對(duì)于AOP來說很不好的例子,這里我們只為說明Spring AOP的使用。
  
  1.首先我們來創(chuàng)建一個(gè)自己的interceptor
  這個(gè)類必須繼續(xù)org.aopalliance.intercept. MethodInterceptor接口。Spring的AOP框架就是參照aopalliance這個(gè)標(biāo)準(zhǔn)實(shí)現(xiàn)的,所以我們的MyInterceptor要繼續(xù)這個(gè)標(biāo)準(zhǔn)中的接口。
  這個(gè)接口只有一個(gè)要求實(shí)現(xiàn)的方法:
  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.");
  }
  }
  }
  
  對(duì)于上面的代碼需要說明的是下面兩行代碼:
  Object result = methodInvocation.proceed();
  return result;
  整個(gè)程序的流程是這樣的:
  1,先是執(zhí)行在Object result = methodInvocation.proceed();前面的代碼;
  2,接著執(zhí)行Object result = methodInvocation.proceed();,它把執(zhí)行控制權(quán)交給了interceptor stack(攔截器棧)內(nèi)的下一個(gè)interceptor,假如沒有了就交給真正的業(yè)務(wù)方法;
  3,然后執(zhí)行return result;之前的代碼;
  4,最后執(zhí)行return result;,它把控制權(quán)交回它之上的interceptor,假如沒有了就退出interceptor stack。
  
  2.寫出我們的業(yè)務(wù)對(duì)象及其接口
  為了方便我們的業(yè)務(wù)接口只有一個(gè)hello方法:
  
  public interface BusinessInterface {
  public void hello();
  }
  
  業(yè)務(wù)對(duì)象的代碼如下:
  
  public class BusinessInterfaceImpl implements BusinessInterface{
  public void hello() {
  System.out.println("hello Spring AOP.");
  }
  }
  
  3.接下來,我們來看看如何使用我們的寫的interceptor
  我們把業(yè)務(wù)對(duì)象作為AOP的target:
  <bean id="businessTarget" class="com.rst.spring.testaop.BusinessInterfaceImpl"/>
  接著在bean定義中聲明interceptor:
  <bean id="myInterceptor" class="com.rst.spring.testaop.MyInterceptor"/>
  最后,我們來聲明真正的業(yè)務(wù)對(duì)象,通過使用它的接口以及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>
  
  這里需要說明兩點(diǎn):
  proxyInterfaces:就是我們的業(yè)務(wù)對(duì)象的實(shí)際接口;
  interceptorNames:定義了所有interceptors的執(zhí)行順序,其中業(yè)務(wù)對(duì)象的target作為list的最后一個(gè)。記著一定要把業(yè)務(wù)對(duì)象的target放到list中,否則你的業(yè)務(wù)對(duì)象就不會(huì)工作。
  
  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上看到相應(yīng)的信息了。
  以下是附件源代碼的執(zhí)行效果:
  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。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 前郭尔| 昌平区| 夏津县| 库尔勒市| 吉首市| 孟州市| 万盛区| 仁化县| 林西县| 比如县| 徐汇区| 镇安县| 潜山县| 延津县| 叙永县| 庆阳市| 搜索| 出国| 永康市| 桐柏县| 江山市| 昆明市| 柘荣县| 崇文区| 浦东新区| 嘉鱼县| 安塞县| 丰城市| 胶州市| 汽车| 临猗县| 犍为县| 泌阳县| 海口市| 惠州市| 金阳县| 射阳县| 昆明市| 略阳县| 游戏| 吉木萨尔县|