<?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>cosminaru blog &#187; Java</title>
	<atom:link href="http://cosminaru.ro/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://cosminaru.ro/blog</link>
	<description>home</description>
	<lastBuildDate>Fri, 26 Aug 2011 09:56:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Stripes presentation at TJUG</title>
		<link>http://cosminaru.ro/blog/2009/03/05/stripes-presentation-at-tjug/</link>
		<comments>http://cosminaru.ro/blog/2009/03/05/stripes-presentation-at-tjug/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 15:54:45 +0000</pubDate>
		<dc:creator>Cosmin Marginean</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Stripes]]></category>
		<category><![CDATA[TJUG]]></category>

		<guid isPermaLink="false">http://cosminaru.ro/blog/?p=140</guid>
		<description><![CDATA[Last week at TJUG, our local Java User Group, I had a short presentation called &#8220;Stripes &#8211; Painless Java Web Development&#8221;, introducing (obviously) Stripes. Slides can be downloaded here. We also had a chance to take a look at Eclipse RAP in another presentation by Gabriel Ciuloaica &#8211; &#8220;From Eclipse RCP to RAP&#8221;. Many thanks [...]]]></description>
			<content:encoded><![CDATA[<p>Last week at <a href="https://transylvaniajug.dev.java.net/">TJUG</a>, our local Java User Group, I had a short presentation called <em>&#8220;Stripes &#8211; Painless Java Web Development&#8221;</em>, introducing (obviously) <a href="http://www.stripesframework.org/">Stripes</a>. Slides can be downloaded <a href="http://cosminaru.ro/blog/downloads/Stripes-TJUG.pdf">here</a>.</p>
<p>We also had a chance to take a look at <a href="http://www.eclipse.org/rap/">Eclipse RAP</a> in another presentation by Gabriel Ciuloaica &#8211; <em>&#8220;From Eclipse RCP to RAP&#8221;</em>.</p>
<p>Many thanks to Gabi Pop who always takes care of planning and organizing the TJUG meetings.</p>
]]></content:encoded>
			<wfw:commentRss>http://cosminaru.ro/blog/2009/03/05/stripes-presentation-at-tjug/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring by examples &#8211; RMI</title>
		<link>http://cosminaru.ro/blog/2006/11/02/spring-by-examples-rmi/</link>
		<comments>http://cosminaru.ro/blog/2006/11/02/spring-by-examples-rmi/#comments</comments>
		<pubDate>Thu, 02 Nov 2006 08:29:00 +0000</pubDate>
		<dc:creator>Cosmin Marginean</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.cosminaru.ro/blog/?p=6</guid>
		<description><![CDATA[RMI is just one of the strategies that you can choose from when you want to implement remoting in your Spring application. At the moment, Spring provides support for Hessian, Burlap, RMI, Web services and an own HTTP remoting solution. All of these remoting strategies were designed so they can share a common configuration style [...]]]></description>
			<content:encoded><![CDATA[<p>RMI is just one of the strategies that you can choose from when you want to implement remoting in your Spring application. At the moment, Spring provides support for <a href="http://www.caucho.com/hessian">Hessian</a>, <a href="http://www.caucho.com/resin-3.0/protocols/burlap.xtp">Burlap</a>, RMI, Web services and an own HTTP remoting solution. All of these remoting strategies were designed so they can share a common configuration style allowing you to manage the exporting and accessing method from outside your business code, in the well known non-intrusive Spring manner. This means that Spring offers a POJO-based programming model for both your server and client, no matter which remoting solution you choose. The reference documentation regarding the remoting features of Spring is available <a href="http://www.springframework.org/docs/reference/remoting.html">here</a>.</p>
<p>For now we will take a look at how Spring handles RMI while peeking into the code of a small demo application. Our example contains a simple delivery service exposed through a <code>DeliveryServiceIntf</code> interface that has two methods for handling <code>Delivery</code> objects. Below is some of the code for these objects.
<pre class="prettyprint"><code>&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0032;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0032;&#0105;&#0109;&#0112;&#0108;&#0101;&#0109;&#0101;&#0110;&#0116;&#0115;&#0032;&#0083;&#0101;&#0114;&#0105;&#0097;&#0108;&#0105;&#0122;&#0097;&#0098;&#0108;&#0101;&#0032;&#0123;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0112;&#0114;&#0105;&#0118;&#0097;&#0116;&#0101;&#0032;&#0083;&#0116;&#0114;&#0105;&#0110;&#0103;&#0032;&#0114;&#0101;&#0113;&#0117;&#0101;&#0115;&#0116;&#0101;&#0114;&#0078;&#0097;&#0109;&#0101;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0112;&#0114;&#0105;&#0118;&#0097;&#0116;&#0101;&#0032;&#0068;&#0097;&#0116;&#0101;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0068;&#0097;&#0116;&#0101;&#0059;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0040;&#0083;&#0116;&#0114;&#0105;&#0110;&#0103;&#0032;&#0114;&#0101;&#0113;&#0117;&#0101;&#0115;&#0116;&#0101;&#0114;&#0078;&#0097;&#0109;&#0101;&#0044;&#0032;&#0068;&#0097;&#0116;&#0101;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0068;&#0097;&#0116;&#0101;&#0041;&#0032;&#0123;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0116;&#0104;&#0105;&#0115;&#0046;&#0114;&#0101;&#0113;&#0117;&#0101;&#0115;&#0116;&#0101;&#0114;&#0078;&#0097;&#0109;&#0101;&#0032;&#0061;&#0032;&#0114;&#0101;&#0113;&#0117;&#0101;&#0115;&#0116;&#0101;&#0114;&#0078;&#0097;&#0109;&#0101;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0116;&#0104;&#0105;&#0115;&#0046;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0068;&#0097;&#0116;&#0101;&#0032;&#0061;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0068;&#0097;&#0116;&#0101;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0125;&#0010;&#0032;&#0032;&#0032;&#0032;&#0046;&#0046;&#0046;&#0010;&#0125;&#0010;&#0010;&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0105;&#0110;&#0116;&#0101;&#0114;&#0102;&#0097;&#0099;&#0101;&#0032;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0110;&#0116;&#0102;&#0032;&#0123;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0118;&#0111;&#0105;&#0100;&#0032;&#0110;&#0101;&#0119;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0040;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0041;&#0059;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0076;&#0105;&#0115;&#0116;&#0032;&#0103;&#0101;&#0116;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0105;&#0101;&#0115;&#0040;&#0041;&#0059;&#0010;&#0010;&#0125;</code></pre>
<p>As you can see, the service interface does not need to extend the <code>java.rmi.Remote</code> in order to be used as an RMI service. Also, it&#8217;s remotely accessible methods don&#8217;t need to throw <code>java.rmi.RemoteException</code>. This is a Spring enhancement that applies to all it&#8217;s remoting solutions, not just to RMI. But this is no reason to get scared, because Spring will manage the invocation failure in it&#8217;s own fashion by throwing a <code><a href="http://www.springframework.org/docs/api/org/springframework/remoting/RemoteAccessException.html">org.springframework.remoting.RemoteAccessException</a></code>. This is an unchecked exception so the client can decide if it wants to catch and handle this exception or just ignore it when it&#8217;s occurrence it&#8217;s considered fatal. This is however Spring&#8217;s recommended approach, but you can also expose traditional RMI implementations with Spring. Actually, this is an important issue that you should consider and analyze when remoting with Spring. While both traditional and Spring-managed RMI clients can access a traditional RMI server implementation exported through Spring, only a Spring client will be able to access a Spring exported RMI service that uses a POJO-based model.<br />Getting back to our example, we have below the implementation of our service which I tried to keep as simple as possible:
<pre class="prettyprint"><code>&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0032;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0109;&#0112;&#0108;&#0032;&#0105;&#0109;&#0112;&#0108;&#0101;&#0109;&#0101;&#0110;&#0116;&#0115;&#0032;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0110;&#0116;&#0102;&#0032;&#0123;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0112;&#0114;&#0105;&#0118;&#0097;&#0116;&#0101;&#0032;&#0076;&#0105;&#0115;&#0116;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0105;&#0101;&#0115;&#0032;&#0061;&#0032;&#0110;&#0101;&#0119;&#0032;&#0065;&#0114;&#0114;&#0097;&#0121;&#0076;&#0105;&#0115;&#0116;&#0040;&#0041;&#0059;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0118;&#0111;&#0105;&#0100;&#0032;&#0110;&#0101;&#0119;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0040;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0041;&#0032;&#0123;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0105;&#0101;&#0115;&#0046;&#0097;&#0100;&#0100;&#0040;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0125;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0076;&#0105;&#0115;&#0116;&#0032;&#0103;&#0101;&#0116;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0105;&#0101;&#0115;&#0040;&#0041;&#0032;&#0123;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0114;&#0101;&#0116;&#0117;&#0114;&#0110;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0105;&#0101;&#0115;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0125;&#0010;&#0125;</code></pre>
<p>After we have everything implemented on the server side, we need to configure Spring to export the service through RMI. The following Spring configuration snippet does just that.
<pre class="prettyprint"><code>&#0032;&#0032;&#0032;&#0032;&#0060;&#0098;&#0101;&#0097;&#0110;&#0032;&#0105;&#0100;&#0061;&#0034;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0061;&#0034;&#0099;&#0111;&#0109;&#0046;&#0116;&#0101;&#0115;&#0116;&#0046;&#0114;&#0109;&#0105;&#0046;&#0115;&#0101;&#0114;&#0118;&#0101;&#0114;&#0046;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0109;&#0112;&#0108;&#0034;&#0062;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0010;&#0032;&#0032;&#0032;&#0032;&#0060;&#0047;&#0098;&#0101;&#0097;&#0110;&#0062;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0060;&#0098;&#0101;&#0097;&#0110;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0061;&#0034;&#0111;&#0114;&#0103;&#0046;&#0115;&#0112;&#0114;&#0105;&#0110;&#0103;&#0102;&#0114;&#0097;&#0109;&#0101;&#0119;&#0111;&#0114;&#0107;&#0046;&#0114;&#0101;&#0109;&#0111;&#0116;&#0105;&#0110;&#0103;&#0046;&#0114;&#0109;&#0105;&#0046;&#0082;&#0109;&#0105;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0069;&#0120;&#0112;&#0111;&#0114;&#0116;&#0101;&#0114;&#0034;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0032;&#0110;&#0097;&#0109;&#0101;&#0061;&#0034;&#0115;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0078;&#0097;&#0109;&#0101;&#0034;&#0032;&#0118;&#0097;&#0108;&#0117;&#0101;&#0061;&#0034;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0045;&#0115;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0047;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0032;&#0110;&#0097;&#0109;&#0101;&#0061;&#0034;&#0115;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0032;&#0114;&#0101;&#0102;&#0061;&#0034;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0047;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0032;&#0110;&#0097;&#0109;&#0101;&#0061;&#0034;&#0115;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0110;&#0116;&#0101;&#0114;&#0102;&#0097;&#0099;&#0101;&#0034;&#0032;&#0118;&#0097;&#0108;&#0117;&#0101;&#0061;&#0034;&#0099;&#0111;&#0109;&#0046;&#0116;&#0101;&#0115;&#0116;&#0046;&#0114;&#0109;&#0105;&#0046;&#0099;&#0111;&#0109;&#0109;&#0111;&#0110;&#0046;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0110;&#0116;&#0102;&#0034;&#0047;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0032;&#0110;&#0097;&#0109;&#0101;&#0061;&#0034;&#0114;&#0101;&#0103;&#0105;&#0115;&#0116;&#0114;&#0121;&#0080;&#0111;&#0114;&#0116;&#0034;&#0032;&#0118;&#0097;&#0108;&#0117;&#0101;&#0061;&#0034;&#0049;&#0050;&#0051;&#0052;&#0034;&#0047;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0060;&#0047;&#0098;&#0101;&#0097;&#0110;&#0062;</code></pre>
<p>This is the point where we can choose which one of Spring&#8217;s remoting solutions we want to adopt. In our case we will use <code><a href="http://www.springframework.org/docs/api/org/springframework/remoting/rmi/RmiServiceExporter.html">org.springframework.remoting.rmi.RmiServiceExporter</a></code> which will also manage the RMI registry for us. The <code>serviceName</code> and <code>registryPort</code> properties are RMI specific settings that will constitute the information the client side needs to access our service. The URL for the service in this case will look something like <code>rmi://hostname:1234/delivery-service</code>. The <code>serviceInterface</code> property indicates the POJO whose methods will be the subject of RMI invoking and the <code>service</code> property references a bean implementing the business logic of the service.<br />At this point, having all up-and-running on the server side requires a small amount of effort and it only consists of classic Spring context initialization.
<pre class="prettyprint"><code>&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0032;&#0082;&#0109;&#0105;&#0083;&#0101;&#0114;&#0118;&#0101;&#0114;&#0084;&#0101;&#0115;&#0116;&#0032;&#0123;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0115;&#0116;&#0097;&#0116;&#0105;&#0099;&#0032;&#0118;&#0111;&#0105;&#0100;&#0032;&#0109;&#0097;&#0105;&#0110;&#0040;&#0083;&#0116;&#0114;&#0105;&#0110;&#0103;&#0091;&#0093;&#0032;&#0097;&#0114;&#0103;&#0115;&#0041;&#0032;&#0123;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0065;&#0112;&#0112;&#0108;&#0105;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0067;&#0111;&#0110;&#0116;&#0101;&#0120;&#0116;&#0032;&#0099;&#0116;&#0120;&#0032;&#0061;&#0032;&#0110;&#0101;&#0119;&#0032;&#0067;&#0108;&#0097;&#0115;&#0115;&#0080;&#0097;&#0116;&#0104;&#0088;&#0109;&#0108;&#0065;&#0112;&#0112;&#0108;&#0105;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0067;&#0111;&#0110;&#0116;&#0101;&#0120;&#0116;&#0040;&#0034;&#0099;&#0111;&#0109;&#0047;&#0116;&#0101;&#0115;&#0116;&#0047;&#0114;&#0109;&#0105;&#0047;&#0115;&#0101;&#0114;&#0118;&#0101;&#0114;&#0047;&#0114;&#0109;&#0105;&#0045;&#0115;&#0101;&#0114;&#0118;&#0101;&#0114;&#0045;&#0099;&#0111;&#0110;&#0116;&#0101;&#0120;&#0116;&#0046;&#0120;&#0109;&#0108;&#0034;&#0041;&#0059;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0010;&#0032;&#0032;&#0032;&#0032;&#0125;&#0010;&#0125;</code></pre>
<p>Now let&#8217;s see how does it work on the client side and how should we proceed to access the remote service. Here is actually a little bit easier because everything relies on the Spring bean configuration file, as seen below, while the rest of the code is pretty straightforward.
<pre class="prettyprint"><code>&#0032;&#0032;&#0032;&#0032;&#0060;&#0098;&#0101;&#0097;&#0110;&#0032;&#0105;&#0100;&#0061;&#0034;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0061;&#0034;&#0111;&#0114;&#0103;&#0046;&#0115;&#0112;&#0114;&#0105;&#0110;&#0103;&#0102;&#0114;&#0097;&#0109;&#0101;&#0119;&#0111;&#0114;&#0107;&#0046;&#0114;&#0101;&#0109;&#0111;&#0116;&#0105;&#0110;&#0103;&#0046;&#0114;&#0109;&#0105;&#0046;&#0082;&#0109;&#0105;&#0080;&#0114;&#0111;&#0120;&#0121;&#0070;&#0097;&#0099;&#0116;&#0111;&#0114;&#0121;&#0066;&#0101;&#0097;&#0110;&#0034;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0032;&#0110;&#0097;&#0109;&#0101;&#0061;&#0034;&#0115;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0085;&#0114;&#0108;&#0034;&#0032;&#0118;&#0097;&#0108;&#0117;&#0101;&#0061;&#0034;&#0114;&#0109;&#0105;&#0058;&#0047;&#0047;&#0108;&#0111;&#0099;&#0097;&#0108;&#0104;&#0111;&#0115;&#0116;&#0058;&#0049;&#0050;&#0051;&#0052;&#0047;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0045;&#0115;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0047;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0032;&#0110;&#0097;&#0109;&#0101;&#0061;&#0034;&#0115;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0110;&#0116;&#0101;&#0114;&#0102;&#0097;&#0099;&#0101;&#0034;&#0032;&#0118;&#0097;&#0108;&#0117;&#0101;&#0061;&#0034;&#0099;&#0111;&#0109;&#0046;&#0116;&#0101;&#0115;&#0116;&#0046;&#0114;&#0109;&#0105;&#0046;&#0099;&#0111;&#0109;&#0109;&#0111;&#0110;&#0046;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0110;&#0116;&#0102;&#0034;&#0047;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0060;&#0047;&#0098;&#0101;&#0097;&#0110;&#0062;</code></pre>
<p>As you can see, we just need to define the <code>deliveryService</code> bean of class <code><a href="http://www.springframework.org/docs/api/org/springframework/remoting/rmi/RmiProxyFactoryBean.html">org.springframework.remoting.rmi.RmiProxyFactoryBean</a></code>. This will indicate Spring that the <code>deliveryService</code> bean will actually be a proxy accessing the remote service at the URL <code>rmi://localhost:1234/delivery-service</code>, and that the business logic of the service will be used by the client through the methods of the <code>DeliveryServiceIntf</code> interface.<br />Accessing this bean will create a proxy that we can cast to the <code>DeliveryServiceIntf</code> interface and that will dispatch all the calls on this interface to the RMI service. Below is the pretty self-explanatory client-side code.
<pre class="prettyprint"><code>&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0032;&#0082;&#0109;&#0105;&#0067;&#0108;&#0105;&#0101;&#0110;&#0116;&#0084;&#0101;&#0115;&#0116;&#0032;&#0123;&#0010;&#0010;&#0032;&#0032;&#0032;&#0032;&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0115;&#0116;&#0097;&#0116;&#0105;&#0099;&#0032;&#0118;&#0111;&#0105;&#0100;&#0032;&#0109;&#0097;&#0105;&#0110;&#0040;&#0083;&#0116;&#0114;&#0105;&#0110;&#0103;&#0091;&#0093;&#0032;&#0097;&#0114;&#0103;&#0115;&#0041;&#0032;&#0123;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0065;&#0112;&#0112;&#0108;&#0105;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0067;&#0111;&#0110;&#0116;&#0101;&#0120;&#0116;&#0032;&#0099;&#0116;&#0120;&#0032;&#0061;&#0032;&#0110;&#0101;&#0119;&#0032;&#0067;&#0108;&#0097;&#0115;&#0115;&#0080;&#0097;&#0116;&#0104;&#0088;&#0109;&#0108;&#0065;&#0112;&#0112;&#0108;&#0105;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0067;&#0111;&#0110;&#0116;&#0101;&#0120;&#0116;&#0040;&#0034;&#0099;&#0111;&#0109;&#0047;&#0116;&#0101;&#0115;&#0116;&#0047;&#0114;&#0109;&#0105;&#0047;&#0099;&#0108;&#0105;&#0101;&#0110;&#0116;&#0047;&#0114;&#0109;&#0105;&#0045;&#0099;&#0108;&#0105;&#0101;&#0110;&#0116;&#0045;&#0099;&#0111;&#0110;&#0116;&#0101;&#0120;&#0116;&#0046;&#0120;&#0109;&#0108;&#0034;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0110;&#0116;&#0102;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0032;&#0061;&#0032;&#0040;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0073;&#0110;&#0116;&#0102;&#0041;&#0032;&#0099;&#0116;&#0120;&#0046;&#0103;&#0101;&#0116;&#0066;&#0101;&#0097;&#0110;&#0040;&#0034;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0100;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0046;&#0110;&#0101;&#0119;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0040;&#0110;&#0101;&#0119;&#0032;&#0068;&#0101;&#0108;&#0105;&#0118;&#0101;&#0114;&#0121;&#0040;&#0034;&#0074;&#0111;&#0104;&#0110;&#0032;&#0068;&#0111;&#0101;&#0034;&#0044;&#0032;&#0110;&#0101;&#0119;&#0032;&#0068;&#0097;&#0116;&#0101;&#0040;&#0041;&#0041;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0046;&#0046;&#0046;&#0010;&#0032;&#0032;&#0032;&#0032;&#0125;&#0010;&#0125;</code></pre>
<p>Therefore, the client will not be aware of the fact that the service is running remote and even less about the fact that its method calls are marshaled through RMI. The Spring bean configuration file takes care of these details, so the client code will not be affected if we change the remoting strategy or even if we choose to run the service in-process.</p>
<p>The code for this demo can be found <a href="http://cosminaru.ro/downloads/code-samples/spring/rmi.zip">here</a>. The first step is to run <code>ant run-server</code> in order to launch the RMI server and afterwards you need to run <code>ant run-client</code> from another console to start up the RMI client.<br />Hope this post can give you a basic feeling about Spring&#8217;s RMI support. Please feel free to comment on any aspect of this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://cosminaru.ro/blog/2006/11/02/spring-by-examples-rmi/feed/</wfw:commentRss>
		<slash:comments>374</slash:comments>
		</item>
		<item>
		<title>Spring by examples &#8211; method interceptors</title>
		<link>http://cosminaru.ro/blog/2006/10/16/spring-by-examples-method-interceptors/</link>
		<comments>http://cosminaru.ro/blog/2006/10/16/spring-by-examples-method-interceptors/#comments</comments>
		<pubDate>Mon, 16 Oct 2006 16:16:00 +0000</pubDate>
		<dc:creator>Cosmin Marginean</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.cosminaru.ro/blog/?p=5</guid>
		<description><![CDATA[Spring is a great Java technology that has become a very popular application framework during the past few years. My intention is not to go through the whole concepts and architectural details of the framework, because that kind of information can be easily looked up starting at http://www.springframework.org. As the article title indicates, I intend [...]]]></description>
			<content:encoded><![CDATA[<p>Spring is a great Java technology that has become a very popular application framework during the past few years. My intention is not to go through the whole concepts and architectural details of the framework, because that kind of information can be easily looked up starting at <a href="http://www.springframework.org">http://www.springframework.org</a>. As the article title indicates, I intend to provide hands-on examples showing the minimal requirements to bundle certain Spring functionalities in your Java applications. So, because I will not go into the &#8220;what&#8217;s under the hood&#8221; approach unless absolutely necessary, most of the examples might require the knowledge of basic Spring concepts. Anyway, the basic idea is that you must RTFM before deciding if Spring is right for your application.</p>
<p>The first example is a short look at a simple method intercepting strategy. You can read all about this and the whole Spring AOP API <a href="http://static.springframework.org/spring/docs/2.0.x/reference/aop-api.html">here</a>.The source code for this example can be found <a href="http://cosminaru.ro/downloads/code-samples/spring/method-interceptors.zip">here</a>. In the project directory run <code>ant compile run</code> to launch the application.</p>
<p>For the beginning let&#8217;s consider that we have the service <code>MyService</code> that that has a method <code>doSomething()</code> performing an operation which takes a long time to execute. Below you can see the (pretty dumb) code of this method.
<pre class="prettyprint"><code>&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0032;&#0077;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0032;&#0123;&#0010;&#0032;&#0032;&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0118;&#0111;&#0105;&#0100;&#0032;&#0100;&#0111;&#0083;&#0111;&#0109;&#0101;&#0116;&#0104;&#0105;&#0110;&#0103;&#0040;&#0041;&#0032;&#0123;&#0010;&#0032;&#0032;&#0032;&#0032;&#0102;&#0111;&#0114;&#0032;&#0040;&#0105;&#0110;&#0116;&#0032;&#0105;&#0032;&#0061;&#0032;&#0049;&#0059;&#0032;&#0105;&#0032;&#0060;&#0032;&#0049;&#0048;&#0048;&#0048;&#0048;&#0059;&#0032;&#0105;&#0043;&#0043;&#0041;&#0032;&#0123;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0083;&#0121;&#0115;&#0116;&#0101;&#0109;&#0046;&#0111;&#0117;&#0116;&#0046;&#0112;&#0114;&#0105;&#0110;&#0116;&#0108;&#0110;&#0040;&#0034;&#0105;&#0061;&#0034;&#0032;&#0043;&#0032;&#0105;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0125;&#0010;&#0032;&#0032;&#0125;&#0010;&#0125;</code></pre>
<p>In order to print out the performance statistics on the method call, we must first implement the interceptor that actually calculates the execution time for this method. To do this we need to implement the <code>org.aopalliance.intercept.MethodInterceptor</code> interface shipped with Spring. This is actually a callback providing access to the actual call of the methods of our service. The JavaDoc for this interface is <a href="http://aopalliance.sourceforge.net/doc/org/aopalliance/intercept/MethodInterceptor.html">here</a>.
<pre class="prettyprint"><code>&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0032;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0077;&#0101;&#0116;&#0104;&#0111;&#0100;&#0073;&#0110;&#0116;&#0101;&#0114;&#0099;&#0101;&#0112;&#0116;&#0111;&#0114;&#0032;&#0105;&#0109;&#0112;&#0108;&#0101;&#0109;&#0101;&#0110;&#0116;&#0115;&#0032;&#0077;&#0101;&#0116;&#0104;&#0111;&#0100;&#0073;&#0110;&#0116;&#0101;&#0114;&#0099;&#0101;&#0112;&#0116;&#0111;&#0114;&#0032;&#0123;&#0010;&#0032;&#0032;&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0079;&#0098;&#0106;&#0101;&#0099;&#0116;&#0032;&#0105;&#0110;&#0118;&#0111;&#0107;&#0101;&#0040;&#0077;&#0101;&#0116;&#0104;&#0111;&#0100;&#0073;&#0110;&#0118;&#0111;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0032;&#0109;&#0101;&#0116;&#0104;&#0111;&#0100;&#0073;&#0110;&#0118;&#0111;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0041;&#0032;&#0116;&#0104;&#0114;&#0111;&#0119;&#0115;&#0032;&#0084;&#0104;&#0114;&#0111;&#0119;&#0097;&#0098;&#0108;&#0101;&#0032;&#0123;&#0010;&#0032;&#0032;&#0032;&#0032;&#0108;&#0111;&#0110;&#0103;&#0032;&#0115;&#0116;&#0097;&#0114;&#0116;&#0084;&#0105;&#0109;&#0101;&#0032;&#0061;&#0032;&#0083;&#0121;&#0115;&#0116;&#0101;&#0109;&#0046;&#0099;&#0117;&#0114;&#0114;&#0101;&#0110;&#0116;&#0084;&#0105;&#0109;&#0101;&#0077;&#0105;&#0108;&#0108;&#0105;&#0115;&#0040;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0079;&#0098;&#0106;&#0101;&#0099;&#0116;&#0032;&#0114;&#0101;&#0115;&#0117;&#0108;&#0116;&#0032;&#0061;&#0032;&#0109;&#0101;&#0116;&#0104;&#0111;&#0100;&#0073;&#0110;&#0118;&#0111;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0046;&#0112;&#0114;&#0111;&#0099;&#0101;&#0101;&#0100;&#0040;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0108;&#0111;&#0110;&#0103;&#0032;&#0100;&#0117;&#0114;&#0097;&#0116;&#0105;&#0111;&#0110;&#0032;&#0061;&#0032;&#0083;&#0121;&#0115;&#0116;&#0101;&#0109;&#0046;&#0099;&#0117;&#0114;&#0114;&#0101;&#0110;&#0116;&#0084;&#0105;&#0109;&#0101;&#0077;&#0105;&#0108;&#0108;&#0105;&#0115;&#0040;&#0041;&#0032;&#0045;&#0032;&#0115;&#0116;&#0097;&#0114;&#0116;&#0084;&#0105;&#0109;&#0101;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0077;&#0101;&#0116;&#0104;&#0111;&#0100;&#0032;&#0109;&#0101;&#0116;&#0104;&#0111;&#0100;&#0032;&#0061;&#0032;&#0109;&#0101;&#0116;&#0104;&#0111;&#0100;&#0073;&#0110;&#0118;&#0111;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0046;&#0103;&#0101;&#0116;&#0077;&#0101;&#0116;&#0104;&#0111;&#0100;&#0040;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0083;&#0116;&#0114;&#0105;&#0110;&#0103;&#0032;&#0109;&#0101;&#0116;&#0104;&#0111;&#0100;&#0078;&#0097;&#0109;&#0101;&#0032;&#0061;&#0032;&#0109;&#0101;&#0116;&#0104;&#0111;&#0100;&#0046;&#0103;&#0101;&#0116;&#0068;&#0101;&#0099;&#0108;&#0097;&#0114;&#0105;&#0110;&#0103;&#0067;&#0108;&#0097;&#0115;&#0115;&#0040;&#0041;&#0046;&#0103;&#0101;&#0116;&#0078;&#0097;&#0109;&#0101;&#0040;&#0041;&#0032;&#0043;&#0032;&#0034;&#0046;&#0034;&#0032;&#0043;&#0032;&#0109;&#0101;&#0116;&#0104;&#0111;&#0100;&#0046;&#0103;&#0101;&#0116;&#0078;&#0097;&#0109;&#0101;&#0040;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0083;&#0121;&#0115;&#0116;&#0101;&#0109;&#0046;&#0111;&#0117;&#0116;&#0046;&#0112;&#0114;&#0105;&#0110;&#0116;&#0108;&#0110;&#0040;&#0034;&#0077;&#0101;&#0116;&#0104;&#0111;&#0100;&#0032;&#0039;&#0034;&#0032;&#0043;&#0032;&#0109;&#0101;&#0116;&#0104;&#0111;&#0100;&#0078;&#0097;&#0109;&#0101;&#0032;&#0043;&#0032;&#0034;&#0039;&#0032;&#0116;&#0111;&#0111;&#0107;&#0032;&#0034;&#0032;&#0043;&#0032;&#0100;&#0117;&#0114;&#0097;&#0116;&#0105;&#0111;&#0110;&#0032;&#0043;&#0032;&#0034;&#0032;&#0109;&#0105;&#0108;&#0108;&#0105;&#0115;&#0101;&#0099;&#0111;&#0110;&#0100;&#0115;&#0032;&#0116;&#0111;&#0032;&#0114;&#0117;&#0110;&#0034;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0114;&#0101;&#0116;&#0117;&#0114;&#0110;&#0032;&#0110;&#0117;&#0108;&#0108;&#0059;&#0010;&#0032;&#0032;&#0125;&#0010;&#0125;&#0010;</code></pre>
<p>Next we need to proxy our service in order to obtain an instance whose methods are being intercepted by our <code>ServiceMethodInterceptor</code>. To achieve this, all it takes is a little magic in Spring&#8217;s bean configuration file, as you can see below.
<pre class="prettyprint"><code>&#0060;&#0098;&#0101;&#0097;&#0110;&#0115;&#0062;&#0010;&#0032;&#0032;&#0060;&#0098;&#0101;&#0097;&#0110;&#0032;&#0105;&#0100;&#0061;&#0034;&#0109;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0061;&#0034;&#0099;&#0111;&#0109;&#0046;&#0116;&#0101;&#0115;&#0116;&#0046;&#0077;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0062;&#0010;&#0032;&#0032;&#0060;&#0047;&#0098;&#0101;&#0097;&#0110;&#0062;&#0010;&#0010;&#0032;&#0032;&#0060;&#0098;&#0101;&#0097;&#0110;&#0032;&#0105;&#0100;&#0061;&#0034;&#0105;&#0110;&#0116;&#0101;&#0114;&#0099;&#0101;&#0112;&#0116;&#0111;&#0114;&#0034;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0061;&#0034;&#0099;&#0111;&#0109;&#0046;&#0116;&#0101;&#0115;&#0116;&#0046;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0077;&#0101;&#0116;&#0104;&#0111;&#0100;&#0073;&#0110;&#0116;&#0101;&#0114;&#0099;&#0101;&#0112;&#0116;&#0111;&#0114;&#0034;&#0062;&#0010;&#0032;&#0032;&#0060;&#0047;&#0098;&#0101;&#0097;&#0110;&#0062;&#0010;&#0010;&#0032;&#0032;&#0060;&#0098;&#0101;&#0097;&#0110;&#0032;&#0105;&#0100;&#0061;&#0034;&#0105;&#0110;&#0116;&#0101;&#0114;&#0099;&#0101;&#0112;&#0116;&#0101;&#0100;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0061;&#0034;&#0111;&#0114;&#0103;&#0046;&#0115;&#0112;&#0114;&#0105;&#0110;&#0103;&#0102;&#0114;&#0097;&#0109;&#0101;&#0119;&#0111;&#0114;&#0107;&#0046;&#0097;&#0111;&#0112;&#0046;&#0102;&#0114;&#0097;&#0109;&#0101;&#0119;&#0111;&#0114;&#0107;&#0046;&#0080;&#0114;&#0111;&#0120;&#0121;&#0070;&#0097;&#0099;&#0116;&#0111;&#0114;&#0121;&#0066;&#0101;&#0097;&#0110;&#0034;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0060;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0032;&#0110;&#0097;&#0109;&#0101;&#0061;&#0034;&#0116;&#0097;&#0114;&#0103;&#0101;&#0116;&#0034;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0114;&#0101;&#0102;&#0032;&#0098;&#0101;&#0097;&#0110;&#0061;&#0034;&#0109;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0047;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0060;&#0047;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0060;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0032;&#0110;&#0097;&#0109;&#0101;&#0061;&#0034;&#0105;&#0110;&#0116;&#0101;&#0114;&#0099;&#0101;&#0112;&#0116;&#0111;&#0114;&#0078;&#0097;&#0109;&#0101;&#0115;&#0034;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0108;&#0105;&#0115;&#0116;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0118;&#0097;&#0108;&#0117;&#0101;&#0062;&#0105;&#0110;&#0116;&#0101;&#0114;&#0099;&#0101;&#0112;&#0116;&#0111;&#0114;&#0060;&#0047;&#0118;&#0097;&#0108;&#0117;&#0101;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0032;&#0032;&#0060;&#0047;&#0108;&#0105;&#0115;&#0116;&#0062;&#0010;&#0032;&#0032;&#0032;&#0032;&#0060;&#0047;&#0112;&#0114;&#0111;&#0112;&#0101;&#0114;&#0116;&#0121;&#0062;&#0010;&#0032;&#0032;&#0060;&#0047;&#0098;&#0101;&#0097;&#0110;&#0062;&#0010;&#0060;&#0047;&#0098;&#0101;&#0097;&#0110;&#0115;&#0062;</code></pre>
<p>The key in this XML snippet is Spring&#8217;s built-in class <a href="http://www.springframework.org/docs/api/org/springframework/aop/framework/ProxyFactoryBean.html"><code>org.springframework.aop.framework.ProxyFactoryBean</code></a> which provides the actual proxying of our service. In order to obtain the desired effect we must set the <code>target</code> and <code>interceptorNames</code> properties for this bean. The <code>target</code> property represents the name of the bean that we want to proxy, which in our case is the <code>myService</code> bean. The <code>interceptorNames</code> property holds a list of bean names that will be used as interceptors for the proxied bean. So, yes, you can define more than one interceptor for your bean.<br />As everything seems to be packed pretty nice, all we need to do now is to have our service instantiated using Spring and call itâs <code>doSomething</code> method.
<pre class="prettyprint"><code>&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0099;&#0108;&#0097;&#0115;&#0115;&#0032;&#0084;&#0101;&#0115;&#0116;&#0032;&#0123;&#0010;&#0032;&#0032;&#0112;&#0117;&#0098;&#0108;&#0105;&#0099;&#0032;&#0115;&#0116;&#0097;&#0116;&#0105;&#0099;&#0032;&#0118;&#0111;&#0105;&#0100;&#0032;&#0109;&#0097;&#0105;&#0110;&#0040;&#0083;&#0116;&#0114;&#0105;&#0110;&#0103;&#0091;&#0093;&#0032;&#0097;&#0114;&#0103;&#0115;&#0041;&#0032;&#0123;&#0010;&#0032;&#0032;&#0032;&#0032;&#0065;&#0112;&#0112;&#0108;&#0105;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0067;&#0111;&#0110;&#0116;&#0101;&#0120;&#0116;&#0032;&#0099;&#0116;&#0120;&#0032;&#0061;&#0032;&#0110;&#0101;&#0119;&#0032;&#0067;&#0108;&#0097;&#0115;&#0115;&#0080;&#0097;&#0116;&#0104;&#0088;&#0109;&#0108;&#0065;&#0112;&#0112;&#0108;&#0105;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0067;&#0111;&#0110;&#0116;&#0101;&#0120;&#0116;&#0040;&#0034;&#0099;&#0111;&#0109;&#0047;&#0116;&#0101;&#0115;&#0116;&#0047;&#0097;&#0112;&#0112;&#0108;&#0105;&#0099;&#0097;&#0116;&#0105;&#0111;&#0110;&#0067;&#0111;&#0110;&#0116;&#0101;&#0120;&#0116;&#0046;&#0120;&#0109;&#0108;&#0034;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0077;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0032;&#0109;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0032;&#0061;&#0032;&#0040;&#0077;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0041;&#0099;&#0116;&#0120;&#0046;&#0103;&#0101;&#0116;&#0066;&#0101;&#0097;&#0110;&#0040;&#0034;&#0105;&#0110;&#0116;&#0101;&#0114;&#0099;&#0101;&#0112;&#0116;&#0101;&#0100;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0034;&#0041;&#0059;&#0010;&#0032;&#0032;&#0032;&#0032;&#0109;&#0121;&#0083;&#0101;&#0114;&#0118;&#0105;&#0099;&#0101;&#0046;&#0100;&#0111;&#0083;&#0111;&#0109;&#0101;&#0116;&#0104;&#0105;&#0110;&#0103;&#0040;&#0041;&#0059;&#0010;&#0032;&#0032;&#0125;&#0010;&#0125;&#0010;</code></pre>
<p>So we need to look up the <code>interceptedService</code> bean in order to get the proxied service, but if we choose to remove the performance monitor we can simply lookup the initial <code>myService</code> bean.<br />Normally, after the method <code>doSomething</code> has run, you should see, as the last output line, something like this:
<pre class="prettyprint"><code>Method 'com.test.MyService.doSomething' took 281 milliseconds to run</code></pre>
<p>Except from the <code>MethodInterceptor</code> Spring also offers other method interception strategies. For example you can choose to handle a method execution right before or immediately after the actual call, or when an exception is thrown during the execution of your method. The reference documentation about these types of interceptors that Spring offers is available <a href="http://static.springframework.org/spring/docs/2.0.x/reference/aop-api.html#aop-api-advice-types">here</a>.</p>
<p>Please note that basic performance monitoring can also be achieved by using Spring&#8217;s built-in <code>PerformanceMonitorInterceptor</code>. We used this logic just as a sample for method intercepting, but as your intuition might tell you, this is just one of the many things you can do with this feature of Spring. For example, if you need to implement a fine-grained security module, you might choose not to allow the method call to execute if the user does not have rights on the business method. So, basically, you will have to see for yourself how you can use this functionality in your application.</p>
<p>I hope you find this article useful.<br />Have a happy coding day <img src='http://cosminaru.ro/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://cosminaru.ro/blog/2006/10/16/spring-by-examples-method-interceptors/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>

