<?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>hemju &#187; Spring</title>
	<atom:link href="http://www.hemju.com/tags/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hemju.com</link>
	<description></description>
	<lastBuildDate>Wed, 08 Sep 2010 08:42:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using Spring/AOP to inject in ‘new’ objects with @Configurable</title>
		<link>http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/</link>
		<comments>http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 14:27:16 +0000</pubDate>
		<dc:creator>Helmut M. Juskewycz</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://www.hemju.com/?p=78</guid>
		<description><![CDATA[I am currently working on Web project using Hibernate/Spring for the persistence layer. I decided to use the not the old DTO/DAO. That means the data is encapsulated in POJO’s, the Data Transfer Objects (DTO’s), and Data Access Objects (DAO’s) are used to actually access the database. Typical methods from DAO’s are getXXXById, create, … [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I am currently working on Web project using Hibernate/Spring for the persistence layer. I decided to use the not the old DTO/DAO. That means the data is encapsulated in POJO’s, the Data Transfer Objects (DTO’s), and Data Access Objects (DAO’s) are used to actually access the database. Typical methods from DAO’s are getXXXById, create, … Ok, so far so good. The next thing I wanted to do, is to use the DAO’s in other objects, let’s call them Commands. Such a command gets some information, makes some checks, and does something. Some of them are checking if an entity already exisits, e.g. if an entity with the same username already exists. Not really a problem until now, but I also want to test the commands, therfore, I need some mocks. In this case I need DAO mocks. So the DAO’s have to be interchangeable while the Commands are created with “new”. Further, I wanted them injected before the constructor runs.</p>
<p>Sounds complicated, not a Problem with Spring!</p>
<p>First we have to annotate the Command with @Configurable:</p>
<pre lang="java">@Configurable(autowire = Autowire.BY_TYPE, preConstruction = true)

public class CreateUserCmd {

private UserDAO dao;

public CreateUserCmd (String username) {

if(dao.findByUsername(username)) {

throw new IllegalArgumentException("User already exists!");

}

}

// getter/setter for dao

}</pre>
<p>So what are we doing here. We are telling Spring that this Command Type (autowire) should be configured before the constructor (preConstruction). Configured with what? It wouldn’t be Spring without XML. Therefore, we are defining a prototype bean for our Command, so Spring knows what should be injected.</p>
<pre lang="xml"><bean class="CreateUserCmd" scope="prototype">
<property name="dao" ref="UserDAO"/>
</bean></pre>
<p>Spring knows where to inject, but not what. For our tests we said that we are creating a mock class. I will omit the class here, but we have to define a Spring bean for it:</p>
<pre lang="xml"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" >

<aop:spring-configured/>

<bean id="UserDAO" class="UserDAOMock" />

...

</beans></pre>
<p>Note, that the mock “id” and the “ref” are the same and we are enabling AOP. Now Spring knows everything it needs. Not really, because we have to configure how the injection is done. In our case we are using AspectJ for AOP which weaves the necessary code into our class. All needed jar files are coming with Spring. We need to include “spring-aspects.jar” and “aspectjweaver.jar”. The Spring configuration is complete, but AspectJ needs some more information. We are creating the XML aop.xml in the folder META-INF:</p>
<pre lang="xml"><aspectj>
<weaver options="-showWeaveInfo">
<!--  <weaver options="-verbose"/> -->
<include within="*" />
<!-- <exclude within="*..*CGLIB*" />  -->
</weaver>
</aspectj></pre>
<p>Viola, we are done! Now we only have to tell the compiler to use the weaver with “-javaagent:lib/aspectjweaver.jar”. Download the <a href="http://juskewycz.com/wp-content/uploads/SpringAOPSample.zip">Sample Project</a> (includes all libaries and code excamples).</p>
<p>More information can be found <a href="http://www.aspectprogrammer.org/blogs/adrian/2006/02/a_practical_gui_2.html" target="_blank">here</a> and <a href="http://static.springframework.org/spring/docs/2.0.x/reference/aop.html" target="_blank">here</a>.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-‘new’-objects-with-configurable/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/&amp;title=Using+Spring%2FAOP+to+inject+in+%E2%80%98new%E2%80%99+objects+with+%40Configurable" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/&amp;title=Using+Spring%2FAOP+to+inject+in+%E2%80%98new%E2%80%99+objects+with+%40Configurable" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/&amp;title=Using+Spring%2FAOP+to+inject+in+%E2%80%98new%E2%80%99+objects+with+%40Configurable&amp;desc=I%20am%20currently%20working%20on%20Web%20project%20using%20Hibernate%2FSpring%20for%20the%20persistence%20layer.%20I%20decided%20to%20use%20the%20not%20the%20old%20DTO%2FDAO.%20That%20means%20the%20data%20is%20encapsulated%20in%20POJO%E2%80%99s%2C%20the%20Data%20Transfer%20Objects%20%28DTO%E2%80%99s%29%2C%20and%20Data%20Access%20Objects%20%28DAO%E2%80%99s%29%20are%20used%20to%20actually%20access%20the%20database.%20Typical%20" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/&amp;bm_description=Using+Spring%2FAOP+to+inject+in+%E2%80%98new%E2%80%99+objects+with+%40Configurable&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/&amp;title=Using+Spring%2FAOP+to+inject+in+%E2%80%98new%E2%80%99+objects+with+%40Configurable" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/&amp;title=Using+Spring%2FAOP+to+inject+in+%E2%80%98new%E2%80%99+objects+with+%40Configurable" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/&amp;title=Using+Spring%2FAOP+to+inject+in+%E2%80%98new%E2%80%99+objects+with+%40Configurable" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Using+Spring%2FAOP+to+inject+in+%E2%80%98new%E2%80%99+objects+with+%40Configurable+-+http://b2l.me/3g88c&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>



<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.hemju.com/2008/04/23/using-springaop-to-inject-in-%e2%80%98new%e2%80%99-objects-with-configurable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
