{"id":1005,"date":"2010-10-02T00:33:03","date_gmt":"2010-10-02T00:33:03","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1005"},"modified":"2010-10-02T00:33:03","modified_gmt":"2010-10-02T00:33:03","slug":"%e4%bc%a0%e7%bb%9faop%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/10\/02\/%e4%bc%a0%e7%bb%9faop%e5%ae%9e%e7%8e%b0\/","title":{"rendered":"\u4f20\u7edfAOP\u5b9e\u73b0"},"content":{"rendered":"<p>1\uff0c\u5b8c\u6210\u65b9\u6cd5\u7684\u589e\u5f3a<br \/>\n\u5b9e\u73b0MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor,ThrowsAdvice\u7b49\u63a5\u53e3<br \/>\nMethodBeforeAdvice,? \u5728\u65b9\u6cd5\u8c03\u7528\u524d\u6267\u884c before( Method m, Object[] arg1, Object arg2)<br \/>\nAfterReturningAdvice, \u5728\u65b9\u6cd5\u8c03\u7528\u540e\u6267\u884c afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3)<br \/>\nMethodInterceptor\uff0c\u5728process()\u524d\u540e\u6267\u884c\uff0cinvoke(MethodInvocation mi)<br \/>\nThrowsAdvice \u5f53\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\u65f6\u6267\u884c\uff0cafterThrowing(Exception ex)<\/p>\n<p>public class LoginBeforeAop implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor {<\/p>\n<p>public void before( Method m, Object[] arg1, Object arg2)<br \/>\nthrows Throwable {<br \/>\nSystem.out.println( m.getName() + &#8221; &#8221; + &#8221; before &#8212;&#8212;&#8212;&#8212; &#8221; );<\/p>\n<p>}<\/p>\n<p>public void afterReturning(Object arg0, Method arg1, Object[] arg2,<br \/>\nObject arg3) throws Throwable {<br \/>\n\/\/ TODO Auto-generated method stub<br \/>\nSystem.out.println(? &#8221; end &#8212;&#8212;&#8212;&#8212; &#8221; );<br \/>\n}<\/p>\n<p>public Object invoke(MethodInvocation mi) throws Throwable {<br \/>\n\/\/\u5728\u65b9\u6cd5\u6267\u884c\u524d\u6700\u65e9\u6267\u884c\uff0c\u5728\u65b9\u6cd5\u6267\u884c\u540e\u6700\u540e\u6267\u884c<br \/>\n\/\/ TODO Auto-generated method stub<br \/>\nSystem.out.println(&#8220;method &#8220;+mi.getMethod()+&#8221; is called on &#8220;+ mi.getThis()+&#8221; with args &#8220;+(mi.getArguments()));<br \/>\nObject ret = mi.proceed();<br \/>\nSystem.out.println( &#8220;methodInterceptor after &#8220;);<br \/>\nreturn ret;?? \/\/\u6ce8\uff1a\u82e5\u6ca1\u6709\u8fd4\u56deret,\u8c03\u7528\u4f1a\u51fa\u9519<br \/>\n}<\/p>\n<p>2\uff0c\u5b9e\u73b0\u81ea\u5df1\u7684\u4e1a\u52a1\u7c7b\u4e0e\u76f8\u5e94\u7684\u63a5\u53e3\uff0c\u62bd\u8c61\u7c7b\u7b49\u3002<br \/>\n\u5982\uff1a LoginDaoInf,? LoginDaoAbst,? LoginDaoImpl,????<\/p>\n<p>3, \u5c06\u589e\u5f3a\u4e0e\u4e1a\u52a1\u7c7b\u5bf9\u5e94\u8d77\u6765<\/p>\n<p>\u5728applicationContext.xml\u4e2d\u914d\u7f6e\uff1a??<br \/>\n\/\/\u4e1a\u52a1\u7c7b<br \/>\n&lt;bean id=&#8221;loginDaoImpl&#8221;&gt;&lt;\/bean&gt;<br \/>\n\/\/\u589e\u5f3a\u7c7b<br \/>\n&lt;bean id=&#8221;loginDaoAD&#8221;&gt;&lt;\/bean&gt;<\/p>\n<p>\/\/\u4e1a\u52a1\u7c7b\u4e0e\u589e\u5f3a\u7c7b\u88c5\u914d<br \/>\n&lt;bean id=&#8221;loginDaoInf&#8221;&gt;<br \/>\n\/\/\u6307\u5b9a\u4e1a\u52a1\u7c7b<br \/>\n&lt;property name=&#8221;target&#8221; ref = &#8220;loginDaoImpl&#8221; \/&gt;<br \/>\n\/\/\u6307\u5b9a\u589e\u5f3a<br \/>\n&lt;property name=&#8221;interceptorNames&#8221;&gt;<br \/>\n&lt;list&gt;<br \/>\n&lt;value&gt;loginDaoAD&lt;\/value&gt;? \/\/\u6ce8\uff0c\u53ef\u6307\u5b9a\u591a\u4e2a\u589e\u5f3a<br \/>\n&lt;\/list&gt;<br \/>\n&lt;\/property&gt;<br \/>\n&lt;\/bean&gt;???<\/p>\n<p>\u6ce8\uff1a \uff081\uff09 \u5bf9\u6307\u5b9a\u7684\u65b9\u6cd5\u5e94\u7528\u589e\u5f3a\u3002? \u5b9e\u73b0\u65b9\u5f0f\uff1a\u589e\u5f3aadvisor\u914d\u7f6e<br \/>\n\u4e0a\u8ff0\u7684\u914d\u7f6e\u65b9\u6cd5\u4f1a\u5bf9\u4e00\u7279\u5b9a\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\u8fdb\u884c\u589e\u5f3a\uff0c\u82e5\u60f3\u5bf9\u8be5\u7c7b\u6307\u5b9a\u7684\u65b9\u6cd5\u8fdb\u884c\u589e\u5f3a\uff0c\u8981\u914d\u7f6e\u4e00\u4e2aadvisor<br \/>\n\u8ba9\u5b83\u5305\u542badvice,\u4e4b\u540e\u5728\u4e1a\u52a1\u7c7b\u4e0e\u589e\u5f3a\u7c7b\u88c5\u914d\u65f6\u7528advisor\u4ee3\u66ffadvice\u5373\u53ef\u3002<br \/>\n??? \uff08advice \u5efa\u8bae\uff0cadvisor\u987e\u95ee\uff0c\u5efa\u8bae\u9ad8\u7ea7\u5f62\u5f0f\u5c31\u662f\u987e\u95ee\uff09<\/p>\n<p>\u914d\u7f6e\u65f6class\u4e3aNameMatchMethodPointcutAdvisor<br \/>\n&lt;bean id=&#8221;loginDaoADR&#8221;&gt;<br \/>\n&lt;property name = &#8220;mappedNames&#8221; &gt;<br \/>\n&lt;list&gt;<br \/>\n&lt;value&gt;check*&lt;\/value&gt;<br \/>\n&lt;value&gt;show&lt;\/value&gt;<br \/>\n??????????????? \/\/\u5bf9\u7c7b\u4ee5check\u5f00\u5934\u7684\u65b9\u6cd5\u53cashow\u65b9\u6cd5\u5e94\u7528\u589e\u5f3a\uff0c\u589e\u5f3a\u7528advice property\u6307\u5b9a\uff0c\u800c\u662f\u54ea\u4e2a\u7c7b\u5219<br \/>\n??????????????? \/\/\u8981\u5728\u88c5\u914d\u65f6\u624d\u80fd\u786e\u5b9a\u3002<br \/>\n&lt;\/list&gt;<br \/>\n&lt;\/property&gt;<br \/>\n&lt;property name=&#8221;advice&#8221; ref=&#8221;loginDaoAD&#8221;&gt;&lt;\/property&gt;? \/\/\u53ef\u770b\u51fa\uff1aadvisor = adivce + poingcut<br \/>\n&lt;\/bean&gt;<\/p>\n<p>\uff082\uff09 \u5bf9\u6307\u5b9a\u7684\u591a\u4e2a\u7c7b\u4e00\u6b21\u6307\u5b9a\u589e\u5f3a\u3002??? \u5b9e\u73b0\u65b9\u5f0f\uff1a\u6539\u53d8\u589e\u5f3a\u4e0e\u4e1a\u52a1\u7c7b\u7684\u88c5\u914d\u65b9\u5f0f\u3002<\/p>\n<p>1) BeanNameAutoProxyCreator\u65b9\u5f0f<\/p>\n<p>&lt;bean id=&#8221;loginDaoInf&#8221;&gt;<br \/>\n&lt;bean id=&#8221;AllAuto&#8221;&gt;<br \/>\n??? &lt;property name=&#8221;beanNames&#8221; &gt;<br \/>\n??????? &lt;list&gt;??????????????????? \/\/\u6307\u5b9a\u4e3a\u54ea\u4e9b\u7c7b\u5e94\u7528\u589e\u5f3a<br \/>\n??????????? &lt;value&gt;*DaoInf&lt;\/value&gt;<br \/>\n??????? &lt;\/list&gt;<br \/>\n??? &lt;\/property&gt;<br \/>\n??? &lt;property name=&#8221;interceptorNames&#8221;&gt;<br \/>\n??????? &lt;list&gt;????????????????? \/\/\u6307\u5b9a\u5e94\u7528\u7684\u589e\u5f3a\uff0cadvice\u6216advisor<br \/>\n??????????? &lt;value&gt;loginDaoAD&lt;\/value&gt;<br \/>\n??????? &lt;\/list&gt;<br \/>\n??? &lt;\/property&gt;<br \/>\n&lt;\/bean&gt;<\/p>\n<p>\uff083\uff09\u81ea\u52a8\u5bf9bean\u5e94\u7528\u9002\u5408\u7684\u589e\u5f3a\uff08\u67e5\u627e\u589e\u5f3a\u65f6\u53ea\u67e5\u627eadvisor,\u4e0d\u7528advice\uff0c\u56e0advice\u4e0d\u80fd\u6307\u5b9a\u65b9\u6cd5\uff09,\u5373\u5f53bean\u65b9\u6cd5\u82e5out()\u6267\u884c\u65f6\uff0c\u5c31\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u6240\u6709advisor\u4e2d\u67e5\u627e\uff0c\u82e5advisor\u6709\u9488\u5bf9out\u65b9\u6cd5\u7684\u914d\u7f6e\uff0c\u5982\uff1a<br \/>\n????????????? &lt;property name = &#8220;mappedNames&#8221; &gt;<br \/>\n??????????????????????? &lt;list&gt;<br \/>\n????????????????????????????? &lt;value&gt;out&lt;\/value&gt;<br \/>\n?????????????????????? &lt;\/list<br \/>\n?????????????? &lt;\/property&gt;<br \/>\n\u5c31\u5bf9\u6b64\u65b9\u6cd5\u5e94\u7528\u589e\u5f3a\u3002<\/p>\n<p>\u8fd9\u79cd\u81ea\u52a8\u589e\u5f3a\u589e\u5f3a\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u4e0b\uff1a<br \/>\n\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u52a0\u5165\uff1a<br \/>\n&lt;bean id=&#8221;autoProxyCreator&#8221;<br \/>\n??&gt;<br \/>\n&lt;\/bean&gt;<br \/>\n\u5c31\u81ea\u52a8\u5b8c\u6210\u65b9\u6cd5\u589e\u5f3a\u7684\u4fa6\u6d4b\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\uff0c\u5b8c\u6210\u65b9\u6cd5\u7684\u589e\u5f3a \u5b9e\u73b0MethodBeforeAdvice,AfterReturningAdvice,Met [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-1005","post","type-post","status-publish","format-standard","hentry","category-spring"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=1005"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1005\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}