<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PermGen error &#187; @annotation</title>
	<atom:link href="http://eggsylife.co.uk/tag/annotation/feed/" rel="self" type="application/rss+xml" />
	<link>http://eggsylife.co.uk</link>
	<description>Eggsylife</description>
	<lastBuildDate>Mon, 18 Apr 2011 19:40:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Spring annotation based AOP and intercepting the ball</title>
		<link>http://eggsylife.co.uk/2010/02/03/spring-annotation-based-aop-and-intercepting-the-ball/</link>
		<comments>http://eggsylife.co.uk/2010/02/03/spring-annotation-based-aop-and-intercepting-the-ball/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 10:13:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[@annotation]]></category>
		<category><![CDATA[@Around]]></category>
		<category><![CDATA[@Aspect]]></category>
		<category><![CDATA[AspectJ]]></category>
		<category><![CDATA[spring 3]]></category>
		<category><![CDATA[spring annotations]]></category>
		<category><![CDATA[spring aop]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=236</guid>
		<description><![CDATA[Spring AOP and Annotation based AspectJ The concept Recently I had to work on a requirement that asked that logs be kept when anyone performs certain actions upon the database. For example if some chose to remove an item from the database &#8211; the action, who performed it, when it was performed and any extra [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2010/02/03/spring-annotation-based-aop-and-intercepting-the-ball/" size="medium" count="true"></div></div><h2>Spring AOP and Annotation based AspectJ</h2>
<h3>The concept</h3>
<p>Recently I had to work on a requirement that asked that logs be kept when anyone performs certain actions upon the database.</p>
<p>For example if some chose to remove an item from the database &#8211; the action, who performed it, when it was performed and any extra information should be noted in the database.</p>
<p>Implementing this can be performed in a variety of ways. We could have simply told developers to call a function within any database methods to log the action. However Spring AOP provides a better way of doing this.</p>
<h3>The idea</h3>
<p>To make it easier to store the logs I wanted users to simply be able to annotate any methods they felt needed logging in the message.</p>
<p>Then using <a href="http://www.eclipse.org/aspectj/" target="_blank">AspectJ</a> we would have code that would intercept the methods and log the action in the database. All developers would need to do is very simply annotate their methods.</p>
<h3>The Annotation</h3>
<p>First of all I had to create the <a href="http://www.developer.com/article.php/3556176" target="_blank">Java Method annotation</a> that can be placed above the required methods.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.ElementType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Retention</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.RetentionPolicy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Target</span><span style="color: #339933;">;</span>
&nbsp;
@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
@Target<span style="color: #009900;">&#40;</span>ElementType.<span style="color: #006633;">METHOD</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> LogAction <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">String</span> actionPerformed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The annotation simply has one parameter that allows the developer to specify the action performed such as &#8216;object deleted&#8217; etc.</p>
<h3>The aspect class</h3>
<p>Next was to write the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj" target="_blank">Spring aspect class</a> which will perform the actual logging of the action</p>
<p>First of all we write a simple class and add the @Aspect annotation before the class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Aspect
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyAspect</pre></div></div>

<p>Next we need to define the Pointcut. As a Spring defintion a Pointcut is:</p>
<p>Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.</p>
<p>I&#8217;m writing a simple point cut that matches any public methods. This is coded like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;execution(public * *(..))&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> anyPublicMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The last part of the Aspect class is the Advice. Spring defines the advice as:</p>
<p>Advice: action taken by an aspect at a particular join point. Different types of advice include &#8220;around,&#8221; &#8220;before&#8221; and &#8220;after&#8221; advice. Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join point.</p>
<p>I need my advice to handle the &#8220;around&#8221; advice and only methods with my @LogAction annotation</p>
<p>To do this I used the following method:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anyPublicMethod() &amp;&amp; @annotation(logAction)&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> logAction<span style="color: #009900;">&#40;</span>ProceedingJoinPoint pjp, LogAction logAction<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The method uses my anyPublicMethod Pointcut and the @annotation(logAction) to match only public methods with the LogAction annotation specified.</p>
<p>I have also included the ProceedingJoinPoint parameter so I can access the various arguments in the annotated method.</p>
<p>The final Aspect class is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.ProceedingJoinPoint</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Around</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Aspect</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Pointcut</span><span style="color: #339933;">;</span>
&nbsp;
@Aspect
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyAspect<span style="color: #009900;">&#123;</span>
&nbsp;
	@Pointcut<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;execution(public * *(..))&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> anyPublicMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anyPublicMethod() &amp;&amp; @annotation(logAction)&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> logAction<span style="color: #009900;">&#40;</span>ProceedingJoinPoint pjp, LogAction logAction<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Do what you want with the actionperformed</span>
		<span style="color: #003399;">String</span> actionPerformed <span style="color: #339933;">=</span> logAction.<span style="color: #006633;">actionPerformed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Do what you want with the join point arguments</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Object</span> object <span style="color: #339933;">:</span> pjp.<span style="color: #006633;">getArgs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> pjp.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Updating the Spring app config</h3>
<p>To ensure that our @Aspect annotated class is picked up by Spring you simply need to add the following to your app-config.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:aspectj-autoproxy</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myAspect&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;package.to.my.MyAspect&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>This assumes that you have the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-aspectj-support" target="_blank">aop namespace</a> within your app-config beans definition like so</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:aop</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/aop&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</span>
<span style="color: #009900;">                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
.....
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>Setting up intercepted methods</h3>
<p>The final step is to mark the methods we want to be intercepted</p>
<p>This is as easy as doing the following</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@LogAction<span style="color: #009900;">&#40;</span>actionPerformed<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;doSomething&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span>MyObject someParameter<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now whenever that method is executed we will firstly execute our Aspect code.</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2010/02/03/spring-annotation-based-aop-and-intercepting-the-ball/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

