{"id":228,"date":"2016-02-21T18:53:56","date_gmt":"2016-02-21T18:53:56","guid":{"rendered":"http:\/\/kaizen-koka.com\/?p=228"},"modified":"2017-04-02T03:10:51","modified_gmt":"2017-04-02T03:10:51","slug":"spring-aop","status":"publish","type":"post","link":"https:\/\/kaizen-koka.com\/?p=228","title":{"rendered":"Spring-AOP"},"content":{"rendered":"<p><a href=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/02\/Ai_gWMr6EMaBE5DiYPZUox55qOsok8yDrJ8xm_WHOEmF1.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"236\" data-permalink=\"https:\/\/kaizen-koka.com\/?attachment_id=236\" data-orig-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/02\/Ai_gWMr6EMaBE5DiYPZUox55qOsok8yDrJ8xm_WHOEmF1.jpg?fit=374%2C664&amp;ssl=1\" data-orig-size=\"374,664\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Ai_gWMr6EMaBE5DiYPZUox55qOsok8yDrJ8xm_WHOEmF\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/02\/Ai_gWMr6EMaBE5DiYPZUox55qOsok8yDrJ8xm_WHOEmF1.jpg?fit=169%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/02\/Ai_gWMr6EMaBE5DiYPZUox55qOsok8yDrJ8xm_WHOEmF1.jpg?fit=374%2C664&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/02\/Ai_gWMr6EMaBE5DiYPZUox55qOsok8yDrJ8xm_WHOEmF1.jpg?resize=374%2C664\" alt=\"Ai_gWMr6EMaBE5DiYPZUox55qOsok8yDrJ8xm_WHOEmF\" width=\"374\" height=\"664\" class=\"aligncenter size-full wp-image-236\" srcset=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/02\/Ai_gWMr6EMaBE5DiYPZUox55qOsok8yDrJ8xm_WHOEmF1.jpg?w=374&amp;ssl=1 374w, https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/02\/Ai_gWMr6EMaBE5DiYPZUox55qOsok8yDrJ8xm_WHOEmF1.jpg?resize=169%2C300&amp;ssl=1 169w\" sizes=\"auto, (max-width: 374px) 100vw, 374px\" \/><\/a><\/p>\n<pre class=\"brush:xml;\">\r\n&lt;dependency&gt;\r\n&nbsp;&nbsp; &nbsp;&lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n&nbsp;&nbsp; &nbsp;&lt;artifactId&gt;spring-aop&lt;\/artifactId&gt;\r\n&lt;\/dependency&gt;\r\n&lt;dependency&gt;\r\n&nbsp;&nbsp; &nbsp;&lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n&nbsp;&nbsp; &nbsp;&lt;artifactId&gt;spring-aspects&lt;\/artifactId&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">Instead of maintaining business logic and services logic together, develop services and business logic separately. Inject those services in to Business class using AOP<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">Maintaining Service logic separately&nbsp;from Business logic&nbsp;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">Services are like Transaction Services, Security, Mailing, JMS Services<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Aspect <\/strong>is nothing but a Service<br \/>\n\t<strong>Advice<\/strong> is nothing but a ServiceProvider. By using Advice you can provide ASPECT<br \/>\n\t<strong>PointCut<\/strong> is nothing but a point or condition to execute aspects for your business methods.<br \/>\n\t<strong>Adviser<\/strong> is nothing but pointcut with advice combination<br \/>\n\t<strong>Proxy<\/strong> is a waiver (proxy object will be generated by combining service and business code)<br \/>\n\t<strong>target<\/strong> is nothing but businees object<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">We can implement in 3 approaches<br \/>\n\tProgramatic approach<br \/>\n\tDeclarative approach<br \/>\n\tAnnotation approach<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Programatic approach<\/strong><\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\">org.springframework.aop.<strong>MethodBeforeAdvice<\/strong>;<br \/>\n\torg.springframework.aop.<strong>AfterReturningAdvice<\/strong>;<br \/>\n\torg.springframework.aop.<strong>ThrowsAdvice<\/strong>;<br \/>\n\torg.springframework.aop.framework.adapter.<strong>MethodBeforeAdviceInterceptor<\/strong>;<\/span>\n<\/p>\n<p>\n\t<br \/>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">Programatic approach:<\/span><\/span>\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class LoggingBefore &nbsp;implements MethodBeforeAdvice{\r\n\r\n&nbsp; &nbsp; Logger log = LoggerFactory.getLogger(LoggingBefore.class);\r\n\r\n&nbsp; &nbsp; @Override\r\n&nbsp; &nbsp; public void before(Method method, Object[] objects, Object o) throws Throwable {\r\n&nbsp; &nbsp; &nbsp; &nbsp; log.debug(&quot;before calling enrollStudent method&quot;);\r\n&nbsp; &nbsp; &nbsp; &nbsp; log.debug(method.getName());\r\n&nbsp; &nbsp; &nbsp; &nbsp; log.debug(objects.toString());\r\n&nbsp; &nbsp; }\r\n}<\/pre>\n<p>\n\t<strong><span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">Client Class:<\/span><\/span><\/strong>\n<\/p>\n<pre class=\"brush:java;\">\r\n\/\/create target\r\nBankServiceImpl bsi = new BankServiceImpl();\r\n\/\/create advice\r\nLogBeforeService lbs = new LogBeforeService()\r\n\r\n\/\/generating proxy by combining target + advice\r\nProxyFactoryBean pfb = new ProxyFactoryBean();\r\npfb.setTarget(bsi);\r\npfb.addAdvice(loggingBefore);\r\n\r\n\/\/get generated proxy object\r\nBankServiceImpl bsi =(BankServiceImpl)pfb.getObject();\r\n<\/pre>\n<p>\n\t<br \/>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Declarative approach<\/strong><br \/>\n\t&nbsp; &nbsp; <\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;!--Target --&gt;\r\n&nbsp; &nbsp;&lt;bean id=&quot;stdImpl&quot; class=&quot;com.springapp.mvc.service.EnrollStudentServiceImpl&quot;\/&gt;\r\n\r\n&nbsp; &nbsp; &lt;!-- advice --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;logBef&quot; class=&quot;com.springapp.mvc.aop.LoggingBefore&quot;\/&gt;\r\n&nbsp; &nbsp;\r\n&nbsp; &nbsp; &lt;!-- proxyBean--&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;proxyStudentImpl&quot; class=&quot;org.springframework.aop.framework.ProxyFactoryBean&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;target&quot; ref=&quot;stdImpl&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;interceptorNames&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;list&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;value&gt;logBef&lt;\/value&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/list&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/property&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Pointcuts :<\/strong> If you want to specify where exactly service need to be executed then we can use pointcut<br \/>\n\tStaticMethodMatcherPointcut<br \/>\n\tNameMatchMatcherPointcut<\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\npublic class EnrollPointCut extends StaticMethodMatcherPointcut {\r\n&nbsp; &nbsp; @Override\r\n&nbsp; &nbsp; public boolean matches(Method method, Class&lt;?&gt; aClass) {\r\n&nbsp; &nbsp; &nbsp; &nbsp; if(method.getName().equals(&quot;enrollStudent&quot;)){\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;\r\n&nbsp; &nbsp; &nbsp; &nbsp; }\r\n&nbsp; &nbsp; &nbsp; &nbsp; return false;\r\n&nbsp; &nbsp; }\r\n}<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Advicer:<\/strong><br \/>\n\tDefaultPointcutAdvicer<br \/>\n\tRegexpMethodPointcutAdvicer&nbsp; &nbsp;<\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n &lt;!--Target --&gt;\r\n&nbsp; &nbsp;&lt;bean id=&quot;stdImpl&quot; class=&quot;com.springapp.mvc.service.EnrollStudentServiceImpl&quot;\/&gt;\r\n\r\n&nbsp; &nbsp; &lt;!-- advice --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;logBef&quot; class=&quot;com.springapp.mvc.aop.LoggingBefore&quot;\/&gt;\r\n\r\n&nbsp; &nbsp; &lt;!-- pointCut --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;enrollPointCut&quot; class =&quot;com.springapp.mvc.aop.EnrollPointCut&quot;\/&gt;\r\n&nbsp; &nbsp; &lt;!-- Advisor --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;dpa&quot; class=&quot;org.springframework.aop.support.DefaultPointcutAdvisor&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;advice&quot; ref=&quot;logBef&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;pointcut&quot; ref=&quot;enrollPointCut&quot;\/&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n\r\n&nbsp; &nbsp; &lt;!-- proxyBean--&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;proxyStudentImpl&quot; class=&quot;org.springframework.aop.framework.ProxyFactoryBean&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;target&quot; ref=&quot;stdImpl&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;interceptorNames&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;list&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;value&gt;dpa&lt;\/value&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/list&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/property&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&lt;dependency&gt; &nbsp;&nbsp; &nbsp;&lt;groupId&gt;org.springframework&lt;\/groupId&gt; &nbsp;&nbsp; &nbsp;&lt;artifactId&gt;spring-aop&lt;\/artifactId&gt; &lt;\/dependency&gt; &lt;dependency&gt; &nbsp;&nbsp; &nbsp;&lt;groupId&gt;org.springframework&lt;\/groupId&gt; &nbsp;&nbsp; &nbsp;&lt;artifactId&gt;spring-aspects&lt;\/artifactId&gt; &lt;\/dependency&gt; Instead of maintaining business logic and services logic together, develop services and business logic separately. Inject those services [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[16],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-spring-technologies"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p70lnf-3G","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=228"}],"version-history":[{"count":5,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":237,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions\/237"}],"wp:attachment":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}