{"id":24939,"date":"2015-08-03T23:56:58","date_gmt":"2015-08-03T18:26:58","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=24939"},"modified":"2015-08-04T09:46:22","modified_gmt":"2015-08-04T04:16:22","slug":"integrating-spring-aop-with-aem","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-spring-aop-with-aem\/","title":{"rendered":"Integrating Spring AOP with AEM"},"content":{"rendered":"<p><strong>Spring AOP<\/strong><\/p>\n<p>Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects.Some of cases where we can use it are to control some of common things like logging , adding some extra feature without actually touching the core logic.<\/p>\n<p>This blog post will explain how to enable aspects in <a title=\"Our AMC Case Studies\" href=\"http:\/\/www.tothenew.com\/success-stories\/#amc\">our AEM projects<\/a> using aspectj-maven-plugin.<br \/>\nBelow are the steps for various configuration we need to make.<\/p>\n<p><strong>Configuration at pom level<\/strong><\/p>\n<p>In plugin section add following lines of code<\/p>\n<p>[xml]<\/p>\n<p>&lt;plugin&gt;<br \/>\n    &lt;groupId&gt;org.codehaus.mojo&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;aspectj-maven-plugin&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;1.3&lt;\/version&gt;<br \/>\n    &lt;configuration&gt;<br \/>\n        &lt;complianceLevel&gt;1.7&lt;\/complianceLevel&gt;<br \/>\n    &lt;\/configuration&gt;<br \/>\n    &lt;executions&gt;<br \/>\n        &lt;execution&gt;<br \/>\n            &lt;goals&gt;<br \/>\n                &lt;goal&gt;compile&lt;\/goal&gt;<br \/>\n            &lt;\/goals&gt;<br \/>\n        &lt;\/execution&gt;<br \/>\n    &lt;\/executions&gt;<br \/>\n&lt;\/plugin&gt;<\/p>\n<p>[\/xml]<\/p>\n<p>In dependency section add<\/p>\n<p>[xml]<\/p>\n<p>&lt;dependencies&gt;<br \/>\n    &lt;dependency&gt;<br \/>\n\t\t\t&lt;groupId&gt;org.aspectj&lt;\/groupId&gt;<br \/>\n\t\t\t&lt;artifactId&gt;aspectjrt&lt;\/artifactId&gt;<br \/>\n\t\t\t&lt;version&gt;1.6.7&lt;\/version&gt;<br \/>\n\t\t\t&lt;scope&gt;compile&lt;\/scope&gt;<br \/>\n   &lt;\/dependency&gt;<br \/>\n    &lt;!&#8211; Day CQ5 WCM Dependencies &#8211;&gt;<br \/>\n    &lt;!&#8211; Apache Sling Dependencies &#8211;&gt;<br \/>\n    &lt;!&#8211; Apache Jackrabbit\/JCR Dependencies &#8211;&gt;<br \/>\n&lt;\/dependencies&gt;<\/p>\n<p>[\/xml]<\/p>\n<p>The Aspect class need to placed under folder src\/main\/aspect.<br \/>\n<em>sample project structure<\/em><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-24977\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/08\/Capture.png\" alt=\"Capture\" width=\"325\" height=\"220\" \/><\/p>\n<p>Below is the sample class which is used to log messages whenever the execution of method starts and end for all java classes methods that are inside the package com.ig<\/p>\n<p>[java]<br \/>\npackage com.ig.aspects;<\/p>\n<p>import 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.slf4j.Logger;<br \/>\nimport org.slf4j.LoggerFactory;<\/p>\n<p>@Aspect<br \/>\npublic class LoggerAspect {<\/p>\n<p>\t@Pointcut(value = &quot;execution(public * com.ig.core.*.*(..))&quot;)<br \/>\n\tpublic void anyMethod() {<br \/>\n\t}<\/p>\n<p>\t@Before(value = &quot;anyMethod()&quot;)<br \/>\n\tpublic void logEntry(JoinPoint joinPoint) {<br \/>\n\t\tLogger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass());<br \/>\n\t\tlogger.info(&quot;Method Started&#8230;.&quot;+joinPoint.getSignature().getName()+&quot;()&quot;);<br \/>\n\t}<\/p>\n<p>\t@After(value = &quot;anyMethod()&quot;)<br \/>\n\tpublic void logExit(JoinPoint joinPoint) {<br \/>\n\t\tLogger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass());<br \/>\n\t\tlogger.info(&quot; Method Exited&#8230;&quot;+joinPoint.getSignature().getName()+&quot;()&quot;);<br \/>\n\t}<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p><strong><em>execution(public * com.ig.core.*.*(..))<\/em><\/strong> : the execution of any method defined in the <strong>core<\/strong> package or a sub-package<br \/>\n<strong><em>@Before <\/em><\/strong>Before advice It will execute before the method execution<br \/>\n<strong><em>@After<\/em><\/strong> After returning advice tt will execute after the method is returned a result.<\/p>\n<p>Refer Spring\u2019s AOP documentation for Joint Point, Advice, Pointcuts, etc. and depending upon the use case in your application select the right one.<\/p>\n<p>References:<br \/>\nhttp:\/\/docs.spring.io\/spring\/docs\/2.5.5\/reference\/aop.html<br \/>\nhttp:\/\/docs.spring.io\/spring\/docs\/current\/spring-framework-reference\/html\/aop.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring AOP Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects.Some of cases where we can use it are to control some of common things like logging , adding some extra feature without actually touching the core logic. This blog post will explain how to enable aspects in our AEM projects [&hellip;]<\/p>\n","protected":false},"author":82,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":22},"categories":[1],"tags":[4847,2106,4841,2113],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24939"}],"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\/82"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=24939"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24939\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=24939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=24939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=24939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}