选择显示字体大小

spring aop之hello world

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

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons