<?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>Dazed &#38; Confused &#187; spring</title>
	<atom:link href="http://blog.f12.no/wp/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.f12.no/wp</link>
	<description>Agile and Programming</description>
	<lastBuildDate>Tue, 02 Aug 2011 11:05:17 +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>Quicker JUnit Spring context tests in Maven</title>
		<link>http://blog.f12.no/wp/2008/12/12/quicker-junit-spring-maven/</link>
		<comments>http://blog.f12.no/wp/2008/12/12/quicker-junit-spring-maven/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 14:26:05 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.f12.no/wp/?p=870</guid>
		<description><![CDATA[This is probably fairly well known, but I couldn&#8217;t find any doc on it when I searched so I&#8217;ll put it up here.
First off: Don&#8217;t load your Spring Context in your tests unless you absolutely have to. Some integration tests should load it, but keep it to a minimum. Loading the context is expensive, especially [...]]]></description>
			<content:encoded><![CDATA[<p>This is probably fairly well known, but I couldn&#8217;t find any doc on it when I searched so I&#8217;ll put it up here.</p>
<p>First off: Don&#8217;t load your Spring Context in your tests unless you <em>absolutely have to</em>. Some integration tests should load it, but keep it to a minimum. Loading the context is expensive, especially if you load up Hibernate and maybe H2 in that context.</p>
<p>Avoid loading the context is important both for speed and design. I have seen way too many tests where the context is loaded just because someone don&#8217;t want to do mocking. Besides, if you have to do a lot of mocking your design is usually too coupled, and should be changed.</p>
<p>So how can you speed up your tests in Maven?</p>
<ul>
<li>Put them in a suite</li>
<li>And make sure they load the same context. Not the actual context object, but an array of strings of the context files in the same order and file names. See <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/test/AbstractSingleSpringContextTests.html#getConfigLocations()">http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/test/AbstractSingleSpringContextTests.html#getConfigLocations()</a>.</li>
</ul>
<p>Spring will <a href="http://static.springframework.org/spring/docs/2.5.x/reference/testing.html#testing-ctx-management">cache the context and make sure it is only loaded once</a>. But due to details I have not studied; in Maven, this only works for the tests that are in the same suite. It should work with fork=once on the surefire-plugin, but for some reason we need the suites too. Test which one works for you.</p>
<p>Drawbacks? If there is state in your context (database, stateful services) you will have created a dependency between your tests. That&#8217;s why, when it comes to database-testing you should use the <a title="class in org.springframework.test" href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/test/AbstractTransactionalSpringContextTests.html">AbstractTransactionalSpringContextTests</a> which will automatically roll back your changes at the end of your tests. If state is modified in a way that has nothing to do with your database use <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/test/AbstractSingleSpringContextTests.html#setDirty%28%29">setDirty()</a> to signal to Spring that the context should be recreated for the next test. Of course then you get the time penalty of recreation.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.f12.no/wp/2008/12/12/quicker-junit-spring-maven/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Clover saving time in development</title>
		<link>http://blog.f12.no/wp/2008/12/01/clover-time-development/</link>
		<comments>http://blog.f12.no/wp/2008/12/01/clover-time-development/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 09:26:25 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[clover]]></category>
		<category><![CDATA[cobertura]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://blog.f12.no/wp/?p=828</guid>
		<description><![CDATA[I read this post about Clover and using it to minimise the number of tests run. A nice idea, so I decided to have a go at it.
What it does is use the test-coverage that it was originally written to do, to figure out which tests exercise which classes. So when you change Class1 and [...]]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://blogs.atlassian.com/developer/2008/11/stop_testing_so_much.html">read this post about Clover and using it to minimise the number of tests run</a>. A nice idea, so I decided to have a go at it.</p>
<p>What it does is use the test-coverage that it was originally written to do, to figure out which tests exercise which classes. So when you change Class1 and Class2 it knows which that Test1, Test2, Test3 and Test4 need to be rerun to check if you have broken anything.</p>
<p>I did an initial test on our build which takes almost 10 minutes. I&#8217;m of the slightly paranoid type so I like to run the full build to verify my changes, at least before I check in. With changes in one class Clover figured it should rerun about 20 tests, and ran in about 4 minutes. That&#8217;s 6 minutes saved many times a day for each developer. We&#8217;re not using it at our build server just yet, but trying to save time for each developer before commit.</p>
<p>Any downsides? Of course. The initial time (mvn clean removes all info) to build is almost doubled for my project. You won&#8217;t catch all errors, and updates to dependencies won&#8217;t be caught. And there is a problem handling deleted classes. For some reason it creates a optimized-src directory which is a copy of all your sources and compiles from there. If you delete a class in your src folder it won&#8217;t be deleted in optimized-src and you could get compilation errors. After some initial tests it seems these problems are bigger in theory than they are in real day situations.</p>
<p>Using Clover should be no excuse bad tests though. I know all too well how I can really mess up my own tests. The long test times often stem from our inability to focus on testing the logic separately from infrastructure as databases or external services. And of course loading the Spring context is done way too much. But that&#8217;s stuff for another post.</p>
<p>But even if you have good unit tests there will be time to save. And everything that can keep me from getting distracted when developing is a good thing.</p>
<p>I&#8217;ll give this a good run until the 30 day trial licence expires, and maybe invest. Maybe we will see something similar in <a href="http://cobertura.sourceforge.net/">Cobertura</a> too&#8230;</p>
<p><strong>Update:</strong> It looks like it is also activated when doing mvn eclipse:eclipse . Because it redefines the source folders to target/clover/src-optimized this is also what is written in you Eclipse project. So be sure to have an easy way to disable running if you&#8217;re going to use this.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.f12.no/wp/2008/12/01/clover-time-development/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Shorter turnaround with JavaRebel</title>
		<link>http://blog.f12.no/wp/2008/08/26/shorter-turnaround-with-javarebel/</link>
		<comments>http://blog.f12.no/wp/2008/08/26/shorter-turnaround-with-javarebel/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 08:59:31 +0000</pubDate>
		<dc:creator>Øyvind</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://blog.f12.no/wp/?p=749</guid>
		<description><![CDATA[All of us have felt the pain waiting for the web-container to reload after just a minor change to the code. Well, recently i came across JavaRebel from ZeroTurnaround and they seem have solved our problem! And if you, as most of us, enjoy Spring you&#8217;ll also enjoy their Spring plug-in to monitor your context-files.
As [...]]]></description>
			<content:encoded><![CDATA[<p>All of us have felt the pain waiting for the web-container to reload after just a minor change to the code. Well, recently i came across <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel </a>from <a href="http://www.zeroturnaround.com/">ZeroTurnaround</a> and they seem have solved our problem! And if you, as most of us, enjoy Spring you&#8217;ll also enjoy their Spring plug-in to monitor your context-files.</p>
<p>As they put it:</p>
<blockquote><p>JavaRebel is a developer tool that will reload changes to compiled Java classes on-the-fly saving the time that it takes to redeploy an application or perform a container restart. It is a generic solution that works for Java EE and Java standalone applications.!</p></blockquote>
<p>Promise me to have a look at their screancasts (found on the right hand side of the page). They also have a nice list of features and supported JVM&#8217;s and web-containers.</p>
<p>They also have a <a href="http://groups.google.com/group/zeroturnaround-community">Google group</a> that answers questions you may have.</p>
<p>I haven&#8217;t tried it yet but am truely looking forward to it:-)<br />
And if you do, please post a comment and let me know how it went.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.f12.no/wp/2008/08/26/shorter-turnaround-with-javarebel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spring Security 2.0 released</title>
		<link>http://blog.f12.no/wp/2008/04/17/spring-security-20-released/</link>
		<comments>http://blog.f12.no/wp/2008/04/17/spring-security-20-released/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 09:22:35 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring-security]]></category>

		<guid isPermaLink="false">http://blog.f12.no/wp/?p=703</guid>
		<description><![CDATA[Yeah, it&#8217;s released. Especially the hierarchical groups is something that I have been missing from just about every security-framework.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.springframework.org/node/627" target="_blank">Yeah, it&#8217;s released</a>. Especially the hierarchical groups is something that I have been missing from just about every security-framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.f12.no/wp/2008/04/17/spring-security-20-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring config without XML</title>
		<link>http://blog.f12.no/wp/2008/03/26/spring-config-without-xml/</link>
		<comments>http://blog.f12.no/wp/2008/03/26/spring-config-without-xml/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 22:24:44 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[javaconfig]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.f12.no/wp/2008/03/26/spring-config-without-xml/</guid>
		<description><![CDATA[Spring has been around for quite a while now. I guess most projects still use XML for the config, but with the 2.5 release that will probably change. Alef Arendsen has a blogpost with links to a lot of resources about the new features, and upcoming ones.
Update: Spring JavaConfig M3 is released.
]]></description>
			<content:encoded><![CDATA[<p>Spring has been around for quite a while now. I guess most projects still use XML for the config, but with the 2.5 release that will probably change. Alef Arendsen has <a href="http://blog.springsource.com/main/2008/03/18/spring-dependency-injection-java-5-including-slides-and-code/">a blogpost with links to a lot of resources about the new features, and upcoming ones</a>.</p>
<p><strong>Update:</strong> <a href="http://blog.springsource.com/main/2008/03/26/spring-java-configuration-whats-new-in-m3/">Spring JavaConfig M3 is released</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.f12.no/wp/2008/03/26/spring-config-without-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Batch concepts</title>
		<link>http://blog.f12.no/wp/2008/03/13/spring-batch-concepts/</link>
		<comments>http://blog.f12.no/wp/2008/03/13/spring-batch-concepts/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 17:00:19 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[batch]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring-batch]]></category>

		<guid isPermaLink="false">http://blog.f12.no/wp/2008/03/13/spring-batch-concepts/</guid>
		<description><![CDATA[I&#8217;m mostly doing batch these days, so I&#8217;m keeping an eye out for what&#8217;s happening with Spring Batch. It does have a lot of concepts that resemble what our home grown framework is built around, so I can relate to a lot of it. The same reason will also hopefully make us able to migrate [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m mostly doing batch these days, so I&#8217;m keeping an eye out for what&#8217;s happening with Spring Batch. It does have a lot of concepts that resemble what our home grown framework is built around, so I can relate to a lot of it. The same reason will also hopefully make us able to migrate to it in a little while. Given the excellent track record of the Spring portfolio of frameworks, I have no doubt that migrating to it will reduce and improve our code.</p>
<p>To check out some comments about the further process and an <a href="http://blog.springsource.com/main/2008/02/04/spring-batch-recent-changes-and-upcoming-m4-release/">explanation of some of the core concepts, check out this blog entry</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.f12.no/wp/2008/03/13/spring-batch-concepts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploying your app in several environments</title>
		<link>http://blog.f12.no/wp/2007/10/31/deploying-your-app-in-several-environments/</link>
		<comments>http://blog.f12.no/wp/2007/10/31/deploying-your-app-in-several-environments/#comments</comments>
		<pubDate>Wed, 31 Oct 2007 08:44:20 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.f12.no/wp/2007/10/31/deploying-your-app-in-several-environments/</guid>
		<description><![CDATA[I have a strong aversion to custom build steps and generation of code. I do have to do it sometimes, but I don&#8217;t like it.   If I generate a WAR for my application I should not need to generate a new one for each environment I need to deploy it in. Joris Kuipers [...]]]></description>
			<content:encoded><![CDATA[<p>I have a strong aversion to custom build steps and generation of code. I do have to do it sometimes, but I don&#8217;t like it. <img src='http://blog.f12.no/wp/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  If I generate a WAR for my application I should not need to generate a new one for each environment I need to deploy it in. Joris Kuipers has a good presentation <a href="http://www.parleys.com/display/PARLEYS/Spring%20Beyond%20the%20Obvious">here</a> on how to achieve this in Spring. I have used some of the same mechanisms, but this takes it to a new level. Check link to the code in the comments beneath.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.f12.no/wp/2007/10/31/deploying-your-app-in-several-environments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More typing</title>
		<link>http://blog.f12.no/wp/2007/10/23/more-typing/</link>
		<comments>http://blog.f12.no/wp/2007/10/23/more-typing/#comments</comments>
		<pubDate>Tue, 23 Oct 2007 19:03:59 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[javaconfig]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[typesafety]]></category>

		<guid isPermaLink="false">http://blog.f12.no/wp/2007/10/23/more-typing/</guid>
		<description><![CDATA[Gavin King has read the post I pointed to in Extraordinarily typesafe and points out how new features in the Java world are giving us a lot of new possibilities in a type safe way.
I am slowly coming to my senses seeing how much benefit newer features like generics, static imports and annotations are bringing [...]]]></description>
			<content:encoded><![CDATA[<p>Gavin King <a href="http://in.relation.to/Bloggers/SafetyInTypes">has read</a> the post I pointed to in <a href="http://blog.f12.no/wp/2007/10/18/extraordinarily-typesafe/">Extraordinarily typesafe</a> and points out how new features in the Java world are giving us a lot of new possibilities in a type safe way.</p>
<p>I am slowly coming to my senses seeing how much benefit newer features like generics, static imports and annotations are bringing to the game. I used to think that these features were nice to have, but didn&#8217;t revolutionize the way we develop our applications. After seeing how Jmock2, Guice, Quaré and others use new features to change the way we code I am beginning to see the light.</p>
<p>I didn&#8217;t use to think the Spring xml files were that bad either, but now I really need to look into the Spring Java config API.</p>
<p>Who knows, I might even look into Ruby one of these days. (I would probably miss my type saftety though) <img src='http://blog.f12.no/wp/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.f12.no/wp/2007/10/23/more-typing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Integration, development and stubs</title>
		<link>http://blog.f12.no/wp/2007/02/04/integration-development-and-stubs/</link>
		<comments>http://blog.f12.no/wp/2007/02/04/integration-development-and-stubs/#comments</comments>
		<pubDate>Sun, 04 Feb 2007 12:13:15 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[stubbing]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.f12.no/wp/2007/02/04/integration-development-and-stubs/</guid>
		<description><![CDATA[In my current project we have a fair bit of integration. Loose coupling and all that is excellent, but it does create some challenges:

Integrating systems are only available inside the firewall. Sometimes it is relevant to test and develop outside our normal network.
Different integrating systems have different availability. You can&#8217;t rely on all of them [...]]]></description>
			<content:encoded><![CDATA[<p>In my current project we have a fair bit of integration. Loose coupling and all that is excellent, but it does create some challenges:</p>
<ul>
<li>Integrating systems are only available inside the firewall. Sometimes it is relevant to test and develop outside our normal network.</li>
<li>Different integrating systems have different availability. You can&#8217;t rely on all of them beeing available all the time.</li>
</ul>
<p>To overcome these we want to be able to stub the integrating systems. But introducing stubs also leads to some complications:</p>
<ul>
<li>The test team needs to be able to check which systems are stubbed and which systems have real integration when testing.</li>
<li>The test team will need different configurations at different times, and I&#8217;m too lazy to package 20 installations. <img src='http://blog.f12.no/wp/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
</ul>
<p>So what I need is:</p>
<ul>
<li>A webinterface to display the status of the integration points (stub or real implementation)</li>
<li>A webinterface to change which points are stubbed or not</li>
</ul>
<h3>The solution</h3>
<p>Since we are using Spring all the wiring and configuration is stored in XML files, and I could probably manipulate files on the disk, but it didn&#8217;t sound like an attractive solution. After asking a bit around at the <a href="http://www.java.no">java.no</a> forum I settled on a proxy based solution in the spirit of Spring&#8217;s <a href="http://www.springframework.org/docs/api/org/springframework/ejb/access/LocalStatelessSessionProxyFactoryBean.html">LocalStatelessSessionProxyFactoryBean</a>.  Implementation was easy enough, and it seems to work. In it&#8217;s essence it is a proxy and a proxyfactory wrapped in one class (FactoryBean and MethodInterceptor is implemented) that handles which class will be invoked (stub/real).</p>
<p>It also has some shortcomings that you should be aware of:</p>
<ul>
<li>No security as to who can use it</li>
<li>It is not tested with proxies inside the proxy, but I guess that will work.</li>
<li>You should probably restart your app before making config changes. Any state in the beans real/stub will be missing when switching.</li>
</ul>
<h3>The config</h3>
<p>The config should be pretty self explanatory with two beans, one interface and one boolean injected into the ProxyFactory. The stubEnabled boolean could be omitted. It defaults to running the real implementation.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><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;myService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;IntegrationPointProxyFactoryBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;businessInterface&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MyService<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;stubImplementation&quot;</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;">class</span>=<span style="color: #ff0000;">&quot;MyServiceStubImpl&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;realImplementation&quot;</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;">class</span>=<span style="color: #ff0000;">&quot;MyServiceImpl&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;stubEnabled&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3>The proxy</h3>
<p>The proxy is the object that sits around the real and stub implementations. It will direct the execution to the correct bean depending on it&#8217;s settings.</p>
<p>The signature looks like this (the larger methods impls is further down):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IntegrationPointProxyFactoryBean <span style="color: #000000; font-weight: bold;">implements</span> FactoryBean, MethodInterceptor, InitializingBean <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">/* Required interface methods. See Spring docs for info. */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> getObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">proxy</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isSingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> getObjectType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">proxy</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">businessInterface</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">proxy</span>.<span style="color: #006633;">getClass</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>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> invoke<span style="color: #009900;">&#40;</span>MethodInvocation invocation<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">SecurityException</span>, <span style="color: #003399;">NoSuchMethodException</span>, <span style="color: #003399;">IllegalArgumentException</span>, <span style="color: #003399;">IllegalAccessException</span>, <span style="color: #003399;">InvocationTargetException</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterPropertiesSet<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;">/* New methods specific for this bean */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBusinessInterface<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> businessInterface<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setRealImplementation<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> realImplementation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStubImplementation<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> stubImplementation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStubEnabled<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> stubEnabled<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The <code>set*</code> methods are just plain old dependency injection methods that sets a class variable, I won&#8217;t write about them. To make the solution better you might want to do some before/after consistency checks in them. The interesting stuff happens in the <code>afterPropertiesSet</code> and <code>invoke</code> methods.</p>
<p><code>afterPropertiesSet</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterPropertiesSet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">/* A bunch of consistency checks for null etc */</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Initialise the proxyobject which is this object</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">proxy</span> <span style="color: #339933;">=</span> ProxyFactory.<span style="color: #006633;">getProxy</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">businessInterface</span>, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Thats pretty much all you need to do to create a proxy with Springs utilities. The object you are assigning as the proxy must implement the <code>MethodInterceptor</code> interface.</p>
<p>That was the config, the rest of the stuff happens runtime in the invoke method.</p>
<p><code>invoke</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> invoke<span style="color: #009900;">&#40;</span>MethodInvocation invocation<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">SecurityException</span>, <span style="color: #003399;">NoSuchMethodException</span>, <span style="color: #003399;">IllegalArgumentException</span>, <span style="color: #003399;">IllegalAccessException</span>, <span style="color: #003399;">InvocationTargetException</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">Method</span> aopMethod <span style="color: #339933;">=</span> invocation.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">Object</span> runObject <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">stubEnabled</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		runObject <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">stubImplementation</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		runObject <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">realImplementation</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003399;">Method</span> classInvocation <span style="color: #339933;">=</span> runObject.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span>aopMethod.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, aopMethod.<span style="color: #006633;">getParameterTypes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">return</span> classInvocation.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>runObject, invocation.<span style="color: #006633;">getArguments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>The controller</h3>
<p>This is all nice and dandy, but it won&#8217;t do you much good if you don&#8217;t have something to control and display this behaviour. You need a controller doing the following:</p>
<ul>
<li>Retrieve all beans of type <code>IntegrationProxtFactoryBean</code> and pass them to the JSP</li>
<li>If enabled, switch implementation between real and stub</li>
</ul>
<p><code>IntegrationPointController</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IntegrationPointController <span style="color: #000000; font-weight: bold;">extends</span> AbstractController <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> ModelAndView handleRequestInternal<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Map</span> beans <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getApplicationContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBeansOfType</span><span style="color: #009900;">&#40;</span>IntegrationPointProxyFactoryBean.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Switch implementasjon</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;point&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">String</span> pointName <span style="color: #339933;">=</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;point&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			IntegrationPointProxyFactoryBean pointBean <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IntegrationPointProxyFactoryBean<span style="color: #009900;">&#41;</span> beans.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>pointName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			pointBean.<span style="color: #006633;">setStubEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>pointBean.<span style="color: #006633;">isStubEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003399;">Map</span> model <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		model.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;points&quot;</span>, beans<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config/showPoints&quot;</span>, model<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And the JSP is something like this (not the prettiest code, but this is just util stuff anyway):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="jsp" style="font-family:monospace;">&lt;c :forEach items=&quot;${points}&quot; var=&quot;point&quot;&gt;
  &lt;div style=&quot;border: 1px solid black; padding: 1em; margin: 1em&quot;&gt;
    &lt;strong&gt;Name:&lt;/strong&gt;
    &lt;c :out value=&quot;${point.key}&quot; /&gt;
    &lt;br /&gt;
    &lt;strong&gt;Real:&lt;/strong&gt;
    &lt;c :if test=&quot;!${point.value.stubEnabled}&quot;&gt;
      &lt;span style=&quot;color: red&quot;&gt;
        &lt;c :out value=&quot;${point.value.realImplementationInjected}&quot; /&gt;
        &lt;c :if test=&quot;${!point.value.stubEnabled}&quot;&gt;&lt;/c&gt;
        &lt;br /&gt;
        &lt;strong&gt;Stub:&lt;/strong&gt;
        &lt;c :if test=&quot;${point.value.stubEnabled}&quot;&gt;
          &lt;span style=&quot;color: green&quot;&gt;
            &lt;c :out value=&quot;${point.value.stubImplementationInjected}&quot; /&gt;
            &lt;c :if test=&quot;${point.value.stubEnabled}&quot;&gt;&lt;/c&gt;
            &lt;br /&gt;
            &lt;c :if test=&quot;${point.value.switchable}&quot;&gt;[ 
            &lt;a href=&quot;&amp;lt;c:url value=&quot;&gt;
            &lt;c :param name=&quot;point&quot;
            value=&quot;${point.key}&quot; /&gt;&quot;&amp;gt;Switch&lt;/a&gt;]&lt;/c&gt;
          &lt;/span&gt;
        &lt;/c&gt;
      &lt;/span&gt;
    &lt;/c&gt;
  &lt;/div&gt;
&lt;/c&gt;</pre></td></tr></table></div>

<p>This is the first time I try to do such an extensive article, I hope it turned out alright. Let me know if you think otherwise. <img src='http://blog.f12.no/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.f12.no/wp/2007/02/04/integration-development-and-stubs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

