{"id":6441,"date":"2012-08-27T11:48:38","date_gmt":"2012-08-27T06:18:38","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6441"},"modified":"2012-08-30T13:32:53","modified_gmt":"2012-08-30T08:02:53","slug":"integrating-of-spring-aop-with-grails-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-of-spring-aop-with-grails-application\/","title":{"rendered":"Integrating Spring AOP with Grails Application"},"content":{"rendered":"<p><strong>AOP means Aspect Oriented Programming<\/strong><\/p>\n<ol>\n<li>Enables encapsulation of \tfunctionality that affects multiple classes in separate units.<\/li>\n<li>Complements Object Oriented \tProgramming.<\/li>\n<li>Cross Cutting Concerns : \tFunctionality whose implementation spans multiple modules.<\/li>\n<\/ol>\n<p><strong>Different From OOPS<\/strong><\/p>\n<p style=\"text-align: left\">Sometimes we want to implement common functionality through out some classes, through out some common method. We can achieve that functionality by implementing separate module without touching core logic.<\/p>\n<p style=\"padding-top: 10px\">\n<p><strong>Take the example of Banking Domain<\/strong><\/p>\n<p>In this case when person calls the withDraw() function, we want to log the detail of each step, that is performed in back end, i.e. what parameters were passed to each function, we want to log such details. Using OOPS concepts, we have to write logging code in each method, it&#8217;s very difficult to implement, but by using AOP we can implement this functionality as separate module without touching the core logic. We can implement security on method level by using some pattern as a Join Point with AOP.<\/p>\n<p style=\"padding-top: 10px\">\n<p><strong>Where can we use AOP<\/strong><\/p>\n<ol>\n<li>Logging<\/li>\n<li>Transaction Management<\/li>\n<li>Profiling<\/li>\n<li>Security<\/li>\n<\/ol>\n<p style=\"padding-top: 10px\">\n<p><strong>What does Spring AOP contains:<\/strong><\/p>\n<ol>\n<li>Join Point : A point \tin the execution of program.<\/li>\n<li>Advice : Code to be executed at a \tjoin point that has been  selected by point cut.<\/li>\n<li>Point cut : An expression mapped \tto a join point.<\/li>\n<\/ol>\n<p style=\"padding-top: 10px\">\n<p>Now Question comes in mind, <strong>How to integrate Spring AOP with Grails Application<\/strong><\/p>\n<p style=\"padding-top: 10px\">\n<p><strong>It&#8217;s very easy to integrate Spring AOP with Grails Application<\/strong><\/p>\n<ol>\n<li>Firstly we \thave to add configuration of Spring AOP in resources.groovy<\/li>\n<li>Now write \tfollowing line of codes in resources.groovy<\/li>\n<\/ol>\n<p>[groovy]<br \/>\nbeans = {<br \/>\nxmlns aop:&quot;http:\/\/www.springframework.org\/schema\/aop&quot;<br \/>\naspectBean(com.test.aop.LoggerInterceptor)<br \/>\naop.config(&quot;proxy-target-class&quot;:true) {<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p style=\"padding-top: 10px;text-align: left\">3.\u00a0\u00a0 In this case we create the bean of \tAOP class by using aspectBean(com.test.aop.LoggerInterceptor).<\/p>\n<p style=\"padding-top: 10px;text-align: left\">4.\u00a0\u00a0\u00a0 aop.config(&#8220;proxy-target-class&#8221;:true) \tIt&#8217;s enable AOP to create proxy of target class.<\/p>\n<p style=\"padding-top: 10px;text-align: left\">5. \u00a0 Create Groovy \tclass that contains the implementation of AOP.<\/p>\n<p style=\"padding-top: 10px;text-align: left\">6.\u00a0\u00a0 I will show the example of logging functionality with using AOP.<\/p>\n<p>[groovy]<br \/>\npackage com.test.aop;<br \/>\nimport org.aspectj.lang.JoinPoint;<br \/>\nimport org.aspectj.lang.annotation.After;<br \/>\nimport org.aspectj.lang.annotation.Aspect;<br \/>\nimport org.aspectj.lang.annotation.Before;<br \/>\nimport org.aspectj.lang.annotation.Pointcut;<br \/>\nimport org.springframework.stereotype.Service;<br \/>\nimport java.util.Arrays;<br \/>\n@Aspect<br \/>\npublic class LoggerInterceptor {<br \/>\nprivate static Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);<br \/>\n@Before(&quot;within(com.service..*)&quot;)<br \/>\npublic void logBefore(JoinPoint joinPoint){<br \/>\nString logMessage = String.format(&quot;Beginning of each method: %s.%s(%s)&quot;,<br \/>\njoinPoint.getTarget().getClass().getName(),<br \/>\njoinPoint.getSignature().getName(),<br \/>\nArrays.toString(joinPoint.getArgs()));<br \/>\nlogger.info(logMessage);<br \/>\n}<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p style=\"padding-top: 10px\">\n<p style=\"text-align: left\">@Aspect : Any bean with a class annotated as an aspect will be automatically detected by Spring.<\/p>\n<p style=\"text-align: left\">@Before : It&#8217;s Before advice that executes before the matched method.<\/p>\n<p style=\"text-align: left\">JoinPoint : Provides access to the current join point (target object, description of advised method,parameters types, parameters values, etc).<\/p>\n<p style=\"text-align: left\">within(com.service..*) : This method will executed before calling any method in service class under package com.service..*<\/p>\n<p style=\"text-align: left\">You can easily apply advice to any public method, any parameterized method to any Class.<\/p>\n<p style=\"padding-top: 10px\">To know more about AOP, you can take the reference of Spring AOP official site.<br \/>\n<a href=\"http:\/\/static.springsource.org\/spring\/docs\/2.5.5\/reference\/aop.html\" target=\"_blank\">http:\/\/static.springsource.org\/spring\/docs\/2.5.5\/reference\/aop.html<\/a><\/p>\n<p style=\"padding-top: 20px\">\n<p>Thanks &amp; Regards,<br \/>\nMohit Garg<\/p>\n<p>mohit@intelligrape.com<br \/>\n@gargmohit143<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AOP means Aspect Oriented Programming Enables encapsulation of functionality that affects multiple classes in separate units. Complements Object Oriented Programming. Cross Cutting Concerns : Functionality whose implementation spans multiple modules. Different From OOPS Sometimes we want to implement common functionality through out some classes, through out some common method. We can achieve that functionality by [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":15},"categories":[7],"tags":[888,4840,4841],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6441"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6441"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6441\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}