<table height="100%" cellspacing=2 cellpadding=0 width="99%" border=0>
<tbody>
<tr>
<td valign=top align=middle colspan=2>
<table class=tf width="98%" border=0>
<tbody>
<tr>
<td class=bw><font class=htd>我们使用一个简单的例子来演示一下spring中的aop,这是一个log的例子,实际上log是一个对于aop来说很不好的例子,这里我们只为说明spring aop的使用。<br><br>一,首先我们来创建一个自己的interceptor。这个类必须继承org.aopalliance.intercept. methodinterceptor接口。spring的aop框架就是参照aopalliance这个标准实现的,所以我们的myinterceptor要继承这个标准中的接口。<br>这个接口只有一个要求实现的方法:<br>public object invoke(methodinvocation methodinvocation) throws throwable;<br>下面是我们的myintercptor:<br>
<table style="table-layout: fixed" cellspacing=0 bordercolordark=#ffffff cellpadding=4 width="98%" align=center bgcolor=#e6e6e6 bordercolorlight=#009ace border=1>
<tbody>
<tr>
<td style="word-wrap: break-word"><br>public class myinterceptor implements methodinterceptor {<br>private final log logger = logfactory.getlog(getclass());<br> <br>public object invoke(methodinvocation methodinvocation) throws throwable {<br>logger.info("beginning method (1): " + <br>methodinvocation.getmethod().getdeclaringclass() + "." + <br>methodinvocation.getmethod().getname() + "()");<br>long starttime = system.currenttimemillis();<br>try{<br>object result = methodinvocation.proceed();<br>return result;<br>}finally{<br>logger.info("ending method (1): " + <br>methodinvocation.getmethod().getdeclaringclass() + "." +<br>methodinvocation.getmethod().getname() + "()");<br>logger.info("method invocation time (1): " + <br>(system.currenttimemillis() - starttime) + " ms.");<br>}<br>}<br>}<br></td></tr></tbody></table><br><br>对于上面的代码需要说明的是下面两行代码:<br>object result = methodinvocation.proceed();<br>return result;<br>整个程序的流程是这样的:<br>1,先是执行在object result = methodinvocation.proceed();前面的代码;<br>2,接着执行object result = methodinvocation.proceed();,它把执行控制权交给了interceptor stack(拦截器栈)内的下一个interceptor,如果没有了就交给真正的业务方法;<br>3,然后执行return result;之前的代码;<br>4,最后执行return result;,它把控制权交回它之上的interceptor,如果没有了就退出interceptor stack。<br><br>二,写出我们的业务对象及其接口<br>为了方便我们的业务接口只有一个hello方法:<br>
<table style="table-layout: fixed" cellspacing=0 bordercolordark=#ffffff cellpadding=4 width="98%" align=center bgcolor=#e6e6e6 bordercolorlight=#009ace border=1>
<tbody>
<tr>
<td style="word-wrap: break-word"><br>public interface businessinterface {<br>public void hello();<br>}<br></td></tr></tbody></table><br><br>业务对象的代码如下:<br>
<table style="table-layout: fixed" cellspacing=0 bordercolordark=#ffffff cellpadding=4 width="98%" align=center bgcolor=#e6e6e6 bordercolorlight=#009ace border=1>
<tbody>
<tr>
<td style="word-wrap: break-word"><br>public class businessinterfaceimpl implements businessinterface{<br>public void hello() {<br>system.out.println("hello spring aop.");<br>}<br>}<br></td></tr></tbody></table><br><br><br>三,接下来,我们来看看如何使用我们的写的interceptor。<br>我们把业务对象作为aop的target:<br><bean id="businesstarget" class="com.rst.spring.testaop.businessinterfaceimpl"/><br>接着在bean定义中声明interceptor:<br><bean id="myinterceptor" class="com.rst.spring.testaop.myinterceptor"/><br>最后,我们来声明真正的业务对象,通过使用它的接口以及spring的proxyfactorybean:<br>
<table style="table-layout: fixed" cellspacing=0 bordercolordark=#ffffff cellpadding=4 width="98%" align=center bgcolor=#e6e6e6 bordercolorlight=#009ace border=1>
<tbody>
<tr>
<td style="word-wrap: break-word"><br><bean id="businessbean"<br> class="org.springframework.aop.framework.proxyfactorybean"><br><property name="proxyinterfaces"><br><value>com.rst.spring.testaop.businessinterface</value><br></property><br><property name="interceptornames"><br><list><br><value>myinterceptor</value><br><value>businesstarget</value><br></list><br></property><br></bean><br></td></tr></tbody></table><br><br>这里需要说明两点:<br>proxyinterfaces:就是我们的业务对象的实际接口;<br>interceptornames:定义了所有interceptors的执行顺序,其中业务对象的target作为list的最后一个。记着一定要把业务对象的target放到list中,否则你的业务对象就不会工作。<br><br>四,最后,写我们的测试类<br>
<table style="table-layout: fixed" cellspacing=0 bordercolordark=#ffffff cellpadding=4 width="98%" align=center bgcolor=#e6e6e6 bordercolorlight=#009ace border=1>
<tbody>
<tr>
<td style="word-wrap: break-word"><br>classpathresource resource = <br>new classpathresource("com/rst/spring/testaop/aop_bean.xml");<br>xmlbeanfactory beanfactory = new xmlbeanfactory(resource);<br>businessinterface businessbean = <br>(businessinterface) beanfactory.getbean("businessbean");<br>businessbean.hello();<br></td></tr></tbody></table><br><br>一切正常就可以在log上看到相应的信息了。<br>以下是附件源代码的执行效果:<br>2004-09-08 16:04:51,210 info - beginning method (1): interface com.rst.spring.testaop.businessinterface.hello()<br>2004-09-08 16:04:51,210 info - beginning method (2): interface com.rst.spring.testaop.businessinterface.hello()<br>hello spring aop.<br>2004-09-08 16:04:51,210 info - ending method (2): interface com.rst.spring.testaop.businessinterface.hello()<br>2004-09-08 16:04:51,210 info - ending method (1): interface com.rst.spring.testaop.businessinterface.hello()<br>2004-09-08 16:04:51,210 info - method invocation time (1): 0 ms.<br>源代码需要spring.jar, aopallience.jar, commons-logging.jar。<br>spring aop是一个很方便高效的框架,尤其在事务管理中使用到很多,希望能和大家交流。<br><img height=16 src="http://www.matrix.org.cn/images/small/download.gif" width=16 align=absmiddle border=0> <a href="http://www.matrix.org.cn/upload/forum/200498171413.rar" target=_blank>『 点击下载 』</a><br></font></td></tr></tbody></table></td></tr>
<tr>
<td align=right colspan=2 height=20><img class=fr src="http://www.matrix.org.cn/images/small/signature.gif" align=absmiddle border=0> </td></tr>
<tr>
<td valign=top align=middle colspan=2 height=30>
<table class=tf width="96%" border=0>
<tbody>
<tr>
<td class=bw><font class=htd>欢迎讨论spring framework相关的问题:<br><a href="http://www.matrix.org.cn/forum_list.asp?forum_id=26" target=_blank>http://www.matrix.org.cn/forum_list.asp?forum_id=26</a></font></a></td></tr></tbody></table></td></tr></tbody></table>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器