{"id":13,"date":"2013-03-20T17:12:35","date_gmt":"2013-03-20T17:12:35","guid":{"rendered":"http:\/\/www.yeetrack.com\/post\/2013-03-20\/java%20Spring%E6%A1%86%E6%9E%B6AOP%EF%BC%88%E9%9D%A2%E5%90%91%E5%88%87%E9%9D%A2%E7%BC%96%E7%A8%8B%EF%BC%89"},"modified":"2013-09-27T22:36:47","modified_gmt":"2013-09-27T14:36:47","slug":"javaspring%e6%a1%86%e6%9e%b6aop%ef%bc%88%e9%9d%a2%e5%90%91%e5%88%87%e9%9d%a2%e7%bc%96%e7%a8%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=13","title":{"rendered":"java Spring\u6846\u67b6AOP\uff08\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff09"},"content":{"rendered":"<p>Sping\u6846\u67b6\u4e24\u4e2a\u91cd\u8981\u601d\u60f3\u662fIOC\u548cAOP\uff0cAOP\u5168\u540d\u662faspect oriented programming,\u5373\u9762\u5411\u5207\u9762\u7f16\u7a0b\u7684\u610f\u601d\u3002AOP\u601d\u60f3\u5141\u8bb8\u6211\u4eec\u5728\u6267\u884c\u4e1a\u52a1\u4ee3\u7801\u7684\u524d\u540e\u6267\u884c\u53e6\u5916\u7684\u4ee3\u7801\uff0c\u5982\u5199\u65e5\u5fd7\u3001\u9a8c\u8bc1\u6743\u9650\u3002\u5176\u5b9eServlet\u4e2d\u7684Filter\u5c31\u4f53\u73b0\u4e86AOP\u601d\u60f3\u3002<br \/> Spring\u4e2d\uff0cAOP\u9700\u8981\u7ee7\u627f\u7279\u5b9a\u7684\u63a5\u53e3\u3002\u8fd9\u4e9b\u5b9e\u73b0\u4e86AOP\u63a5\u53e3\u7684\u7c7b\u88ab\u79f0\u4e3aInterceptor\uff08\u62e6\u622a\u5668\uff09\uff0cInterceptor\u6709\u591a\u79cd\uff0c\u6709\u65b9\u6cd5\u524d\u62e6\u622a\u5668\u3001\u65b9\u6cd5\u540e\u62e6\u622a\u5668\u3001around\u62e6\u622a\u5668\u3001\u5f02\u5e38\u62e6\u622a\u5668\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u65b9\u6cd5\u524d\u62e6\u622a\u5668\uff1a<br \/> MethodBeforeAdviceImpl.java\uff1a<\/p>\n<pre config=\"brush:java;toolbar:false;\">\npackage com.yeetrack.mavenSpring;\n\n    import java.lang.reflect.Method;\n    import java.util.Arrays;\n\n    import org.springframework.aop.MethodBeforeAdvice;\n\n    public class MethodBeforeAdviceImpl implements MethodBeforeAdvice\n    {\n        public void before(Method arg0, Object[] arg1, Object arg2)\n                throws Throwable\n        {\n            \/\/ TODO Auto-generated method stub\n            System.out.println(&quot;\u8fd9\u662f\u63d2\u5165\u8fdb\u6765\u7684\u65b9\u6cd5\uff0c\u65b9\u6cd5\u8c03\u7528\u524d\u6267\u884c---&quot;);\n            System.out.println(&quot;Method: &quot;+ arg0.getName()); \/\/\u8f93\u51fa\u65b9\u6cd5\u540d\u5b57\n            System.out.println(&quot;Args: &quot; + Arrays.asList(arg1)); \/\/\u8f93\u51fa\u65b9\u6cd5\u53c2\u6570\u5217\u8868\n            System.out.println(&quot;Object: &quot;+arg2); \/\/\u8f93\u51fa\u5bf9\u8c61\n        }\n    }\n<\/pre>\n<p>\u5f53\u6211\u4eec\u5728applicationContext.xml\u4e3a\u67d0\u4e2a\u7c7b\u7684\u65b9\u6cd5A\u5b9a\u4e49\u4e86\u6b64\u62e6\u622a\u5668\u540e\uff0cspring\u5728\u6267\u884c\u65b9\u6cd5A\u4e4b\u524d\u4f1a\u8c03\u7528MethodBeforeAdviceImpl\u7c7b\u4e2d\u7684before\u65b9\u6cd5\uff0c\u53c2\u6570\u5206\u522b\u4e3a\u65b9\u6cd5A\u7684\u65b9\u6cd5\u540d\u3001\u53c2\u6570\u5217\u8868\u548cA\u6240\u5728\u7684\u7c7b\u5b9e\u4f8b\u3002<\/p>\n<p>\u5728applicationContext.xml\u4e2d\u914d\u7f6e\u8be5\u62e6\u622a\u5668\uff1a<\/p>\n<pre config=\"brush:html;toolbar:false;\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n    &lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n    xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:p=&quot;http:\/\/www.springframework.org\/schema\/p&quot;\n    xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\n    xmlns:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot;\n    xsi:schemaLocation=&quot;     \n          http:\/\/www.springframework.org\/schema\/beans     \n          http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd     \n          http:\/\/www.springframework.org\/schema\/context     \n          http:\/\/www.springframework.org\/schema\/context\/spring-context-3.0.xsd \n          http:\/\/www.springframework.org\/schema\/aop     \n          http:\/\/www.springframework.org\/schema\/aop\/spring-aop-3.0.xsd&quot;\n    default-autowire=&quot;byName&quot;&gt;\n    &lt;!-- \u62e6\u622a\u5668\u5bf9\u8c61 --&gt;\n    &lt;bean id=&quot;methodBeforeAdviceImpl&quot; class=&quot;com.yeetrack.mavenSpring.MethodBeforeAdviceImpl&quot;&gt;&lt;\/bean&gt;\n    &lt;!-- \u62e6\u622a\u5668\u914d\u7f6e --&gt;\n    &lt;bean id=&quot;theAdvisor&quot; class=&quot;org.springframework.aop.support.NameMatchMethodPointcutAdvisor&quot;&gt;\n        &lt;!-- \u62e6\u622a\u5668\u5c5e\u6027\u8bbe\u7f6e --&gt;\n        &lt;property name=&quot;advice&quot;&gt;\n            &lt;ref local=&quot;methodBeforeAdviceImpl&quot;\/&gt; \n        &lt;\/property&gt;\n        &lt;!-- \u62e6\u622a\u6240\u6709\u65b9\u6cd5 --&gt;\n        &lt;property name=&quot;mappedName&quot; value=&quot;*&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n\n\n    &lt;!-- DAO\u5bf9\u8c61 --&gt;\n    &lt;bean id=&quot;daoImpl&quot; class=&quot;com.yeetrack.mavenSpring.DaoImpl&quot;&gt;&lt;\/bean&gt;\n\n    &lt;!-- Service\u5bf9\u8c61 --&gt;\n    &lt;bean id=&quot;serviceImpl&quot; class=&quot;com.yeetrack.mavenSpring.ServiceImpl&quot;&gt;\n        &lt;!-- \u8bbe\u7f6edao\u5c5e\u6027 --&gt;\n        &lt;property name=&quot;dao&quot; ref=&quot;daoImpl&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n\n    &lt;!-- Spring\u4ee3\u7406\u7c7b --&gt;\n    &lt;bean id=&quot;service&quot; class=&quot;org.springframework.aop.framework.ProxyFactoryBean&quot;&gt;\n        &lt;property name=&quot;interceptorNames&quot; value=&quot;theAdvisor&quot;&gt;&lt;\/property&gt;&lt;!-- \u62e6\u622a\u5668\u8bbe\u7f6e --&gt;\n        &lt;property name=&quot;target&quot;&gt;  &lt;!-- \u88ab\u62e6\u622a\u7684\u5bf9\u8c61 --&gt;\n            &lt;ref local=&quot;serviceImpl&quot;\/&gt;\n        &lt;\/property&gt;\n    &lt;\/bean&gt;\n\n    &lt;\/beans&gt; \n<\/pre>\n<p>\u914d\u7f6e\u5b8c\u4e4b\u540e\uff0c\u4e0d\u7528\u4fee\u6539\u6211\u4eec\u7684SpringTest.java\u7684\u4ee3\u7801\uff0c\u518d\u6b21\u8fd0\u884cSpringTest\u53ef\u4ee5\u770b\u5230\u9996\u5148\u6267\u884c\u4e86\u62e6\u622a\u5668\u4e2d\u7684before\u65b9\u6cd5\uff0c\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u8fd9\u662f\u63d2\u5165\u8fdb\u6765\u7684\u65b9\u6cd5\uff0c\u65b9\u6cd5\u8c03\u7528\u524d\u6267\u884c---\n\n```html\nMethod: sayHello\n    Args: [Good Day!]\n    Object: com.yeetrack.mavenSpring.ServiceImpl@67eb366\n    Hello, Good Day!  \n<\/code><\/pre>\n<p><a href=\"https:\/\/www.yeetrack.com\/post\/2013-03-20\/java%20Spring%E6%A1%86%E6%9E%B6IOC%EF%BC%88%E6%8E%A7%E5%88%B6%E5%8F%8D%E8%BD%AC%EF%BC%89\">java Spring\u6846\u67b6IOC\uff08\u63a7\u5236\u53cd\u8f6c\uff09<\/a><br \/> <a href=\"https:\/\/www.yeetrack.com\/post\/2013-03-20\/java%20Spring%E6%A1%86%E6%9E%B6AOP%EF%BC%88%E9%9D%A2%E5%90%91%E5%88%87%E9%9D%A2%E7%BC%96%E7%A8%8B%EF%BC%89\">java Spring\u6846\u67b6AOP\uff08\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff09<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sping\u6846\u67b6\u4e24\u4e2a\u91cd\u8981\u601d\u60f3\u662fIOC\u548cAOP\uff0cAOP\u5168\u540d\u662faspect oriented programming,\u5373\u9762\u5411\u5207\u9762\u7f16\u7a0b\u7684\u610f\u601d\u3002AOP\u601d\u60f3\u5141\u8bb8\u6211\u4eec\u5728\u6267\u884c\u4e1a\u52a1\u4ee3\u7801\u7684\u524d\u540e\u6267\u884c\u53e6\u5916\u7684\u4ee3\u7801\uff0c\u5982\u5199\u65e5\u5fd7\u3001\u9a8c&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[8,7,5],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-coding","tag-java","tag-7","tag-5"],"views":2911,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions\/188"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}