{"id":43,"date":"2011-08-08T00:18:04","date_gmt":"2011-08-07T22:18:04","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=43"},"modified":"2011-08-08T00:18:04","modified_gmt":"2011-08-07T22:18:04","slug":"spring-aop-advices-on-setters-not-trigged","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=43","title":{"rendered":"Spring AOP advices on setters not trigged"},"content":{"rendered":"<p>A few days ago I had to enhance some text value injected into one of my bean<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n\n&amp;lt;bean id=&amp;quot;myBean&amp;quot;&amp;gt;\n&amp;lt;property name=&amp;quot;featureName&amp;quot; value=&amp;quot;${global.properties.feature.name}&amp;quot; \/&amp;gt;\n&amp;lt;\/bean&amp;gt;\n<\/pre>\n<p>The idea was to modify the injected &#8220;feature&#8221; value with an @Around AOP poincut:<br \/>\n<!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\n@Around(&amp;quot;execution(* com.test.MyBean.setFeature(..))&amp;quot;)\npublic Object enhanceFeatureName(ProceedingJoinPoint jp) throws Throwable\n{\n\tObject[] args = jp.getArgs();\n\targs[0] = ...\/\/ new value of feature name here\n\treturn jp.proceed(args);\n}\n<\/pre>\n<p>To my great despair, this wasn&#8217;t working at all. In debug mode, the <em>enhanceFeatureName()<\/em> method was never called. I&#8217;ve tried everything, changing the poincut expression, turning the<em> @Around<\/em> advice into a<em> @Before<\/em> advice, using XML declarative AOP\u00a0 instead of annotation, nothing seemed to work.<\/p>\n<blockquote><p>With a <em>@Before<\/em> advice, you can read the value of all input arguments but you cannot change them. The returned <strong>Object[] args<\/strong> array is a COPY of the original arguments array. The only way to modify input parameters is using <em>@Around<\/em> advice and calling <em>proceedingJointPoint.proceed(<strong>modifiedArgs<\/strong>)<\/em>.<\/p><\/blockquote>\n<p>Indeed, there is a big flaw in this design. Basically, an AOP proxy is created for the bean only AFTER the bean complete initialization, e.g. after all properties are injected by setter. So when the <em>setFeature()<\/em> is called <strong>for the first time<\/strong>, the AOP proxy did not exist yet. No wonder the <em>enhanceFeatureName()<\/em> method was never called.<\/p>\n<p>However all subsequent calles to the setter, after the proxy has been set-up, will be intercepted properly.<\/p>\n<p>The only solution for this problem is to rely on AspectJ with <strong>compile-time weaving<\/strong>, bypassing the Spring initialization process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few days ago I had to enhance some text value injected into one of my bean The idea was to modify the injected &#8220;feature&#8221; value with an @Around AOP poincut:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,14],"tags":[32,47],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/43"}],"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=43"}],"version-history":[{"count":0,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}