{"id":517,"date":"2012-01-01T20:40:19","date_gmt":"2012-01-01T19:40:19","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=517"},"modified":"2012-01-01T20:40:19","modified_gmt":"2012-01-01T19:40:19","slug":"advanced-aspectj-part-iv-declaring-aspects-precedence","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=517","title":{"rendered":"Advanced AspectJ part IV : declaring aspects precedence"},"content":{"rendered":"<p>Today we discuss about aspects precedence e.g. the order in which several aspects are processed at the same join point.<\/p>\n<p>If you&#8217;re a heavy user of AspectJ you can sometimes end up with having more than one aspects for the same join point. In such case it could be interesting to know which aspect kicks in first and which one kicks in last. This order is called <em>aspect precedence<\/em>.<\/p>\n<p><!--more--><\/p>\n<p>Without any specfic indication, the aspect ordering is totally unpredictable. It is not necessarily a problem if the aspects are totally unrelated.<\/p>\n<h1>I Aspects ordering<\/h1>\n<p>To declare aspect precedence, you can use the special keyword<\/p>\n<blockquote><p><strong>declare precedence : &lt;Type1Pattern&gt;,&lt;Type2Pattern&gt;&#8230;&lt;TypeNPattern&gt;;<\/strong><\/p><\/blockquote>\n<p>In the above example, <strong>aspect1<\/strong>has precedence over <strong>aspect2<\/strong>, which in turn has precedence over <strong>aspect3<\/strong> etc. until <strong>aspectN<\/strong>.<\/p>\n<p>By &#8220;<em>has precedence over<\/em>&#8220;, we mean &#8220;<em>is executed before<\/em>&#8220;. Generally the aspect with highest precedence executes first. The execution order also depends on the <em>type of advice<\/em>.<\/p>\n<ul>\n<li>the aspect with highest precedence kicks in <strong>before<\/strong> the lowest precedence aspect for <strong>around()\/before()<\/strong> advice<\/li>\n<li>the aspect with highest precedence kicks in <strong>after<\/strong> the lowest precedence aspect for <strong>around()\/after()<\/strong> advice<\/li>\n<li>the aspect with highest precedence wraps <strong>around<\/strong> the lowest precedence aspect for <strong>around()<\/strong> advice<\/li>\n<\/ul>\n<p>Exemple:<\/p>\n<pre class=\"brush: java; title: ; wrap-lines: false; notranslate\" title=\"\">\ndeclare precedence : around1, around2, before3, before4, after5;\n<\/pre>\n<p><a href=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2013\/05\/aspectj_precedence1.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1656\" alt=\"AspectJ_Precedence\" src=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2013\/05\/aspectj_precedence1.png\" width=\"232\" height=\"215\" \/><\/a><\/p>\n<p>One best practice in AspectJ is to declare a special aspect whose task consists simply of declaring precedence for other aspects.<\/p>\n<pre class=\"brush: java; title: ; wrap-lines: false; notranslate\" title=\"\">\npublic aspect MyAspectsOrdering\n{\n\tdeclare precedence : around1, around2, before3, before4, after5;\n\tdeclare precedence : Transactional*, around4;\n\tdeclare precedence : *, before2;\n}\n<\/pre>\n<p>It is possible to have many aspect precedence declarations in the same aspect but you should be very carefull not to introduce <strong>circular dependencies<\/strong>. It is perfectly possible to create a circular cycle of aspect precedence with several declarations and there will be no warning message from AspectJ.<\/p>\n<p>Please notice that in the precedence declaration, you can also use wildcards *.<\/p>\n<h1>II Advices ordering<\/h1>\n<p>It is extremely rare though perfectly possible to have several advices applying to the same join point in one aspect:<\/p>\n<pre class=\"brush: java; highlight: [3]; title: ; wrap-lines: false; notranslate\" title=\"\">\npublic aspect MyAspect\n{\n\tpointcut myPointCut() : execution(* com.myApp..*(..));\n\n\tObject around() : myPointCut()\n\t{\n\t\t\/\/ Around advice for myPointCut\n\t}\n\n\tbefore() : myPointCut()\n\t{\n\t\t\/\/ Before advice for myPointCut\n\t}\n}\n<\/pre>\n<p>In such cases, it is the lexical order e.g. the order in which the advices are declared in source code which applies.<\/p>\n<p><a href=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/01\/aspectj_advice_precedence.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-531\" title=\"AspectJ_Advice_Precedence\" alt=\"\" src=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/01\/aspectj_advice_precedence.png\" width=\"198\" height=\"113\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we discuss about aspects precedence e.g. the order in which several aspects are processed at the same join point. If you&#8217;re a heavy user of AspectJ you can sometimes end up with having more than one aspects for the&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/www.doanduyhai.com\/blog\/?p=517\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,11],"tags":[33,44],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/517"}],"collection":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=517"}],"version-history":[{"count":0,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/517\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}