<?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>Jayway Team Blog &#187; ddd</title>
	<atom:link href="http://blog.jayway.com/tag/ddd/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 28 Jan 2012 15:53:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Spring and load-time weaving of Neo4j-based domain objects</title>
		<link>http://blog.jayway.com/2009/05/26/spring-and-load-time-weaving-of-neo4j-based-domain-objects/</link>
		<comments>http://blog.jayway.com/2009/05/26/spring-and-load-time-weaving-of-neo4j-based-domain-objects/#comments</comments>
		<pubDate>Tue, 26 May 2009 07:51:25 +0000</pubDate>
		<dc:creator>Mattias Ask</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[ddd]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[neo4j]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=1773</guid>
		<description><![CDATA[What do you do when your Spring configuration isn't in charge of creating your objects that needs to be injected with stuff? This became a real problem for me when I tried doing some non-anemic domain object implementations persisted as Neo4j Nodes. I was playing around with creating a Twitter clone, in my opinion the [...]]]></description>
			<content:encoded><![CDATA[<p>What do you do when your Spring configuration isn't in charge of creating your objects that needs to be injected with stuff? This became a real problem for me when I tried doing some non-anemic domain object implementations persisted as <a href="http://www.neo4j.org">Neo4j</a> Nodes. </p>
<p>I was playing around with creating a Twitter clone, in my opinion the "Hello World" of graph databases, and I started out with the following simple service.</p>
<p>Spring configurating:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;beans</span> ... <span style="font-weight: bold; color: black;">&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;context:annotation-config</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;neo&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.neo4j.api.core.EmbeddedNeo&quot;</span>
		<span style="color: #000066;">destroy-method</span>=<span style="color: #ff0000;">&quot;shutdown&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor-arg<span style="font-weight: bold; color: black;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>var/neo<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor-arg<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;indexService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.neo4j.util.index.NeoIndexService&quot;</span>
		<span style="color: #000066;">destroy-method</span>=<span style="color: #ff0000;">&quot;shutdown&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;neo&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;twitterService&quot;</span>
		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.jayway.twitter.neo.NeoBasedTweetService&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;neo&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;neo&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;indexService&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;indexService&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Transactions --&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;tx:annotation-driven</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
        ...
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/beans<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>The service:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NeoBasedTweetService <span style="color: #000000; font-weight: bold;">implements</span> TweetService <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> NeoService neo;
	<span style="color: #000000; font-weight: bold;">protected</span> IndexService indexService;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Setters for neo and indexService</span>
&nbsp;
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> Tweet createTweet<span style="color: #66cc66;">&#40;</span>User user, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> tweet<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		UserNode userNode = <span style="color: #000000; font-weight: bold;">new</span> UserNode<span style="color: #66cc66;">&#40;</span>indexService.<span style="color: #006600;">getSingleNode</span><span style="color: #66cc66;">&#40;</span>
				TwitterIndexConstants.<span style="color: #006600;">INDEX_USER</span>, user.<span style="color: #006600;">getUserName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		TweetNode tweetNode = <span style="color: #000000; font-weight: bold;">new</span> TweetNode<span style="color: #66cc66;">&#40;</span>neo.<span style="color: #006600;">createNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		tweetNode.<span style="color: #006600;">setKvitt</span><span style="color: #66cc66;">&#40;</span>tweet<span style="color: #66cc66;">&#41;</span>;
		userNode.<span style="color: #006600;">createRelationshipTo</span><span style="color: #66cc66;">&#40;</span>tweetNode, KvittRelationships.<span style="color: #006600;">KVITTRAT</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">return</span> tweetNode;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Services to create User and more</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>This doesn't feel very DDD. I would really want to do the User to be responsible for creating the Tweet. Like this:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NeoBasedTweetService <span style="color: #000000; font-weight: bold;">implements</span> TweetService <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> NeoService neo;
	<span style="color: #000000; font-weight: bold;">protected</span> IndexService indexService;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Setters for neo and indexService</span>
&nbsp;
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> Tweet createTweet<span style="color: #66cc66;">&#40;</span>User user, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> tweet<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		UserNode userNode = <span style="color: #000000; font-weight: bold;">new</span> UserNode<span style="color: #66cc66;">&#40;</span>indexService.<span style="color: #006600;">getSingleNode</span><span style="color: #66cc66;">&#40;</span>
				TwitterIndexConstants.<span style="color: #006600;">INDEX_USER</span>, user.<span style="color: #006600;">getUserName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">return</span> userNode.<span style="color: #006600;">createTweet</span><span style="color: #66cc66;">&#40;</span>tweet<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Services to create User and more</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The TweetNode and UserNode would look like this:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TweetNode <span style="color: #000000; font-weight: bold;">extends</span> NodeDelegate <span style="color: #000000; font-weight: bold;">implements</span> Tweet <span style="color: #66cc66;">&#123;</span>
&nbsp;
	TweetNode<span style="color: #66cc66;">&#40;</span>Node underlyingNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>underlyingNode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Methods for doing Tweet-stuff</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserNode <span style="color: #000000; font-weight: bold;">extends</span> NodeDelegate <span style="color: #000000; font-weight: bold;">implements</span> User<span style="color: #66cc66;">&#123;</span>
&nbsp;
	UserNode<span style="color: #66cc66;">&#40;</span>Node underlyingNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>underlyingNode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Methods for doing User-stuff</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tweet createTweet<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> kvitt<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Here comes a problem! I need a new Node</span>
		<span style="color: #808080; font-style: italic;">//from the NeoService to create the TweetNode</span>
		TweetNode tweetNode = <span style="color: #000000; font-weight: bold;">new</span> TweetNode<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
		tweetNode.<span style="color: #006600;">setTweet</span><span style="color: #66cc66;">&#40;</span>tweet<span style="color: #66cc66;">&#41;</span>;
		createRelationshipTo<span style="color: #66cc66;">&#40;</span>tweetNode, TwitterRelationships.<span style="color: #006600;">TWEETED</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">return</span> tweetNode;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>NodeDelegate, extended by both User- and TweetNode implements the Neo4j interface <code>Node</code> and delegates all the method calls to the underlying node ( for more on this, see my <a href="http://blog.jayway.com/2008/10/06/neo4j-matches-my-mental-model-of-information/">previous post</a>) :</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NodeDelegate <span style="color: #000000; font-weight: bold;">implements</span> Node <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Node delegate;
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> NodeDelegate<span style="color: #66cc66;">&#40;</span>Node underlyingNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		delegate = underlyingNode;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Methods for delegating all Node methods to the Node delegate</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>As you see I have a problem here. In the first version my NeoService is injected into NeoBasedTweetService and used when I create the new TweetNode. In the re-made, more DDD-style example I create the new TweetNode in the UserNode, where I don't have access to my NeoService. How do we solve this?</p>
<p>One alternative that I instinctively dislike is to inject the NeoService into the UserNode. The reason for me disliking this is that my UserNode IS a Node, and it feels very tangled if a Node holds a reference to its container. But I came to think of another solution thanks to a talk I did a while back together with Henrik Reinhold on what was new in Spring 2.5. </p>
<p>In Spring 2.5 load-time weaving (LTW) capabillites was introduced, and with LTW you can manipulate objects in load-time, as opposed to runtime weaving that is proxy based. This, combine it with @Configurable and @Autowired, proves to be <em>very</em> helpful when the Spring context is not in charge of creating the objects in question, as in the example above...</p>
<p>To enable LTW we add one tag in the Spring configuration:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;context:load-time-weaver</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;</pre>
<p>After that we have to inform the Spring container of how we want thing done. To do we edit the TweetNode and NodeDelegate like this:</p>
<pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">//Annotate the class with @Configurable so that Spring can find it.</span>
@Configurable
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TweetNode <span style="color: #000000; font-weight: bold;">extends</span> NodeDelegate <span style="color: #000000; font-weight: bold;">implements</span> Tweet <span style="color: #66cc66;">&#123;</span>
&nbsp;
	TweetNode<span style="color: #66cc66;">&#40;</span>Node underlyingNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>underlyingNode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Add a default constructor both in the TweetNode</span>
	<span style="color: #808080; font-style: italic;">//and the NodeDelegate, it is new:able without a Node.</span>
	<span style="color: #000000; font-weight: bold;">public</span> TweetNode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Add a setter for the underlaying Node and annotate it with @Autowired.</span>
	@Autowired
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setNode<span style="color: #66cc66;">&#40;</span>Node nodeDelegate<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">setNode</span><span style="color: #66cc66;">&#40;</span>nodeDelegate<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Tweet-methods as before</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NodeDelegate <span style="color: #000000; font-weight: bold;">implements</span> Node <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Node delegate;
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> NodeDelegate<span style="color: #66cc66;">&#40;</span>Node underlyingNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		delegate = underlyingNode;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> NodeDelegate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> setNode<span style="color: #66cc66;">&#40;</span>Node nodeDelegate<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>delegate == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			delegate = nodeDelegate;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Methods for delegating all Node methods to the Node delegate</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Now Spring only has to be able to create Nodes. To do that we create a simple NodeFactory, like this:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NodeFactory <span style="color: #000000; font-weight: bold;">implements</span> FactoryBean<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> NeoService neo = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setNeo<span style="color: #66cc66;">&#40;</span>NeoService neo<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">neo</span> = neo;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> getObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> neo.<span style="color: #006600;">createNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> getObjectType<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Node.<span style="color: #000000; font-weight: bold;">class</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> isSingleton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>... and add it to the configuration, like this:</p>
<pre class="xml">&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;node&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.jayway.neo.utils.NodeFactory&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;neo&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;neo&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>The result of this is that Spring now has a NodeFactory that returns a new node for every request, I have annotated the TweetNode so that Spring knows of it (@Configurable) and what to do with it (@Autowired on setNode) and I've enabled load-time weaving in order to get the a new Node woven into a TweetNode every time a new instance of it is created in the code.</p>
<p>In UserNode I really don't have to make any changes. "setNode(Node)" will get a new Node after the instantiation of a TweetNode, even if the constructor that takes a Node is called. For clarity I still changed the UserNode as follows:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserNode <span style="color: #000000; font-weight: bold;">extends</span> NodeDelegate <span style="color: #000000; font-weight: bold;">implements</span> User<span style="color: #66cc66;">&#123;</span>
&nbsp;
	UserNode<span style="color: #66cc66;">&#40;</span>Node underlyingNode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>underlyingNode<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//Methods for doing User-stuff</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Tweet createTweet<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> kvitt<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//This TweetNode will get a Node set by Spring</span>
		TweetNode tweetNode = <span style="color: #000000; font-weight: bold;">new</span> TweetNode<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		tweetNode.<span style="color: #006600;">setTweet</span><span style="color: #66cc66;">&#40;</span>tweet<span style="color: #66cc66;">&#41;</span>;
		createRelationshipTo<span style="color: #66cc66;">&#40;</span>tweetNode, TwitterRelationships.<span style="color: #006600;">TWEETED</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">return</span> tweetNode;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The problem I had when trying to make the UserNode responsible for creating its TweetNode's was that I needed to inject something that could create Nodes into the UserNodes. By using load-time weaving and instructing Spring of how to wire a TweetNode I don't have to pass my NeoService around to all the UserNodes created in my code. Instead I can rely on Spring to inject what is needed, when it's needed.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/05/26/spring-and-load-time-weaving-of-neo4j-based-domain-objects/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

