{"id":1001,"date":"2010-10-02T00:30:30","date_gmt":"2010-10-02T00:30:30","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1001"},"modified":"2010-10-02T00:30:30","modified_gmt":"2010-10-02T00:30:30","slug":"aop","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/10\/02\/aop\/","title":{"rendered":"AOP"},"content":{"rendered":"<p>?\u6240\u8c13AOP\uff0c\u5373\u9762\u5411\u65b9\u9762\u7f16\u7a0b\uff08Aspect Oriented Programming\uff09,\u65b9\u9762\uff08Aspect\uff09\u201d\u5b83\u5c31\u662f\u5c06\u90a3\u4e9b\u4e0e\u4e1a\u52a1\u65e0\u5173\uff0c\u5374\u4e3a\u4e1a\u52a1\u6a21\u5757\u6240\u5171\u540c\u8c03\u7528\u7684\u903b\u8f91\u6216\u8d23\u4efb\uff0c\u4f8b\u5982\u4e8b\u52a1\u5904\u7406\u3001\u65e5\u5fd7\u7ba1\u7406\u3001\u6743\u9650\u63a7\u5236\u7b49\uff0c\u5c01\u88c5\u8d77\u6765\uff0c\u4fbf\u4e8e\u51cf\u5c11\u7cfb\u7edf\u7684\u91cd\u590d\u4ee3\u7801\uff0c\u964d\u4f4e\u6a21\u5757\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u5e76\u6709\u5229\u4e8e\u672a\u6765\u7684\u53ef\u64cd\u4f5c\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<span style=\"color: #006400; font-family: Tahoma;\"><span style=\"color: #000000;\">\u5b83\u7684\u4f5c\u7528\u66f4\u591a\u5730\u662f\u5173\u6ce8\u4e8e\u7cfb\u7edf\u7684\u67d0\u4e00\u65b9\u9762<\/p>\n<p><\/span><\/span>?? \u201c\u8bbe\u8ba1\u6a21\u5f0f\u201d\u7684\u89e6\u89d2\u59cb\u7ec8\u5728\u63a5\u53e3\u4e0e\u62bd\u8c61\u4e2d\u5927\u505a\u6587\u7ae0\uff0c\u800c\u5bf9\u4e8e\u5bf9\u8c61\u5185\u90e8\u5219\u65e0\u80fd\u4e3a\u529b\u3002AOP\u5229\u7528\u4e00\u79cd\u79f0\u4e3a\u201c\u6a2a\u5207\u201d\u7684\u6280\u672f\uff0c\u5256\u89e3\u5f00\u5c01\u88c5\u7684\u5bf9\u8c61\u5185\u90e8\uff0c\u5e76\u5c06\u90a3\u4e9b\u5f71\u54cd\u4e86\u591a\u4e2a\u7c7b\u7684\u884c\u4e3a\u5c01\u88c5\u5230\u4e00\u4e2a\u53ef\u91cd\u7528\u6a21\u5757\uff0c\u5e76\u5c06\u5176\u540d\u4e3a\u201cAspect\u201d<\/p>\n<p>??? Spring AOP\u5b9e\u73b0AOP\u6280\u672f\u4ece\u672c\u8d28\u4e0a\u6765\u8bb2\uff0c\u662f\u5229\u7528\u4e86JDK\u63d0\u4f9b\u7684\u52a8\u6001\u4ee3\u7406\u6280\u672f\u3002\u800c\u4ece\u5b9e\u9645\u7684\u5b9e\u73b0\u65b9\u5f0f\u6765\u770b\uff0c\u5219\u662f\u5229\u7528\u4e86IoC\uff08Inversion of Control\uff0c\u53cd\u8f6c\u6a21\u5f0f\uff09\u673a\u5236\uff0c\u540c\u65f6\u91c7\u7528\u4e86AOP\u8054\u76df\uff08AOP Alliance\uff09\u7684\u901a\u7528AOP\u63a5\u53e3\u3002\u9996\u5148\uff0cSpring AOP\u901a\u8fc7xml\u914d\u7f6e\u6587\u4ef6\u914d\u7f6e\u4e86pointcut\uff0c\u5e76\u5229\u7528Interceptor\uff08\u62e6\u622a\u673a\uff09\u4f5c\u4e3a\u8bbe\u5b9a\u7684\u89e6\u53d1\u6761\u4ef6\u3002Interceptor\u662f\u7531\u7528\u6237\u81ea\u5b9a\u4e49\u7684\uff0c\u5b83\u76f8\u5f53\u4e8e\u662fAOP\u4e2d\u7684advice\uff0c\u4f46\u8be5Interceptor\u9700\u8981\u5b9e\u73b0AOP\u8054\u76df\u7684\u901a\u7528AOP\u63a5\u53e3\uff0c\u4f8b\u5982 org.aopalliance.intercept.MethodInterceptor\u3002\u6700\u540e\u5b9a\u4e49\u4e00\u4e2aSpring AOP ProxyFactory\u7528\u4e8e\u52a0\u8f7d\u6267\u884cAOP\u7ec4\u4ef6\uff0c\u5e76\u5229\u7528IoC\u673a\u5236\u5c06advice\u6ce8\u5165\u5230\u63a5\u53e3\u4ee5\u53ca\u5b9e\u73b0\u7c7b\u4e2d\u3002<\/p>\n<p>?Spring AOP\u7814\u7a76<\/p>\n<p>Spring AOP\u4f7f\u7528\u7eafJava\u5b9e\u73b0\uff0c\u4e0d\u9700\u8981\u7279\u522b\u7684\u7f16\u8bd1\u8fc7\u7a0b\uff0c\u4e5f\u4e0d\u9700\u8981\u63a7\u5236\u7c7b\u88c5\u8f7d\u5c42\u6b21<\/p>\n<p>\u3002\u4e0eJBoss AOP\u76f8\u540c\uff0c\u5b83\u4ecd\u7136\u5229\u7528\u4e86\u62e6\u622a\u5668\u5b8c\u6210\u5bf9\u65b9\u6cd5\u7684\u62e6\u622a\u3002\u7136\u800c\uff0cSpring AOP<\/p>\n<p>\u5b9e\u73b0AOP\u7684\u4e3b\u8981\u6280\u672f\u5374\u4e3b\u8981\u6765\u81ea\u4e8eAOP\u8054\u76df\uff0c\u5982\u62e6\u622a\u5668\u5e94\u5b9e\u73b0<\/p>\n<p>org.aopalliance.intercept.MethodInterceptor \u63a5\u53e3\uff0c\u800c\u6240\u6709advice\u5fc5\u987b\u5b9e\u73b0<\/p>\n<p>org.aopalliance.aop.Advice\u6807\u7b7e\u63a5\u53e3\u3002\u6b64\u5916\uff0cSpring\u5b9e\u73b0AOP\u7684\u76ee\u6807\u4e5f\u4e0d\u540c\u4e8e\u5176<\/p>\n<p>\u4ed6\u5927\u90e8\u5206AOP\u6846\u67b6\uff0c\u5b83\u7684\u76ee\u6807\u4e0d\u662f\u63d0\u4f9b\u53ca\u5176\u5b8c\u5584\u7684AOP\u5b9e\u73b0\uff0c\u800c\u662f\u63d0\u4f9b\u4e00\u4e2a\u548c<\/p>\n<p>Spring IoC\u7d27\u5bc6\u6574\u5408\u7684AOP\u5b9e\u73b0\uff0c\u5e2e\u52a9\u89e3\u51b3\u4f01\u4e1a\u5e94\u7528\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u3002\u56e0\u6b64\uff0cSpring<\/p>\n<p>AOP\u7684\u529f\u80fd\u901a\u5e38\u662f\u548cSpring IoC\u5bb9\u5668\u8054\u5408\u4f7f\u7528\u7684\u3002AOP Advice\u662f\u7528\u666e\u901a\u7684bean\u5b9a\u4e49<\/p>\n<p>\u8bed\u6cd5\u6765\u5b9a\u4e49\u7684\uff0cAdvice\u548cpointcut\u672c\u8eab\u7531Spring IoC \u7ba1\u7406\u3002\u8fd9\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u5176\u4ed6<\/p>\n<p>AOP\u5b9e\u73b0\u7684\u533a\u522b\u3002<\/p>\n<p>3.2.3.1 \u5207\u5165\u70b9\uff08pointcut\uff09<\/p>\n<p>Spring\u7684\u5207\u5165\u70b9\u6a21\u578b\u80fd\u591f\u4f7fpointcut\u72ec\u7acb\u4e8eadvice\u7c7b\u578b\u88ab\u91cd\u7528\u3002\u540c\u6837\u7684pointcut<\/p>\n<p>\u6709\u53ef\u80fd\u63a5\u53d7\u4e0d\u540c\u7684advice\u3002\u5c06 Pointcut\u63a5\u53e3\u5206\u6210\u4e24\u4e2a\u90e8\u5206\u6709\u5229\u4e8e\u91cd\u7528\u7c7b\u548c\u65b9\u6cd5\u7684<\/p>\n<p>\u5339\u914d\u90e8\u5206\uff0c\u5e76\u4e14\u7ec4\u5408\u7ec6\u7c92\u5ea6\u7684\u64cd\u4f5c\uff08\u5982\u548c\u53e6\u4e00\u4e2a\u65b9\u6cd5\u5339\u914d\u5668\u6267\u884c\u4e00\u4e2a\u201c\u5e76\u201d\u7684\u64cd<\/p>\n<p>\u4f5c\uff09\u3002<\/p>\n<p>\u5728Spring\u7684\u5207\u5165\u70b9\u4e2d\uff0corg.springframework.aop.Pointcut\u63a5\u53e3\u662f\u91cd\u8981\u7684\u63a5\u53e3\uff0c<\/p>\n<p>\u5b83\u7528\u6765\u6307\u5b9a\u901a\u77e5\u5230\u7279\u5b9a\u7684\u7c7b\u548c\u65b9\u6cd5\u76ee\u6807\u3002\u5b8c\u6574\u7684\u63a5\u53e3\u5b9a\u4e49\u5982\u4e0b:<br \/>\npublic interface Pointcut<br \/>\n{<br \/>\n??? ClassFilter getClassFilter();<br \/>\n??? MethodMatcher getMethodMatcher();<br \/>\n}<\/p>\n<p>ClassFilte\u7c7b\u578b\u4e5f\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u8be5\u63a5\u53e3\u88ab\u7528\u6765\u5c06\u5207\u5165\u70b9\u9650\u5236\u5230\u4e00\u4e2a\u7ed9\u5b9a\u7684\u76ee\u6807\u7c7b<\/p>\n<p>\u7684\u96c6\u5408\u3002 \u5982\u679cmatches()\u6c38\u8fdc\u8fd4\u56detrue\uff0c\u6240\u6709\u7684\u76ee\u6807\u7c7b\u90fd\u5c06\u88ab\u5339\u914d\u3002<br \/>\npublic interface ClassFilter<br \/>\n{<br \/>\n??? boolean matches(Class clazz);<br \/>\n}<\/p>\n<p>MethodMatcher\u63a5\u53e3\u901a\u5e38\u66f4\u52a0\u91cd\u8981\u3002\u5b8c\u6574\u7684\u63a5\u53e3\u5b9a\u4e49\u5982\u4e0b\uff1a<br \/>\npublic interface MethodMatcher<br \/>\n{<br \/>\n??? boolean matches(Method m, Class targetClass);<br \/>\n??? boolean matches(Method m, Class targetClass, Object[] args);<br \/>\n??? boolean isRuntime();<br \/>\n}<\/p>\n<p>matches(Method, Class) \u65b9\u6cd5\u88ab\u7528\u6765\u6d4b\u8bd5\u8fd9\u4e2a\u5207\u5165\u70b9\u662f\u5426\u5339\u914d\u76ee\u6807\u7c7b\u7684\u7ed9\u5b9a\u65b9\u6cd5<\/p>\n<p>\u3002\u8fd9\u4e2a\u6d4b\u8bd5\u53ef\u4ee5\u5728AOP\u4ee3\u7406\u521b\u5efa\u7684\u65f6\u5019\u6267\u884c\uff0c\u907f\u514d\u5728\u6240\u6709\u65b9\u6cd5\u8c03\u7528\u65f6\u90fd\u9700\u8981\u8fdb\u884c\u6d4b<\/p>\n<p>\u8bd5\u3002\u5982\u679c2\u4e2a\u53c2\u6570\u7684matches()\u65b9\u6cd5\u5bf9\u67d0\u4e2a\u65b9\u6cd5\u8fd4\u56detrue\uff0c\u5e76\u4e14MethodMatcher\u7684<\/p>\n<p>isRuntime()\u4e5f\u8fd4\u56detrue\uff0c\u90a3\u4e483\u4e2a\u53c2\u6570\u7684matches()\u65b9\u6cd5\u5c06\u5728\u6bcf\u6b21\u65b9\u6cd5\u8c03\u7528\u7684\u65f6\u5019<\/p>\n<p>\u88ab\u8c03\u7528\u3002\u8fd9\u4f7f\u5207\u5165\u70b9\u80fd\u591f\u5728\u76ee\u6807advice\u88ab\u6267\u884c\u4e4b\u524d\u7acb\u5373\u67e5\u770b\u4f20\u9012\u7ed9\u65b9\u6cd5\u8c03\u7528\u7684\u53c2<\/p>\n<p>\u6570\u3002\u7531\u4e8e\u5927\u90e8\u5206 MethodMatcher\u90fd\u662f\u9759\u6001\u7684\uff0c\u610f\u5473\u7740isRuntime()\u65b9\u6cd5\u4f1a\u8fd4\u56de<\/p>\n<p>false\u3002\u6b64\u79cd\u60c5\u51b5\u4e0b\uff0c3\u4e2a\u53c2\u6570\u7684matches()\u65b9\u6cd5\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>Spring AOP\u63d0\u4f9b\u4e86\u51e0\u4e2a\u5b9e\u7528\u7684\u5207\u5165\u70b9\u5b9e\u73b0\uff0c\u5176\u4e2d\u8f83\u4e3a\u5e38\u7528\u7684\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u5207\u5165\u70b9<\/p>\n<p>\uff1aorg.springframework.aop.support.RegexpMethodPointcut\uff0c\u5b83\u4f7f\u7528Perl 5\u7684<\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\u3002\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6a21\u5f0f\u7684\u5217\u8868\u3002\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u5339\u914d<\/p>\n<p>\uff0c\u90a3\u4e2a\u5207\u5165\u70b9\u5c06\u88ab\u8ba1\u7b97\u6210 true\u3002\u7528\u6cd5\u5982\u4e0b\uff1a<br \/>\n&lt;bean id=&#8221;settersAndAbsquatulatePointcut&#8221;<br \/>\n???&gt;<br \/>\n??? &lt;property name=&#8221;patterns&#8221;&gt;<br \/>\n??????? &lt;list&gt;<br \/>\n??????????? &lt;value&gt;.*get.*&lt;\/value&gt;<br \/>\n??????????? &lt;value&gt;.*absquatulate&lt;\/value&gt;<br \/>\n??????? &lt;\/list&gt;<br \/>\n??? &lt;\/property&gt;<br \/>\n&lt;\/bean&gt;<\/p>\n<p>\u4e0d\u8fc7\uff0c\u66f4\u591a\u60c5\u51b5\u4e0b\u662f\u76f4\u63a5\u4f7f\u7528RegexpMethodPointcut\u4e00\u4e2a\u5b9e\u7528\u5b50\u7c7b\uff1a<\/p>\n<p>RegexpMethodPointcutAdvisor\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u540c\u65f6\u5f15\u7528\u4e00\u4e2aadvice\uff08\u5728Spring AOP<\/p>\n<p>\u4e2d\uff0cadvice\u53ef\u4ee5\u662f\u62e6\u622a\u5668\uff0c\u4e5f\u53ef\u4ee5\u662fbefore advice\uff0cthrows advice\u7b49)\u3002\u8fd9\u5c31\u7b80<\/p>\n<p>\u5316\u4e86bean\u7684\u88c5\u914d\uff0c\u56e0\u4e3a\u4e00\u4e2abean\u53ef\u4ee5\u540c\u65f6\u5f53\u4f5cpointcut\u548cadvice\uff0c\u5982\u4e0b\u6240\u793a\uff1a<br \/>\n&lt;bean id=&#8221;myPointcutAdvisor&#8221;<\/p>\n<p>class=&#8221;org.springframework.aop.support.RegexpMethodPointcutAdvisor&#8221;&gt;<br \/>\n??? &lt;property name=&#8221;advice&#8221;&gt;<br \/>\n??????? &lt;ref local=&#8221;MyInterceptor&#8221; \/&gt;<br \/>\n??? &lt;\/property&gt;<br \/>\n??? &lt;property name=&#8221;patterns&#8221;&gt;<br \/>\n??????? &lt;list&gt;<br \/>\n??????????? &lt;value&gt;.*save.*&lt;\/value&gt;<br \/>\n??????????? &lt;value&gt;.*do.*&lt;\/value&gt;<br \/>\n??????? &lt;\/list&gt;<br \/>\n??? &lt;\/property&gt;<br \/>\n&lt;\/bean&gt;<\/p>\n<p>\u6ce8\u610f\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684myPointcutAdvisor\uff0c\u5728Spring AOP\u4e2d\uff0c\u4e00\u4e2aadvisor\u5c31\u662f\u4e00\u4e2a<\/p>\n<p>aspect\u5b8c\u6574\u7684\u6a21\u5757\u5316\u8868\u793a\u3002\u901a\u8fc7advisor\uff0c\u53ef\u4ee5\u5c06pointcut\u548cadvice\uff08\u5728\u6b64\u5904\u5373\u4e3a<\/p>\n<p>MyInterceptor\uff09\u7ed1\u5b9a\u8d77\u6765\u3002<\/p>\n<p>3.2.3.2 \u901a\u77e5\uff08advice\uff09<\/p>\n<p>Spring AOP\u7684advice\u53ef\u4ee5\u8de8\u8d8a\u591a\u4e2a\u88abadvice\u5bf9\u8c61\u5171\u4eab\uff0c\u6216\u8005\u6bcf\u4e2a\u88abadvice\u5bf9\u8c61\u6709<\/p>\n<p>\u81ea\u5df1\u7684advice\u3002\u8981\u5b9e\u73b0advice\uff0c\u6700\u7b80\u5355\u7684\u505a\u6cd5\u5c31\u662f\u5b9a\u4e49\u4e00\u4e2a\u62e6\u622a\u5668\uff08Interceptor<\/p>\n<p>\uff09\u3002\u5b83\u91c7\u7528\u4e86AOP\u8054\u76df\uff08AOP Alliance\uff09\u7684\u901a\u7528AOP\u63a5\u53e3\uff08\u63a5\u53e3\u5b9a\u4e49\u4e3a<\/p>\n<p>aopalliance.jar\uff09\u3002\u8981\u5b9e\u73b0advice\uff0c\u9700\u8981\u5b9e\u73b0aopalliance.jar\u4e2d\u5b9a\u4e49\u7684<\/p>\n<p>MethodInterceptor\u63a5\u53e3\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e1a\u52a1\u5bf9\u8c61\u63a5\u53e3BusinessObject\u53ca\u5176\u5b9e\u73b0\u7c7b<\/p>\n<p>BusinessObjectImpl\uff0c\u8be5\u4e1a\u52a1\u5bf9\u8c61\u80fd\u591f\u5b58\u50a8\u6570\u636e\uff0c\u5176\u5b9a\u4e49\u5982\u4e0b\uff1a<br \/>\npublic interface BusinessObject<br \/>\n{<br \/>\n??? public void save();<br \/>\n}<br \/>\npublic class BusinessObjectImpl implements BusinessObject<br \/>\n{<br \/>\n??? public void save()<br \/>\n??? {<br \/>\n???????? System.out.println(&#8220;saving domain object&#8230;&#8230;&#8221;);<br \/>\n??? }<br \/>\n}<\/p>\n<p>\u73b0\u5728\u9700\u8981\u4e3a\u4e1a\u52a1\u5bf9\u8c61BusinessObject\u7684Save()\u65b9\u6cd5\uff0c\u63d0\u4f9bLock\u673a\u5236\u3002\u6839\u636eSpring<\/p>\n<p>AOP\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2aLockInterceptor\u6765\u5b9e\u73b0MethodInterceptor\u63a5<\/p>\n<p>\u53e3\uff1a<br \/>\nimport org.aopalliance.intercept.MethodInterceptor;<br \/>\nimport org.aopalliance.intercept.MethodInvocation;<\/p>\n<p>public class LockInterceptor implements MethodInterceptor<br \/>\n{<br \/>\n??? public Object invoke(MethodInvocation invocation) throws Throwable<br \/>\n??? {<br \/>\n??????? \/\/ TODO Auto-generated method stub<br \/>\n??????? lock();<br \/>\n??????? Object ret= invocation.proceed();<br \/>\n??????? unlock();<br \/>\n??????? return ret;<br \/>\n??? }<br \/>\n??? private void lock()<br \/>\n??? {<br \/>\n??????? System.out.println(&#8220;lock domain object&#8230;&#8221;);<br \/>\n??? }<br \/>\n??? private void unlock()<br \/>\n??? {<br \/>\n??????? System.out.println(&#8220;unlock domain object&#8230;&#8221;);<br \/>\n??? }<br \/>\n}<\/p>\n<p>\u4e3a\u5c06interceptor\u4e0e\u5177\u4f53\u7684advice\u7ed1\u5b9a\u8d77\u6765\uff0c\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6ebean\uff1a<br \/>\n&amp;lt;bean id=&#8221;MyInterceptor&#8221;<\/p>\n<p>class=&#8221;test.aop.spring.LockInterceptor&#8221;\/&amp;gt;<\/p>\n<p>3.2.3.3 AOP\u4ee3\u7406\u4e0eIoC\u5bb9\u5668<\/p>\n<p>\u7531\u4e8eSpring\u4e2d\u63d0\u4f9b\u4e86IoC\u5bb9\u5668\uff08\u4f8b\u5982BeanFactory\uff09\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Ioc\u673a\u5236\uff0c<\/p>\n<p>\u5229\u7528ProxyFactoryBean\u6765\u521b\u5efa AOP\u4ee3\u7406\u3002ProxyFactoryBean\u548c\u5176\u4ed6Spring\u7684<\/p>\n<p>FactoryBean\u5b9e\u73b0\u4e00\u6837\uff0c\u5f15\u5165\u4e00\u4e2a\u95f4\u63a5\u7684\u5c42\u6b21\u3002\u5982\u679c\u4f60\u5b9a\u4e49\u4e00\u4e2a\u540d\u5b57\u4e3afoo\u7684<\/p>\n<p>ProxyFactoryBean\uff0c\u5f15\u7528foo\u7684\u5bf9\u8c61\u6240\u770b\u5230\u7684\u4e0d\u662f ProxyFactoryBean\u5b9e\u4f8b\u672c\u8eab\uff0c<\/p>\n<p>\u800c\u662f\u7531\u5b9e\u73b0ProxyFactoryBean\u7684\u7c7b\u7684 getObject()\u65b9\u6cd5\u6240\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u8fd9\u4e2a\u65b9\u6cd5<\/p>\n<p>\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u88c5\u4e86\u76ee\u6807\u5bf9\u8c61 \u7684AOP\u4ee3\u7406\u3002<\/p>\n<p>AOP\u4ee3\u7406\u5229\u7528\u7684\u662fJava\u7684\u52a8\u6001\u4ee3\u7406\u6280\u672f\uff0c\u901a\u8fc7\u5b83\u5c31\u53ef\u4ee5\u52a0\u8f7d\u5e76\u6267\u884cAOP\u7ec4\u4ef6\u3002\u540c\u65f6<\/p>\n<p>\uff0c\u8fd8\u9700\u8981\u901a\u8fc7IoC\u7684\u65b9\u5f0f\u5c06advice\u6ce8\u5165\u5230\u63a5\u53e3\u4ee5\u53ca\u5176\u5b9e\u73b0\u7c7b\u3002\u4ee5\u524d\u9762\u7684\u4e1a\u52a1\u5bf9\u8c61<\/p>\n<p>BusinessObject\u4e3a\u4f8b\uff0c\u5728xml\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u914d\u7f6e\u5982\u4e0b\uff1a<br \/>\n&lt;bean id=&#8221;myAOPProxy&#8221;<\/p>\n<p>class=&#8221;org.springframework.aop.framework.ProxyFactoryBean&#8221;&gt;<br \/>\n??? &lt;property name=&#8221;proxyInterfaces&#8221;&gt;<br \/>\n?????? &lt;value&gt;test.aop.spring.BusinessObject&lt;\/value&gt;<br \/>\n??? &lt;\/property&gt;<br \/>\n??? &lt;property name=&#8221;target&#8221;&gt;<br \/>\n?????? &lt;ref local=&#8221;impl&#8221; \/&gt;<br \/>\n??? &lt;\/property&gt;<br \/>\n??? &lt;property name=&#8221;interceptorNames&#8221;&gt;<br \/>\n?????? &lt;value&gt;myPointcutAdvisor&lt;\/value&gt;<br \/>\n??? &lt;\/property&gt;<br \/>\n&lt;\/bean&gt;<br \/>\n&lt;bean id=&#8221;impl&#8221;\/&gt;<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u5bf9pointcut\u3001advice\u3001advisor\u548cAOP\u4ee3\u7406\u7684\u914d\u7f6e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8f7b\u6613\u5730\u5728<\/p>\n<p>Spring\u4e2d\u5b9e\u73b0AOP\uff0c\u4f8b\u5982\uff1a<br \/>\nimport org.springframework.context.ApplicationContext;<br \/>\nimport<\/p>\n<p>org.springframework.context.support.FileSystemXmlApplicationContext;<\/p>\n<p>public class App<br \/>\n{<br \/>\n??? private BusinessObject bo = null;<br \/>\n??? public static void main(String[] args)<br \/>\n??? {<br \/>\n??????? ApplicationContext ctx=new FileSystemXmlApplicationContext<\/p>\n<p>(&#8220;Bean.xml&#8221;);<br \/>\n??????? bo= (BusinessObject) ctx.getBean(&#8220;myAOPProxy&#8221;);<br \/>\n??????? bo.save();<br \/>\n??? }<br \/>\n}<\/p>\n<p>\u9996\u5148\uff0c\u901a\u8fc7AOP\u4ee3\u7406\u83b7\u5f97BusinessObject\u5bf9\u8c61\u3002\u5f53\u8c03\u7528BusinessObject\u5bf9\u8c61\u7684<\/p>\n<p>save()\u65b9\u6cd5\u65f6\uff0c\u62e6\u622a\u5668 LockInterceptor\u6839\u636eRegexpMethodPointcutAdvisor\u914d\u7f6e<\/p>\n<p>\u7684pointcut\u548cadvice\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5224\u5b9a\u8be5\u65b9\u6cd5\u7684\u8c03\u7528\u4e3ajoin point\uff0c\u4ece\u800c\u62e6\u622a\u8be5<\/p>\n<p>\u65b9\u6cd5\u8c03\u7528\uff0c\u5e76\u6ce8\u5165advice\u7684\u6267\u884c\u903b\u8f91\uff0c\u5373lock()\u548cunlock()\uff0c\u6700\u7ec8\u5b9e\u73b0\u4e86AOP\u3002<\/p>\n<p>3.2.3.4 \u5f15\u5165\uff08introduction\uff09<\/p>\n<p>\u5728Spring AOP\u4e2d\uff0c\u5c06introduction\u5f53\u4f5cadvice\u6765\u5904\u7406\u3002\u4e0e\u4e00\u822c\u7684advice\u4e00\u6837\uff0c<\/p>\n<p>introduction advice\u76f8\u5f53\u4e8e\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u62e6\u622a\u901a\u77e5\uff0c\u9700\u8981\u5b9e\u73b0<\/p>\n<p>IntroductionAdvisor\u548cIntroductionInterceptor\u63a5\u53e3\uff0c\u800c<\/p>\n<p>IntroductionInterceptor\u63a5\u53e3\u7ee7\u627f\u81eaMethodInterceptor\uff1a<br \/>\npublic interface IntroductionInterceptor extends MethodInterceptor<br \/>\n{<br \/>\n??? boolean implementsInterface(Class intf);<br \/>\n}<\/p>\n<p>Introduction\u901a\u77e5\u4e0d\u80fd\u88ab\u7528\u4e8e\u4efb\u4f55pointcut\uff0c\u56e0\u4e3a\u5b83\u53ea\u80fd\u4f5c\u7528\u4e8e\u7c7b\u5c42\u6b21\u4e0a\uff0c\u800c\u4e0d<\/p>\n<p>\u662f\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u53ea\u7528InterceptionIntroductionAdvisor\u6765\u5b9e\u73b0\u5bfc\u5165\u901a\u77e5\uff0c\u5b83\u6709<\/p>\n<p>\u4e0b\u9762\u7684\u65b9\u6cd5\uff1a<br \/>\npublic interface InterceptionIntroductionAdvisor extends<\/p>\n<p>InterceptionAdvisor<br \/>\n{<br \/>\n??? ClassFilter getClassFilter();<br \/>\n??? IntroductionInterceptor getIntroductionInterceptor();<br \/>\n??? Class[] getInterfaces();<br \/>\n}<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4ee5JBoss AOP\u4e00\u8282\u4e2d\u7684\u4f8b\u5b50\u6765\u8bf4\u660eintroduction\u5728Spring AOP\u4e2d\u7684\u5e94\u7528<\/p>\n<p>\u3002\u6211\u4eec\u7684\u76ee\u6807\u4ecd\u7136\u662f\u4e3a\u4e00\u4e2a\u5df2\u6709\u7684\u4e1a\u52a1\u5bf9\u8c61\u5f15\u5165\u7b2c\u4e09\u65b9\u63a5\u53e3Tracing\uff1a<br \/>\npublic interface Tracing<br \/>\n{<br \/>\n??? void enableTracing();<br \/>\n??? void disableTracing();<br \/>\n??? boolean enabled();<br \/>\n}<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u505a\u5927\u91cf\u8f6c\u5316\u7684IntroductionInterceptor\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u7ee7\u627f<\/p>\n<p>org.springframework.aop.support.DelegatingIntroductionInterceptor \u5b9e\u73b0<\/p>\n<p>\u7c7b\u3002\u5f53\u7136\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5b9e\u73b0IntroductionInterceptor\u63a5\u53e3\uff0c\u4f46\u662f\u5927\u591a\u6570\u60c5\u51b5\u4e0b<\/p>\n<p>DelegatingIntroductionInterceptor\u662f\u6700\u5408\u9002\u7684\u3002<\/p>\n<p>DelegatingIntroductionInterceptor\u7684\u8bbe\u8ba1\u662f\u5c06introduction\u59d4\u6258\u5230\u771f\u6b63\u5b9e\u73b0<\/p>\n<p>introduction \u63a5\u53e3\u7684\u63a5\u53e3\uff0c\u9690\u85cf\u5b8c\u6210\u8fd9\u4e9b\u5de5\u4f5c\u7684\u62e6\u622a\u5668\u3002\u59d4\u6258\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u65b9\u6cd5<\/p>\n<p>\u53c2\u6570\u8bbe\u7f6e\u5230\u4efb\u4f55\u5bf9\u8c61\u4e2d\uff1b\u9ed8\u8ba4\u7684\u59d4\u6258\u5c31\u662f\u81ea\u5df1\uff08\u5f53\u65e0\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5\u88ab\u4f7f\u7528\u65f6\uff09<\/p>\n<p>\u3002\u8fd9\u6837\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u91cc\uff0c\u59d4\u6258\u662fDelegatingIntroductionInterceptor\u7684\u5b50\u7c7b<\/p>\n<p>TracingMixin\u3002\u7ed9\u5b9a\u4e00\u4e2a\u59d4\u6258\uff08\u9ed8\u8ba4\u662f\u81ea\u8eab\uff09\u7684<\/p>\n<p>DelegatingIntroductionInterceptor\u5b9e\u4f8b\u5bfb\u627e\u88ab\u8fd9\u4e2a\u59d4\u6258\uff08\u800c\u4e0d\u662f<\/p>\n<p>IntroductionInterceptor\uff09\u5b9e\u73b0\u7684\u6240\u6709\u63a5\u53e3\uff0c\u5e76\u652f\u6301\u5b83\u4eec\u4e2d\u4efb\u4f55\u4e00\u4e2a\u5bfc\u5165\u3002\u5b50\u7c7b<\/p>\n<p>\u5982TracingMixi\u4e5f\u53ef\u80fd\u8c03\u7528suppressInterflace(Class intf) \u65b9\u6cd5\u6765\u9690\u85cf\u4e0d\u5e94\u66b4<\/p>\n<p>\u9732\u7684\u63a5\u53e3\u3002\u7136\u800c\uff0c\u4e0d\u7ba1IntroductionInterceptor \u51c6\u5907\u652f\u6301\u591a\u5c11\u63a5\u53e3\uff0c<\/p>\n<p>IntroductionAdvisor\u5c06\u63a7\u5236\u54ea\u4e2a\u63a5\u53e3\u5c06\u88ab\u5b9e\u9645\u66b4\u9732\u3002\u4e00\u4e2a\u5bfc\u5165\u7684\u63a5\u53e3\u5c06\u9690\u85cf\u76ee\u6807<\/p>\n<p>\u7684\u540c\u4e00\u4e2a\u63a5\u53e3\u7684\u6240\u6709\u5b9e\u73b0\u3002<\/p>\n<p>\u8fd9\u6837\uff0cTracingMixin\u7ee7\u627fDelegatingIntroductionInterceptor\u5e76\u81ea\u5df1\u5b9e\u73b0\u63a5\u53e3<\/p>\n<p>Tracing\u3002\u7236\u7c7b\u81ea\u52a8\u9009\u62e9\u652f\u6301introduction\u7684Tracing\uff0c\u6240\u4ee5\u6211\u4eec\u4e0d\u9700\u8981\u6307\u5b9a\u5b83\u3002<\/p>\n<p>\u7528\u8fd9\u79cd\u65b9\u6cd5\u6211\u4eec\u53ef\u4ee5\u5bfc\u5165\u4efb\u610f\u6570\u91cf\u7684\u63a5\u53e3\u3002<br \/>\npublic class TracingMixin extends DelegatingIntroductionInterceptor<\/p>\n<p>implements Tracing<br \/>\n{<br \/>\n??? private boolean enabled;<br \/>\n??? public void enableTracing ()<br \/>\n??? {<br \/>\n??????? this.enabled = true;<br \/>\n??? }<\/p>\n<p>??? public void disableTracing ()<br \/>\n??? {<br \/>\n??????? this. enabled = false;<br \/>\n??? }<\/p>\n<p>??? public boolean enabled()<br \/>\n??? {<br \/>\n??????? return this.enabled;<br \/>\n??? }<br \/>\n??? public Object invoke(MethodInvocation invocation) throws Throwable<br \/>\n??? {?????<br \/>\n??????? return super.invoke(invocation);<br \/>\n??? }<br \/>\n}<\/p>\n<p>\u901a\u5e38\u4e0d\u8981\u9700\u8981\u6539\u5199invoke()\u65b9\u6cd5\uff1a\u5b9e\u73b0DelegatingIntroductionInterceptor\u5c31\u8db3<\/p>\n<p>\u591f\u4e86\uff0c\u5982\u679c\u662f\u5f15\u5165\u7684\u65b9\u6cd5\uff0cDelegatingIntroductionInterceptor\u5b9e\u73b0\u4f1a\u8c03\u7528\u59d4\u6258<\/p>\n<p>\u65b9\u6cd5\uff0c \u5426\u5219\u7ee7\u7eed\u6cbf\u7740\u8fde\u63a5\u70b9\u5904\u7406\u3002<\/p>\n<p>\u6240\u9700\u7684introduction advisor\u662f\u5f88\u7b80\u5355\u7684\u3002\u53ea\u9700\u4fdd\u5b58\u4e00\u4e2a\u72ec\u7acb\u7684TracingMixin\u5b9e<\/p>\n<p>\u4f8b\uff0c\u5e76\u6307\u5b9a\u5bfc\u5165\u7684\u63a5\u53e3\uff0c\u5728\u8fd9\u91cc\u5c31\u662fTracing\u3002\u6b64\u65f6\uff0cTracingMixin\u6ca1\u6709\u76f8\u5173\u914d\u7f6e<\/p>\n<p>\uff0c\u6240\u4ee5\u6211\u4eec\u7b80\u5355\u5730\u4f7f\u7528new\u6765\u521b\u5efa\u5b83\u3002<\/p>\n<p>public class TracingMixinAdvisor extends DefaultIntroductionAdvisor<br \/>\n{<br \/>\n??? public TracingMixinAdvisor() {<br \/>\n??????? super(new TracingMixin(),Tracing.class);<br \/>\n??? }<br \/>\n}<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u7b80\u5355\u5730\u4f7f\u7528\u8fd9\u4e2aadvisor\u3002\u5b83\u4e0d\u9700\u8981\u4efb\u4f55\u914d\u7f6e\u3002\uff08\u4f46\u662f\uff0c\u6709\u4e00\u70b9\u662f\u5fc5<\/p>\n<p>\u8981\u7684\uff1a\u5c31\u662f\u4e0d\u53ef\u80fd\u5728\u6ca1\u6709IntroductionAdvisor \u7684\u60c5\u51b5\u4e0b\u4f7f\u7528<\/p>\n<p>IntroductionInterceptor\u3002\uff09 \u548c\u5f15\u5165\u4e00\u6837\uff0c\u901a\u5e38 advisor\u5fc5\u987b\u662f\u9488\u5bf9\u6bcf\u4e2a\u5b9e\u4f8b<\/p>\n<p>\u7684\uff0c\u5e76\u4e14\u662f\u6709\u72b6\u6001\u7684\u3002\u6211\u4eec\u4f1a\u6709\u4e0d\u540c\u7684TracingMixinAdvisor\u3002\u6bcf\u4e2a\u88ab\u901a\u77e5\u5bf9\u8c61\uff0c<\/p>\n<p>\u4f1a\u6709\u4e0d\u540c\u7684 TracingMixin\u3002advisor\u7ec4\u6210\u4e86\u88ab\u901a\u77e5\u5bf9\u8c61\u7684\u72b6\u6001\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<p>\u5728Spring\u4e2d\uff0cSpring AOP\u7684\u6838\u5fc3API\u5df2\u7ecf\u57fa\u672c\u7a33\u5b9a\u4e86\u3002\u548cSpring\u7684\u5176\u5b83\u90e8\u5206\u4e00\u6837\uff0c<\/p>\n<p>AOP\u6846\u67b6\u662f\u6a21\u5757\u5316\u7684\uff0c\u5728\u4fdd\u7559\u57fa\u7840\u8bbe\u8ba1\u7684\u540c\u65f6\u63d0\u4f9b\u6269\u5c55\u3002\u5728Spring 1.1\u52301.2\u9636\u6bb5<\/p>\n<p>\u6709\u5f88\u591a\u5730\u65b9\u53ef\u80fd\u4f1a\u6709\u6240\u63d0\u9ad8\uff0c\u4f46\u662f\u8fd9\u4e9b\u5730\u65b9\u4e5f\u4fdd\u7559\u4e86\u5411\u540e\u517c\u5bb9\u6027\u3002\u5b83\u4eec\u662f\uff1a<\/p>\n<p>\uff08\u4e00\uff09\u6027\u80fd\u7684\u63d0\u9ad8\uff1aAOP\u4ee3\u7406\u7684\u521b\u5efa\u7531\u5de5\u5382\u901a\u8fc7\u7b56\u7565\u63a5\u53e3\u5904\u7406\u3002\u56e0\u6b64\u80fd\u591f\u652f\u6301\u989d\u5916<\/p>\n<p>\u7684AOP \u4ee3\u7406\u7c7b\u578b\u800c\u4e0d\u5f71\u54cd\u7528\u6237\u4ee3\u7801\u6216\u6838\u5fc3\u5b9e\u73b0\u3002<br \/>\n\uff08\u4e8c\uff09\u66f4\u5177\u8868\u8fbe\u529b\u7684pointcut\uff1aSpring\u76ee\u524d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5177\u6709\u8868\u8fbe\u529b\u7684\u5207\u5165\u70b9\u63a5\u53e3<\/p>\n<p>\uff0c\u540c\u65f6\u6dfb\u52a0\u4e86\u66f4\u591a\u7684\u5207\u5165\u70b9\u5b9e\u73b0\u3002Spring\u6b63\u5728\u8003\u8651\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u4f46\u5177\u6709\u5f3a\u5927\u8868\u8fbe<\/p>\n<p>\u5f0f\u8bed\u8a00\u7684\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>?\u6240\u8c13AOP\uff0c\u5373\u9762\u5411\u65b9\u9762\u7f16\u7a0b\uff08Aspect Oriented Programming\uff09,\u65b9\u9762\uff08Aspect\uff09\u201d [&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-1001","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\/1001","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=1001"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1001\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}