网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  spring aop之hello world     
  文章作者:未知  文章来源:水木森林  
  查看:74次  录入:管理员--2007-11-17  
 
  我们使用一个简单的例子来演示一下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。
 
 
上一篇: spring reference    下一篇: spring,ioc模式与ejb3的slsb实现
  相关文档
在java中保留stereotype 11-17
开源技术:如何在eclipse中构建备忘单 11-16
关于java中相对路径,绝对路径问题总结 02-25
深入探讨ejb中新的消息驱动组件(一) 11-17
java理论与实践:您的小数点在哪? 11-17
ejb应用开发的设计实例深入分析 11-17
创建访问db2 everyplace的jsp应用程序 11-17
jfc/swing活学活用之定制jlist显示 11-17
j2me学习笔记(3)—初次接触midlets 11-17
struts构建文件上传(1) 11-17
对 jdo 的架构作一个简单的介绍 11-17
将数字字符串转换成大写金额字符串 11-17
sun. xvr-1200 图形加速器的使用 11-17
j2se中的序列化详解(一) 11-16
对java多线程技术中所有方法的详细解析 11-16
使用java applet访问数据库 11-17
使用mrjappbuilder创建本地应用 11-17
3d建模技巧 11-17
struts中用动态选择的元素创建复选框 11-16
如何使用preparedstatement减少开发时间 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息