{"id":1543,"date":"2016-09-06T02:48:31","date_gmt":"2016-09-06T02:48:31","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1543"},"modified":"2016-09-06T16:21:02","modified_gmt":"2016-09-06T16:21:02","slug":"aspect-oriented-programming-spring","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/aspect-oriented-programming-spring\/","title":{"rendered":"Aspect Oriented Programming Spring"},"content":{"rendered":"<p>Aspect Oriented Programming answers cross-cutting concern.Cross-cutting concern is one that can affect the whole application and should be centralized in one location in code as possible, such as transaction management, authentication, logging, security etc.<\/p>\n<p><strong>Aspect<\/strong> &#8211; A module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement.<\/p>\n<p><strong>Advice<\/strong> &#8211; This is the actual action to be taken either before or after the method execution. This is actual piece of code that is invoked during program execution by Spring AOP framework.<\/p>\n<p><strong>LoggingAspect.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.shapes;\r\n\r\nimport org.aspectj.lang.annotation.Aspect;\r\nimport org.aspectj.lang.annotation.Before;\r\n\r\n@Aspect\r\npublic class LoggingAspect \r\n{\r\n\t@Before(&quot;execution(public String getName())&quot;)\r\n\tpublic void loggingAdvice()\r\n\t{\r\n\t\tSystem.out.println(&quot;This is Logging Advice&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>DrawingApp.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.shapes;\r\n\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\r\n\r\npublic class DrawingApp \r\n{\r\n\tpublic static void main(String&#x5B;] args) \r\n\t{\r\n\t\tApplicationContext objContext = new ClassPathXmlApplicationContext(&quot;Spring-Customer.xml&quot;);\r\n\t\tShapeService  objCircle = (ShapeService) objContext.getBean(&quot;shapeService&quot;, ShapeService.class);\r\n\t\tSystem.out.println(objCircle.getObjCircle().getName());\r\nSystem.out.println(objCircle.getObjTriangle().getName());\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Circle.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.shapes;\r\n\r\npublic class Circle {\r\n\tprivate String name;\r\n\r\n\tpublic String getName() {\r\n\t\treturn name;\r\n\t}\r\n\r\n\tpublic void setName(String name) {\r\n\t\tthis.name = name;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Triangle.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.shapes;\r\n\r\npublic class Triangle {\r\n\tprivate String name;\r\n\r\n\tpublic String getName() {\r\n\t\treturn name;\r\n\t}\r\n\r\n\tpublic void setName(String name) {\r\n\t\tthis.name = name;\r\n\t}\t\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nThis is Logging Advice\r\nCircle Name\r\nThis is Logging Advice\r\nTriangle Name\r\n<\/pre>\n<p><em>@Before(&#8220;execution(public String getName())&#8221;)<\/em><br \/>\nApplies for all the methods with getName() signature (Triangle and Circle Class)<\/p>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nThis is Logging Advice\r\nCircle Name\r\nThis is Logging Advice\r\nTriangle Name\r\n<\/pre>\n<p><em>@Before(&#8220;execution(public String com.mugil.shapes.Circle.getName())&#8221;)<\/em><br \/>\nApplies for the methods with getName() signature (Circle Class)<\/p>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nThis is Logging Advice\r\nCircle Name\r\n<\/pre>\n<p><em>@Before(&#8220;execution(public String com.mugil.shapes.*.getName())&#8221;)<\/em><br \/>\nApplies for getName() method in Triangle and Circle Class<\/p>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nThis is Logging Advice\r\nCircle Name\r\nThis is Logging Advice\r\nTriangle Name\r\n<\/pre>\n<p><em>@Before(&#8220;execution(public * get*())&#8221;)<\/em><br \/>\nApplies for all getters method in Triangle,Circle and ShapeService Class<\/p>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nThis is Logging Advice\r\nThis is Logging Advice\r\nCircle Name\r\nThis is Logging Advice\r\nThis is Logging Advice\r\nTriangle Name\r\n<\/pre>\n<p><em>@Before(&#8220;execution(public String getName(*))&#8221;)<\/em><br \/>\nApplies to all getName() method with one or more argument.<\/p>\n<p><em>@Before(&#8220;execution(public String getName(..))&#8221;)<\/em><br \/>\nApplies to all getName() method with zero or more argument.<\/p>\n<p><strong>Pointcut<\/strong> &#8211; This is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns as we will see in our AOP examples.<\/p>\n<p><strong>DrawingApp.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.shapes;\r\n\r\nimport org.aspectj.lang.annotation.Aspect;\r\nimport org.aspectj.lang.annotation.Before;\r\nimport org.aspectj.lang.annotation.Pointcut;\r\n\r\n@Aspect\r\npublic class LoggingAspect \r\n{\t\r\n\t@Before(&quot;allGetters()&quot;)\r\n\tpublic void loggingAdvice1()\r\n\t{\r\n\t\tSystem.out.println(&quot;This is Logging Advice 1&quot;);\r\n\t}\r\n\t\r\n\t@Before(&quot;allGetters()&quot;)\r\n\tpublic void loggingAdvice2()\r\n\t{\r\n\t\tSystem.out.println(&quot;This is Logging Advice 2&quot;);\r\n\t}\r\n\t\t\r\n\t@Pointcut(&quot;execution(* getName())&quot;)\r\n\tpublic void allGetters()\r\n\t{\t\r\n\t}\r\n}\r\n<\/pre>\n<ol>\n<li>allGetters() method will be called when ever the getName() method of Circle (or) Triangle gets called<\/li>\n<li>Before allGetters() method gets called the loggingAdvice1() and loggingAdvice2() method gets called.<\/li>\n<\/ol>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nThis is Logging Advice 1\r\nThis is Logging Advice 2\r\nCircle Name\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n @Pointcut(&quot;execution(* com.mugil.shapes.*.get*(..))&quot;)\r\n<\/pre>\n<p>Applies for all the Getters within shapes package<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n @Pointcut(&quot;execution(public * com.mugil.shapes.*.get*(..))&quot;)\r\n<\/pre>\n<p>Applies for all the <strong>Public Getters<\/strong> within shapes package<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n @Pointcut(&quot;within(com.mugil.shapes.*)&quot;)\r\n<\/pre>\n<p><strong>within applies for everything within class whereas execution applies only to the methods.<\/strong><\/p>\n<p>Runs for all methods getters and setters in  <strong>com.mugil.shapes.*<\/strong> package.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Pointcut(&quot;args(..)&quot;)\r\n<\/pre>\n<p>Runs for all methods getters and setters.<\/p>\n<p><strong>Join point<\/strong> &#8211; This represents a point in your application where you can plug-in AOP aspect. You can also say, it is the actual place in the application where an action will be taken using Spring AOP framework.<\/p>\n<p>Using JoinPoint we can have access to the Object in the advice method.<\/p>\n<p><strong>LoggingAspect.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.shapes;\r\n\r\nimport org.aspectj.lang.JoinPoint;\r\nimport org.aspectj.lang.annotation.Aspect;\r\nimport org.aspectj.lang.annotation.Before;\r\nimport org.aspectj.lang.annotation.Pointcut;\r\n\r\n@Aspect\r\npublic class LoggingAspect {\r\n\t\r\n\t@Before(&quot;allGetters()&quot;)\r\n\t public void LogginAdvice(JoinPoint joinpoint)\r\n    {\r\n\t\tSystem.out.println(&quot;advice is run&quot;);\r\n\t    System.out.println(joinpoint.toString());\r\n    }\r\n\t\r\n\t@Before(&quot;args(name)&quot;)\r\n\tpublic void LoggingAdvice2(String name)\r\n\t{\r\n\t\tSystem.out.println(&quot;-- &quot; + name);\r\n\t}\r\n\t\r\n\t@Pointcut(&quot;execution(* com.mugil.shapes.*.get*())&quot;)\r\n\tpublic void allGetters()\r\n\t{\t\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>DrawingApp.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class DrawingApp {\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tApplicationContext objContext = new ClassPathXmlApplicationContext(&quot;Spring-Customer.xml&quot;);\r\n\t\tShapeService  objCircle = (ShapeService) objContext.getBean(&quot;shapeService&quot;, ShapeService.class);\r\nobjCircle.getObjTriangle().setName(&quot;Test Tri&quot;);\r\n\t\tSystem.out.println(objCircle.getObjTriangle().getName());\t\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nadvice is run\r\nexecution(Triangle com.mugil.shapes.ShapeService.getObjTriangle())\r\n-- Test Tri\r\nadvice is run\r\nexecution(Triangle com.mugil.shapes.ShapeService.getObjTriangle())\r\nadvice is run\r\nexecution(String com.mugil.shapes.Triangle.getName())\r\nTest Tri\r\n<\/pre>\n<p><strong>LoggingAspect.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Aspect\r\npublic class LoggingAspect {\r\n\t\r\n\t@Before(&quot;allGetters()&quot;)\r\n\t public void LogginAdvice(JoinPoint joinpoint)\r\n    {\r\n\t\tSystem.out.println(&quot;advice is run&quot;);\r\n\t    System.out.println(joinpoint.toString());\r\n    }\r\n\t\r\n\t@AfterReturning(pointcut =&quot;args(name)&quot;, returning=&quot;returnString&quot;)\r\n\tpublic void LoggingAdvice2(String name, String returnString)\r\n\t{\r\n\t\tSystem.out.println(&quot;This is Input to Method &quot; + name);\r\n\t\tSystem.out.println(&quot;This is Returned  from  Method &quot; + returnString);\r\n\t}\r\n\t\r\n\t@AfterThrowing(pointcut=&quot;args(name)&quot;, throwing=&quot;ex&quot;)\r\n\tpublic void LoggingAdvice4(String name, RuntimeException ex)\r\n\t{\r\n\t\tSystem.out.println(&quot;This will be Printed incase of Exception is Thrown in Method&quot;);\t\t\r\n\t\tSystem.out.println(ex);\r\n\t}\r\n\r\n\t\r\n\t@Pointcut(&quot;execution(* com.mugil.shapes.*.get*())&quot;)\r\n\tpublic void allGetters()\r\n\t{\t\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p><strong>Using Around<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Around(&quot;allGetters()&quot;)\r\n\tpublic void LoggingAdvice2(ProceedingJoinPoint pjp)\r\n\t{\r\n\t\tObject objObject = null;\t\t\r\n\t\t\r\n\t\ttry {\r\n\t\t\tSystem.out.println(&quot;Code before Method Execution Goes here&quot;);\t\t\t\r\n\t\t\tobjObject = pjp.proceed();\r\n\t\t\tSystem.out.println(&quot;Code after Method Execution Goes here&quot;);\r\n\t\t} catch (Throwable e) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Aspect Oriented Programming answers cross-cutting concern.Cross-cutting concern is one that can affect the whole application and should be centralized in one location in code as possible, such as transaction management, authentication, logging, security etc. Aspect &#8211; A module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called&hellip; <a href=\"https:\/\/codethataint.com\/blog\/aspect-oriented-programming-spring\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[191],"tags":[],"class_list":["post-1543","post","type-post","status-publish","format-standard","hentry","category-aop"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1543"}],"version-history":[{"count":9,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1543\/revisions"}],"predecessor-version":[{"id":1552,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1543\/revisions\/1552"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}