<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5210551511410992690</id><updated>2012-01-25T00:41:15.423-08:00</updated><category term='Builder Pattern in Java'/><category term='BeanFactory and ApplicationContext in Spring'/><category term='design patterns'/><category term='javascript'/><category term='Template Method Pattern'/><category term='Dependency Injection'/><category term='arrays in javascript'/><category term='Hollywood principle'/><category term='Integrating Spring with Struts'/><category term='difference between adapter pattern and some of the other patterns'/><category term='Assosiative Arrays in PL/SQL'/><category term='Composite Pattern'/><category term='Struts with Spring'/><category term='injecting HasMap'/><category term='Inner Classes in Java'/><category term='State Design Pattern'/><category term='Chain Of Responsibility Pattern'/><category term='Persistence in Spring'/><category term='Education and the Significance of Life'/><category term='Decorator Pattern in Java'/><category term='ApplicationContext in Spring'/><category term='Iterator Pattern in Java'/><category term='Memento Pattern in Java'/><category term='Singleton Pattern in Java'/><category term='Exceptions in Spring'/><category term='truncate table irrespective of constraints'/><category term='Integrating Hibernate with Spring'/><category term='sorting'/><category term='Mediator Pattern in Java'/><category term='Visitor Pattern in Java'/><category term='Comparator'/><category term='how and when to use adapter pattern'/><category term='Flyweight Patternin Java'/><category term='Generics in Java'/><category term='Bridge Pattern in Java'/><category term='Collections in Java'/><category term='ORA-02266'/><category term='Configuring a data source in Spring'/><category term='override equals() and hashCode()'/><category term='facade pattern'/><category term='BeanFactory in Spring'/><category term='name-value pairs in PL/SQL'/><category term='Comparable'/><category term='Class Loaders in Java'/><category term='Dependency Injection in Spring'/><category term='Prototype Pattern in Java'/><category term='proxy pattern'/><category term='principle of least knowledge'/><category term='adapter pattern'/><category term='example of adapter pattern'/><title type='text'>Sneha's Blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>42</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-7986618936789776487</id><published>2010-06-16T06:58:00.000-07:00</published><updated>2010-06-16T07:08:17.547-07:00</updated><title type='text'>Ajax with Direct Web Remoting (DWR)</title><content type='html'>This is my blog entry after a long time.&lt;br /&gt;&lt;br /&gt;Recently i have used ajax with dwr (Direct Web Remoting) and it was very useful. I would like to discuss the same this time.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;DWR allows the us to call server-side Java objects from javascript with a great ease.&lt;/strong&gt; &lt;br /&gt;&lt;br /&gt;- Using DWR you can call server-side Java objects from javascript directly, so you can eliminate entire request-response handling code.&lt;br /&gt;&lt;br /&gt;- DWR is deployed as a servlet within your Web application. But there is no need for you to write the servlet code to call the back end object.&lt;br /&gt;&lt;br /&gt;- We can introduce the classes we require to call objects from. For each exposed class, DWR dynamically generates JavaScript to include in your Web page. The generated JavaScript contains stub functions that represent the corresponding methods on the Java class and also performs XMLHttpRequests behind the scenes. These requests are sent to the DWR servlet, which, in its second role, translates the request into a method call on a server-side Java object and sends the method's return value back to the client side in its servlet response, encoded into JavaScript.&lt;br /&gt;&lt;br /&gt;- There are some utility functions provided to perform common UI tasks.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;How to use it?&lt;br /&gt;&lt;br /&gt;Ensure you have dwr.jar in your classpath.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Main components include:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;- jsp event handler to call javascript.&lt;br /&gt;&lt;br /&gt;- javascript function to call the server-side class.&lt;br /&gt;&lt;br /&gt;- Server-side class to make the actual database call.&lt;br /&gt;&lt;br /&gt;- dwr.xml file to expose the server-side class to the client.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now let us look at an example.&lt;br /&gt;&lt;br /&gt;In our jsp file, if you select a stream type, all the streams with that stream type should be populated dynamically.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;select&lt;/font&gt; &lt;font color="#2040a0"&gt;id=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;streamType&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;name=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;streamType&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;onchange=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;onStreamTypeChange();&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;!--&lt;/font&gt; &lt;font color="#2040a0"&gt;options&lt;/font&gt; &lt;font color="#2040a0"&gt;holding&lt;/font&gt; &lt;font color="#2040a0"&gt;the&lt;/font&gt; &lt;font color="#2040a0"&gt;list&lt;/font&gt; &lt;font color="#2040a0"&gt;of&lt;/font&gt; &lt;font color="#2040a0"&gt;stream&lt;/font&gt; &lt;font color="#2040a0"&gt;types--!&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/select&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;select&lt;/font&gt; &lt;font color="#2040a0"&gt;id=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;streamDesc&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;name=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;streamDesc&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;         &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;option&lt;/font&gt; &lt;font color="#2040a0"&gt;value=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="ff0000"&gt;&amp;amp;nbsp;&lt;/font&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/option&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/select&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This calls the below javascript function to populate the relevant streams, whenever the value of this select list is changed (stream type is changed).&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;function&lt;font color="ff0000"&gt; onStreamTypeChange&lt;/font&gt;&lt;font color="2040a0"&gt;()&lt;/font&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;        l &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;&lt;strong&gt;document&lt;/strong&gt;&lt;/font&gt;.findJobs.streamType&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;        sST &lt;font color="4444FF"&gt;=&lt;/font&gt; l.&lt;font color="#2040a0"&gt;&lt;strong&gt;options&lt;/strong&gt;&lt;/font&gt;[l.&lt;font color="#2040a0"&gt;&lt;strong&gt;selectedIndex&lt;/strong&gt;&lt;/font&gt;].&lt;font color="#2040a0"&gt;&lt;strong&gt;value&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;        StreamAdapter.getStreamsForStreamType(sST, populateStreams)&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="#444444"&gt;/*&lt;br /&gt;  StreamAdapter           - JavaScriptName - specified in dwr.xml(explained below)&lt;br /&gt;  getStreamsForStreamType - methodName&lt;br /&gt;  sST                     - methodParams ...&lt;br /&gt;  populateStreams         - callBack function&lt;br /&gt; */&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This function passes the selected streamType into a method called getStreamsForStreamType of the object StreamAdapter. &lt;br /&gt;&lt;br /&gt;After the remote method is called, the results from that method are passed to the popStreams javascript callback function(populateStreams).&lt;br /&gt;&lt;br /&gt;- NOTE : callback function should be the last parameter. You can have multiple method params before that.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;function&lt;font color="ff0000"&gt; populateStreams&lt;/font&gt;&lt;font color="2040a0"&gt;(data)&lt;/font&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;        DWRUtil.removeAllOptions(&lt;font color="#008000"&gt;&amp;quot;streamDesc&amp;quot;&lt;/font&gt;)&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// Utility functions provided by DWR&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;        DWRUtil.addOptions(&lt;font color="#008000"&gt;&amp;quot;streamDesc&amp;quot;&lt;/font&gt;, data)&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// Utility functions provided by DWR&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;        &lt;font color="#444444"&gt;/*&lt;br /&gt;   if you want to access the elements inside the list, &lt;br /&gt;   then incoming list can be accessed as an array and&lt;br /&gt;   elements can be accessed as below...&lt;br /&gt;  &lt;br /&gt;   streamDesc_1 = data[0];&lt;br /&gt;   streamDesc_2 = data[1];&lt;br /&gt;&lt;br /&gt; */&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;        &lt;strong&gt;var&lt;/strong&gt; l &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;&lt;strong&gt;document&lt;/strong&gt;&lt;/font&gt;.findJobs.streamDesc&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;        &lt;br /&gt;&lt;br /&gt;        &lt;strong&gt;for&lt;/strong&gt;(i&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; i &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt; l.&lt;font color="#2040a0"&gt;&lt;strong&gt;options&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;&lt;strong&gt;length&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; i++)&lt;br /&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;          &lt;strong&gt;if&lt;/strong&gt;(l.&lt;font color="#2040a0"&gt;&lt;strong&gt;options&lt;/strong&gt;&lt;/font&gt;[i].&lt;font color="#2040a0"&gt;&lt;strong&gt;value&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;&amp;lt;c:out value=&amp;quot;&lt;/font&gt;$&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;sessionScope.findJobs.streamDesc&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot; /&amp;gt;&amp;quot;&lt;/font&gt;)&lt;br /&gt;&lt;br /&gt;          &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;            l.&lt;font color="#2040a0"&gt;&lt;strong&gt;selectedIndex&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; i&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;          &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This method contains the logic to adjust the DOM to reflect the data coming back from the server-side. You will see there are a couple of DWR util functions in there to remove the current options from the select list and then add new options, which have come back from the server. The function then selects a value from the new select list based on a session attribute.&lt;br /&gt;&lt;br /&gt;StreamAdapter is a server-side class that actually makes a database call. This class takes in the parameters sent from the JS function, calls down to a DAO class to get some results and returns a List back to the client side as below.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;StreamAdapter&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;  &lt;br /&gt;  &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;getStreamsForStreamType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;streamType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;strong&gt;throws&lt;/strong&gt; &lt;font color="#2040a0"&gt;Exception&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;font color="#2040a0"&gt;CommonJobDAO&lt;/font&gt; &lt;font color="#2040a0"&gt;dao&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;CommonJobDAO&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;result&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;dao&lt;/font&gt;.&lt;font color="#2040a0"&gt;getStreamsForStreamType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;streamType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;result&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt;  &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;StreamBean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &amp;quot;&lt;font color="#2040a0"&gt;getStreamDetails&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;stream_id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; ....&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In order to expose the StreamAdapter class to the client, there is a config file called dwr.xml. This file tells DWR to create a new instance of the StreamAdapter class whenever it is called and gives the location of the class as below.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;!DOCTYPE&lt;/font&gt; &lt;font color="#2040a0"&gt;dwr&lt;/font&gt; &lt;font color="#2040a0"&gt;PUBLIC&lt;br /&gt;&lt;/font&gt;    &lt;font color="#008000"&gt;&amp;quot;-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN&amp;quot;&lt;/font&gt;&lt;font color="#2040a0"&gt;&lt;br /&gt;&lt;/font&gt;    &lt;font color="#008000"&gt;&amp;quot;http://getahead.org/dwr/dwr20.dtd&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;dwr&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;allow&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;create&lt;/font&gt; &lt;font color="#2040a0"&gt;creator=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;new&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;javascript=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;StreamAdapter&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;      &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;param&lt;/font&gt; &lt;font color="#2040a0"&gt;name=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;class&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;com.myproject.adapters.StreamAdapter&amp;quot;&lt;/font&gt;&lt;font color="#2040a0"&gt;/&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;include&lt;/font&gt; &lt;font color="#2040a0"&gt;method=&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;getStreamsForStreamType&amp;quot;&lt;/font&gt;&lt;font color="#2040a0"&gt;/&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;  &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;!OPTIONAL&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;include&lt;/font&gt; &lt;font color="#2040a0"&gt;method=&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;getStreamDetails&amp;quot;&lt;/font&gt;&lt;font color="#2040a0"&gt;/&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;  &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;!OPTIONAL&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/create&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/allow&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;allow&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;create&lt;/font&gt; &lt;font color="#2040a0"&gt;creator=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;new&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;javascript=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;AnotherAdapter&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;      &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;param&lt;/font&gt; &lt;font color="#2040a0"&gt;name=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;class&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;com.myproject.adapter.FieldAdapter&amp;quot;&lt;/font&gt;&lt;font color="#2040a0"&gt;/&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/create&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/allow&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/dwr&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above is the simpler version of dwr.xml file.&lt;br /&gt;&lt;br /&gt;if you are retrieving List&lt;StreamBean&gt; or Map&lt;int,String&gt; then you can use convert element which specifies the members to include in the JavaScript serialization of StreamBean.&lt;br /&gt;&lt;br /&gt;You can declare the dwr servlet as below in web.xml file.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;servlet&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;servlet-name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;dwr-invoker&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/servlet-name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;display-name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;DWR Servlet&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/display-name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;servlet-class&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;org.directwebremoting.servlet.DwrServlet&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/servlet-class&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;init-param&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;       &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;param-name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;debug&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/param-name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;       &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;param-value&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;false&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/param-value&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/init-param&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/servlet&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;servlet-mapping&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;servlet-name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;dwr-invoker&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/servlet-name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;url-pattern&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;/dwr/*&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/url-pattern&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/servlet-mapping&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Pros and cons of DWR:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;- Very easy to use.&lt;br /&gt;- very quick.&lt;br /&gt;- DWR does a great job of hiding the Ajax machinery, but it's important to remember that the network is not transparent .&lt;br /&gt;&lt;br /&gt;Hope it will be useful.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;References&lt;/strong&gt; : &lt;br /&gt;http://www.ibm.com/developerworks/java/library/j-ajax3/&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-7986618936789776487?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/7986618936789776487/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=7986618936789776487' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/7986618936789776487'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/7986618936789776487'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2010/06/ajax-with-direct-web-remoting-dwr.html' title='Ajax with Direct Web Remoting (DWR)'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-5600709056244154629</id><published>2009-05-21T08:07:00.000-07:00</published><updated>2009-05-21T08:16:17.145-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Integrating Spring with Struts'/><category scheme='http://www.blogger.com/atom/ns#' term='Struts with Spring'/><title type='text'>Method C :  Delegate Struts Action management to the Spring framework</title><content type='html'>Here  we are going to delegate Struts action management to the Spring framework. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;All you have to do is...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For each action that uses Spring, you need to define the action mapping to use org.springframework.web.struts.DelegatingActionProxy and declare a matching (action "path" == bean "name") Spring bean for the actual Struts action. This is an example of an action that requires an instance of UserDatabase:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Note the changes in struts-config file.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Inside action-mappings instead of declaring the action's class name, it registers the name of Spring's proxy class. The DelegatingActionProxy class uses the action mapping name to look up the action in the Spring context. This is the context that was declared with ContextLoaderPlugIn.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;struts&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;config&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;searchForm&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.apache.struts.validator.DynaValidatorForm&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;               &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt;    &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;java.lang.Long&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;global&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;forwards&lt;/font&gt; &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.apache.struts.action.ActionForward&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt;   &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;welcome&amp;quot;&lt;/font&gt;                &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/welcome.do&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt;   &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;searchEntry&amp;quot;&lt;/font&gt;            &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchEntry.do&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt;   &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;searchSubmit&amp;quot;&lt;/font&gt;           &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchSubmit.do&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;global&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;forwards&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;action&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;mappings&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;action&lt;/font&gt;    &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/welcome&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;forward&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/pages/welcome.htm&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;action&lt;/font&gt;    &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchEntry&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;forward&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/pages/search.jsp&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;               &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;action&lt;/font&gt;    &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchSubmit&amp;quot;&lt;/font&gt;&lt;br /&gt;               &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.web.struts.DelegatingActionProxy&amp;quot;&lt;/font&gt;&lt;br /&gt;               &lt;font color="#2040a0"&gt;input&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchEntry.do&amp;quot;&lt;/font&gt;&lt;br /&gt;               &lt;font color="#2040a0"&gt;validate&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;true&amp;quot;&lt;/font&gt;&lt;br /&gt;               &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;searchForm&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;              &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;success&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/pages/detail.jsp&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;              &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;failure&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/pages/search.jsp&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;action&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;  &lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;action&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;mappings&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;message&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;resources&lt;/font&gt; &lt;font color="#2040a0"&gt;parameter&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;ApplicationResources&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt; &lt;font color="#2040a0"&gt;className&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;set&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;pathnames&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt; &lt;font color="#2040a0"&gt;className&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.web.struts.ContextLoaderPlugIn&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;set&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;contextConfigLocation&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/beans.xml&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;struts&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;config&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The corresponding Spring bean configuration...&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_1&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.beans.Employee&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;name&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Sneha&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_2&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.beans.Employee&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;2&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;name&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Prashant&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.business.EmployeeImpl&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employees&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;map&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;entry&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ref&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_1&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;entry&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;entry&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;2&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ref&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_2&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;entry&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;map&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchSubmit&amp;quot;&lt;/font&gt; &lt;br /&gt;    &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.actions.FindEmployeeAction&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ref&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Merits:&lt;/span&gt;&lt;br /&gt;1. As the action is under Spring's control, it populates the action's JavaBean properties and leaves the door open to applying features such as Spring's AOP interceptors.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;The action-delegation solution is the best of the three.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;br /&gt;&lt;br /&gt;References : &lt;a href ="http://www.ibm.com/developerworks/java/library/j-sr2.html" target="_blank"&gt; http://www.ibm.com/developerworks/java/library/j-sr2.html &lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-5600709056244154629?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/5600709056244154629/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=5600709056244154629' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/5600709056244154629'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/5600709056244154629'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/05/method-c-delegate-struts-action.html' title='Method C :  Delegate Struts Action management to the Spring framework'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-4526680507836689119</id><published>2009-05-21T08:05:00.000-07:00</published><updated>2009-05-21T08:16:54.002-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Integrating Spring with Struts'/><category scheme='http://www.blogger.com/atom/ns#' term='Struts with Spring'/><title type='text'>Method B : Override the Struts RequestProcessor with Spring's DelegatingRequestProcessor</title><content type='html'>To avoid the above disadvantage of coupling the Struts Action to the Spring framework, you can override the Struts RequestProcessor processor with the org.springframework.web.struts.DelegatingRequestProcessor class.&lt;br /&gt;&lt;br /&gt;To do the above just use &lt;controller&gt; tag to override the default Struts RequestProcessor with the DelegatingRequestProcessor in Struts-config file. Then register the action in Spring config file where you register other beans.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="#2040a0"&gt;xml&lt;/font&gt; &lt;font color="#2040a0"&gt;version&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1.0&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;encoding&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;ISO-8859-1&amp;quot;&lt;/font&gt; ?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="4444FF"&gt;!&lt;/font&gt;&lt;font color="#2040a0"&gt;DOCTYPE&lt;/font&gt; &lt;font color="#2040a0"&gt;struts&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;config&lt;/font&gt; &lt;font color="#2040a0"&gt;PUBLIC&lt;/font&gt;&lt;br /&gt;          &lt;font color="#008000"&gt;&amp;quot;-//Apache Software Foundation//DTD Struts Configuration 1.1//EN&amp;quot;&lt;/font&gt;&lt;br /&gt;          &lt;font color="#008000"&gt;&amp;quot;http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;struts&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;config&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;searchForm&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.apache.struts.validator.DynaValidatorForm&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;               &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt;    &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;java.lang.Long&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;global&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;forwards&lt;/font&gt; &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.apache.struts.action.ActionForward&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt;   &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;welcome&amp;quot;&lt;/font&gt;                &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/welcome.do&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt;   &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;searchEntry&amp;quot;&lt;/font&gt;            &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchEntry.do&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt;   &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;searchSubmit&amp;quot;&lt;/font&gt;           &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchSubmit.do&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;global&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;forwards&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;action&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;mappings&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;action&lt;/font&gt;    &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/welcome&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;forward&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/pages/welcome.htm&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;action&lt;/font&gt;    &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchEntry&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;forward&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/pages/search.jsp&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;               &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;action&lt;/font&gt;    &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchSubmit&amp;quot;&lt;/font&gt;&lt;br /&gt;               &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.actions.FindEmployeeAction&amp;quot;&lt;/font&gt;&lt;br /&gt;               &lt;font color="#2040a0"&gt;input&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchEntry.do&amp;quot;&lt;/font&gt;&lt;br /&gt;               &lt;font color="#2040a0"&gt;validate&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;true&amp;quot;&lt;/font&gt;&lt;br /&gt;               &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;searchForm&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;              &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;success&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/pages/detail.jsp&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;              &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;forward&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;failure&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/pages/search.jsp&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;action&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;  &lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;action&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;mappings&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;message&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;resources&lt;/font&gt; &lt;font color="#2040a0"&gt;parameter&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;ApplicationResources&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;controller&lt;/font&gt; &lt;font color="#2040a0"&gt;processorClass&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.web.struts.DelegatingRequestProcessor&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt; &lt;font color="#2040a0"&gt;className&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;set&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;pathnames&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt; &lt;font color="#2040a0"&gt;className&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.web.struts.ContextLoaderPlugIn&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;set&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;contextConfigLocation&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/beans.xml&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;struts&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;config&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;and here is Spring config file. Register the action here and remember name attribute to match the struts-config action mapping name.&lt;br /&gt;employeeService property will be populated at runtime using getter injection.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="#2040a0"&gt;xml&lt;/font&gt; &lt;font color="#2040a0"&gt;version&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1.0&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;encoding&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;UTF-8&amp;quot;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="4444FF"&gt;!&lt;/font&gt;&lt;font color="#2040a0"&gt;DOCTYPE&lt;/font&gt; &lt;font color="#2040a0"&gt;beans&lt;/font&gt; &lt;font color="#2040a0"&gt;PUBLIC&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;-//SPRING//DTD BEAN//EN&amp;quot;&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;http://www.springframework.org/dtd/spring-beans.dtd&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_1&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.beans.Employee&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;name&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Sneha&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_2&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.beans.Employee&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;2&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;name&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Prashant&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.business.EmployeeImpl&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employees&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;map&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;entry&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ref&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_1&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;entry&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;entry&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;2&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ref&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_2&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;entry&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;map&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/searchSubmit&amp;quot;&lt;/font&gt; &lt;br /&gt;    &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.actions.FindEmployeeAction&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ref&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now your action class extend the Struts's org.apache.struts.action.Action Class instead of Spring ActionSupport Class.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;FindEmployeeAction&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Action&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt; &lt;font color="#2040a0"&gt;employeeService&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt; &lt;font color="#2040a0"&gt;getEmployeeService&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;employeeService&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setEmployeeService&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt; &lt;font color="#2040a0"&gt;employeeService&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;font color="#444444"&gt;//1&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;employeeService&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;employeeService&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;ActionForward&lt;/font&gt; &lt;font color="#2040a0"&gt;execute&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;ActionMapping&lt;/font&gt; &lt;font color="#2040a0"&gt;mapping&lt;/font&gt;,&lt;br /&gt;   &lt;font color="#2040a0"&gt;ActionForm&lt;/font&gt; &lt;font color="#2040a0"&gt;form&lt;/font&gt;,&lt;br /&gt;   &lt;font color="#2040a0"&gt;HttpServletRequest&lt;/font&gt; &lt;font color="#2040a0"&gt;request&lt;/font&gt;,&lt;br /&gt;   &lt;font color="#2040a0"&gt;HttpServletResponse&lt;/font&gt; &lt;font color="#2040a0"&gt;response&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;strong&gt;throws&lt;/strong&gt; &lt;font color="#2040a0"&gt;IOException&lt;/font&gt;, &lt;font color="#2040a0"&gt;ServletException&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;   &lt;font color="#2040a0"&gt;DynaActionForm&lt;/font&gt; &lt;font color="#2040a0"&gt;searchForm&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;DynaActionForm&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;Long&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;Long&lt;/font&gt;.&lt;font color="#2040a0"&gt;parseLong&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;searchForm&lt;/font&gt;.&lt;font color="#2040a0"&gt;get&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;toString&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;   &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Inside FindEmployeeAction_2 id = &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;         &lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;getEmployeeService&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;findEmployeeById&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;   &lt;strong&gt;if&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;null&lt;/strong&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee not found&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;    &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;mapping&lt;/font&gt;.&lt;font color="#2040a0"&gt;findForward&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;failure&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;   &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee found&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;request&lt;/font&gt;.&lt;font color="#2040a0"&gt;setAttribute&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;emp&amp;quot;&lt;/font&gt;, &lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;mapping&lt;/font&gt;.&lt;font color="#2040a0"&gt;findForward&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;success&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As you have seen at &lt;span style="font-weight:bold;"&gt;1&lt;/span&gt; employeeService JavaBean property will be automatically populated by the DelegatingRequestProcessor.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Merits:&lt;/span&gt;&lt;br /&gt;1. Protects the Struts action from knowing it's being managed by Spring while giving you all the benefits of Spring's action management framework. &lt;br /&gt;2. Because your Struts actions are oblivious to the existence of Spring, you can swap out Spring for some other inversion of control container without refactoring your Struts code.&lt;br /&gt;3. This approach is definitely better than the first one&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Demerits:&lt;/span&gt;&lt;br /&gt;1. If you were using a different RequestProcessor, then you would need to integrate the Spring DelegatingRequestProcessor manually. &lt;br /&gt;2. The added code would become a maintenance hassle and would also reduce your application's flexibility going forward. Moreover, there has been some talk of replacing the Struts RequestProcessor with a chain of command. Such a change would negatively impact the longevity of this solution.&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;br /&gt;&lt;br /&gt;References : &lt;a href ="http://www.ibm.com/developerworks/java/library/j-sr2.html" target="_blank"&gt; http://www.ibm.com/developerworks/java/library/j-sr2.html &lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-4526680507836689119?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/4526680507836689119/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=4526680507836689119' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/4526680507836689119'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/4526680507836689119'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/05/method-b-override-struts.html' title='Method B : Override the Struts RequestProcessor with Spring&apos;s DelegatingRequestProcessor'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-4246908644784532697</id><published>2009-05-21T08:01:00.000-07:00</published><updated>2009-05-21T08:17:22.889-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Integrating Spring with Struts'/><category scheme='http://www.blogger.com/atom/ns#' term='Struts with Spring'/><title type='text'>Method A : Using Spring's ActionSupport Class to integrate Struts</title><content type='html'>Here we are going to use Spring's ActionSupport class to integrate Struts.&lt;br /&gt;&lt;br /&gt;Action classes of Struts framework do not get started as Spring framework beans, thus cannot be wired through Dependency Injection&lt;br /&gt;&lt;br /&gt;If the Action class wants to access beans of Spring framework, it has to access them manually.&lt;br /&gt;&lt;br /&gt;Instead of extending Action class, you can extend Spring's org.springframework.web.struts.ActionSupport Class which provides getWebApplicationContext() method to obtain a Spring context&lt;br /&gt;&lt;br /&gt;Action Support provides a reference to the current Spring application context, e.g. for bean lookup or resource loading&lt;br /&gt;- Auto-detects a ContextLoaderPlugIn context, falling back to the root WebApplicationContext.&lt;br /&gt;- For typical usage, i.e. accessing middle tier beans, use a root WebApplicationContext.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Below is how your Struts Action Class look like :&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;FindEmployeeAction&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;ActionSupport&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;font color="#444444"&gt;//1&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;ActionForward&lt;/font&gt; &lt;font color="#2040a0"&gt;execute&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;ActionMapping&lt;/font&gt; &lt;font color="#2040a0"&gt;mapping&lt;/font&gt;,&lt;br /&gt;   &lt;font color="#2040a0"&gt;ActionForm&lt;/font&gt; &lt;font color="#2040a0"&gt;form&lt;/font&gt;,&lt;br /&gt;   &lt;font color="#2040a0"&gt;HttpServletRequest&lt;/font&gt; &lt;font color="#2040a0"&gt;request&lt;/font&gt;,&lt;br /&gt;   &lt;font color="#2040a0"&gt;HttpServletResponse&lt;/font&gt; &lt;font color="#2040a0"&gt;response&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;strong&gt;throws&lt;/strong&gt; &lt;font color="#2040a0"&gt;IOException&lt;/font&gt;, &lt;font color="#2040a0"&gt;ServletException&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;   &lt;font color="#2040a0"&gt;DynaActionForm&lt;/font&gt; &lt;font color="#2040a0"&gt;searchForm&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;DynaActionForm&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;form&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;Long&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;Long&lt;/font&gt;.&lt;font color="#2040a0"&gt;parseLong&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;searchForm&lt;/font&gt;.&lt;font color="#2040a0"&gt;get&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;toString&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;   &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Inside FindEmployeeAction id = &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;   &lt;font color="#2040a0"&gt;ApplicationContext&lt;/font&gt; &lt;font color="#2040a0"&gt;ctx&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;getWebApplicationContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;//2&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt; &lt;font color="#2040a0"&gt;employeeService&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;ctx&lt;/font&gt;.&lt;font color="#2040a0"&gt;getBean&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;//3&lt;/font&gt;&lt;br /&gt;         &lt;br /&gt;   &lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;employeeService&lt;/font&gt;.&lt;font color="#2040a0"&gt;findEmployeeById&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;   &lt;strong&gt;if&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;null&lt;/strong&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;mapping&lt;/font&gt;.&lt;font color="#2040a0"&gt;findForward&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;failure&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;   &lt;font color="#2040a0"&gt;request&lt;/font&gt;.&lt;font color="#2040a0"&gt;setAttribute&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;emp&amp;quot;&lt;/font&gt;, &lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;mapping&lt;/font&gt;.&lt;font color="#2040a0"&gt;findForward&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;success&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Let see how the above Action Class is different from Traditional Action Class and how can it provide a reference to the current Spring context.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;1.&lt;/span&gt; Extend ActionSupport(org.springframework.web.struts.ActionSupport) Class instead Action Class.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2.&lt;/span&gt; ApplicationContext refernce is obtained through getWebApplicationContext() method which is there in ActionSupport Class.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3.&lt;/span&gt; Look up for Spring bean using the context reference you have obtained.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Have a look at Spring-config file also.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_1&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.beans.Employee&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;name&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Sneha&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_2&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.beans.Employee&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;2&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;name&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Prashant&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;struts.spring.employee.business.EmployeeImpl&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employees&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;map&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;entry&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ref&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_1&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;entry&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;entry&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;2&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ref&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee_2&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;entry&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;map&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;   &lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Merits : &lt;/span&gt;&lt;br /&gt;1. Very Simple and easy to understand.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Demerits:&lt;/span&gt;&lt;br /&gt;1. It couples the Struts action to the Spring framework. If you ever decide to replace Spring, you would have to rewrite the code. &lt;br /&gt;2. As the Struts action isn't under Spring's control, it can't reap the benefits of Spring AOP. &lt;br /&gt;3. Useful when using multiple independent Spring contexts, but for the most part it's not as desirable a solution as the other two choices.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;br /&gt;&lt;br /&gt;References : &lt;a href ="http://www.ibm.com/developerworks/java/library/j-sr2.html" target="_blank"&gt; http://www.ibm.com/developerworks/java/library/j-sr2.html &lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-4246908644784532697?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/4246908644784532697/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=4246908644784532697' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/4246908644784532697'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/4246908644784532697'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/05/using-springs-actionsupport-class-to.html' title='Method A : Using Spring&apos;s ActionSupport Class to integrate Struts'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-3781141448935794289</id><published>2009-05-20T08:31:00.000-07:00</published><updated>2009-05-21T08:13:24.256-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='injecting HasMap'/><category scheme='http://www.blogger.com/atom/ns#' term='Integrating Spring with Struts'/><category scheme='http://www.blogger.com/atom/ns#' term='Struts with Spring'/><title type='text'>Integrating Spring with Struts</title><content type='html'>Key to the Spring Framework is the freedom to choose what works best for your application. Spring offers you choices when building web applications, too. Although Spring offers its own very capable web framework, you are free to choose another if you’d like and still be able to take advantage of Spring in the other layers of your application.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Advantages of integrating a Struts application into the Spring framework:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;1.&lt;/span&gt; Spring was explicitly designed to resolve some of the real-world problems of JEE, such as complexity, poor performance, testability, and much more. &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2.&lt;/span&gt; Second, the Spring framework includes an AOP implementation that lets you apply aspect-oriented techniques to normal object-oriented code.&lt;br /&gt;&lt;br /&gt;Both Struts and Spring frameworks have their merits and drawbacks, but most would agree that Struts is still king when it comes to MVC. Many development teams have learned to rely on Struts as the foundation for building quality software under strict deadlines. With so much momentum behind Struts, even development teams that would like to integrate features of the Spring framework don't want to switch to Spring MVC. The good news is that you don't have to. The Spring architecture allows you to connect Struts as your Web framework to Spring-based business and persistence layers.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Below we will see three approaches to integrating Struts applications into the Spring framework and discuss their merits and demerits.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;A.&lt;/span&gt; Using Spring's ActionSupport class to integrate Struts.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;B.&lt;/span&gt; Override the Struts RequestProcessor with Spring's DelegatingRequestProcessor.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;C.&lt;/span&gt; Delegate Struts Action management to the Spring framework.&lt;br /&gt;&lt;br /&gt;Whatever technique you use, you will need to add the Spring ContextLoaderPlugin to load the Spring application context for the Struts ActionServlet. Simply add the plug-in to your struts-config.xml file as you would any other plug-in, as shown below:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt; &lt;font color="#2040a0"&gt;className&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.web.struts.ContextLoaderPlugIn&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;set&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;contextConfigLocation&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/WEB-INF/beans.xml&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;plug&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;in&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;and here is the code for my bean, interface and impl which is common to all three methodologies.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;Serializable&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;Long&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;Long&lt;/font&gt; &lt;font color="#2040a0"&gt;getId&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setId&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Long&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;id&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;getName&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setName&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;name&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;findEmployeeById&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Long&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;getNoOfEmployees&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmployeeImpl&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;Map&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Long&lt;/font&gt;,&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;HashMap&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Long&lt;/font&gt;,&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;findEmployeeById&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Long&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Inside findEmployeeById id = &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;employees&lt;/font&gt;.&lt;font color="#2040a0"&gt;get&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;id&lt;/font&gt;.&lt;font color="#2040a0"&gt;toString&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Employee found = &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;getNoOfEmployees&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt;.&lt;font color="#2040a0"&gt;size&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;Map&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Long&lt;/font&gt;, &lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;getEmployees&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setEmployees&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Map&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Long&lt;/font&gt;, &lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;employees&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;In my Impl Class I am having HashMap which is set at runtime using Spring's getter injection.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I have divided three methodologies into three ariticles as dont want to make it messy. Complete code is provided in the respective articles. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Method A :&lt;/span&gt; &lt;a href="http://snehaprashant.blogspot.com/2009/05/using-springs-actionsupport-class-to.html" target="_blank"&gt;&lt;span style="font-weight:bold;"&gt;A.&lt;/span&gt; Using Spring's ActionSupport class to integrate Struts.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Method B :&lt;/span&gt; &lt;a href="http://snehaprashant.blogspot.com/2009/05/method-b-override-struts.html" target="_blank"&gt;&lt;span style="font-weight:bold;"&gt;B.&lt;/span&gt; Override the Struts RequestProcessor with Spring's DelegatingRequestProcessor. &lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Method C :&lt;/span&gt; &lt;a href="http://snehaprashant.blogspot.com/2009/05/method-c-delegate-struts-action.html" target="_blank"&gt;&lt;span style="font-weight:bold;"&gt;C.&lt;/span&gt; Delegate Struts Action management to the Spring framework. &lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you directly want to go the best method. Find out my continuation article,&lt;br /&gt;&lt;a href="http://snehaprashant.blogspot.com/2009/05/method-c-delegate-struts-action.html" target="_blank"&gt;&lt;span style="font-weight:bold;"&gt;C.&lt;/span&gt; Delegate Struts Action management to the Spring framework. &lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-3781141448935794289?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/3781141448935794289/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=3781141448935794289' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/3781141448935794289'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/3781141448935794289'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/05/integrating-spring-with-struts.html' title='Integrating Spring with Struts'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-2443354257308389925</id><published>2009-05-14T08:18:00.000-07:00</published><updated>2009-05-14T08:29:33.567-07:00</updated><title type='text'>RMI using Spring</title><content type='html'>Let us look at Spring’s support to Remoting. &lt;br /&gt;&lt;br /&gt;Spring supports remoting for several different Remote Procedure Call models, including Remote Method Invocation (RMI), Caucho’s Hessian and Burlap, and Spring’s own HTTP invoker.&lt;br /&gt;&lt;br /&gt;Spring offers a POJO-based programming model for both your server and client, no matter which remoting solution you choose. This is accomplished using a proxy factory bean that enables you to wire remote services into properties of your other beans as if they were local objects.&lt;br /&gt;&lt;br /&gt;The client makes calls to the proxy as if the proxy were providing the service functionality. The proxy communicates with the remote service on behalf of the client. It handles the details of connecting and making remote calls to the remote service.&lt;br /&gt;&lt;br /&gt;If the call to the remote service results in a &lt;span style="font-weight:bold;"&gt;java.rmi.RemoteException&lt;/span&gt;, the proxy handles that exception and rethrows it as an unchecked&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;org.springframework.remoting.RemoteAccessException&lt;/span&gt;. Remote exceptions usually signal problems such as network or configuration issues that can’t be gracefully recovered from. Since there’s usually very little that a client can do to gracefully recover from a remote exception, rethrowing a RemoteAccessException makes it optional for the client to handle the exception.&lt;br /&gt;&lt;br /&gt;Spring simplifies the RMI model by providing a proxy factory bean that enables you to wire RMI services into your Spring application is if they were local JavaBeans. Spring also provides a remote exporter that makes short work of converting your Spring-managed beans into RMI services.&lt;br /&gt;&lt;br /&gt;Spring’s &lt;span style="font-weight:bold;"&gt;RmiProxyFactoryBean&lt;/span&gt; is a factory bean that creates a proxy to an RMI service. RmiProxyFactoryBean produces a proxy object that talks to remote RMI services on behalf of the client. The client talks to the proxy through the service’s interface as if the remote service were just a local POJO.&lt;br /&gt;&lt;br /&gt;RmiProxyFactoryBean certainly simplifies the use of RMI services in a Spring application. But that’s only half of an RMI conversation.&lt;br /&gt;&lt;br /&gt;Spring provides an easier way to publish RMI services. Instead of writing RMI-specific classes with methods that throw RemoteException, you simply write a POJO that performs the functionality of your service. Spring handles the rest.&lt;br /&gt;&lt;br /&gt;For a typical Spring Application we need the following files:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;1.&lt;/span&gt; An interface that defines the functions.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2.&lt;/span&gt; An Implementation that contains properties, its setter and getter methods, functions etc.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3.&lt;/span&gt; A XML file called Spring configuration file.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;4.&lt;/span&gt; Client program that uses the function&lt;br /&gt;&lt;br /&gt;Because the service interface doesn’t extend java.rmi.Remote and none of its methods throw java.rmi.RemoteException, this trims the interface down a bit. But more importantly, a client accessing the service through this interface will not have to catch exceptions that they probably won’t be able to deal with. Instead of generating a server skeleton and client stub using rmic and manually adding it to the RMI registry (as you would in conventional RMI), we’ll use Spring’s RmiServiceExporter.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;RmiServiceExporter&lt;/span&gt; exports any Spring-managed bean as an RMI service. RmiServiceExporter works by wrapping the bean in an adapter class. The adapter class is then bound to the RMI registry and proxies requests to the service class.&lt;br /&gt;&lt;br /&gt;The simplest way to use RmiServiceExporter to expose the employeeService bean as an RMI service is to configure it in Spring with the following XML:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.remoting.rmi.RmiServiceExporter&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;serviceName&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee-service&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;service&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;ref&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;serviceInterface&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;rmi.common.EmployeeI&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;registryPort&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1234&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here the employeeService bean is wired into the service property to indicate that RmiServiceExporter is going to export the bean as an RMI service. ServiceName property names the RMI service and the serviceInterface property specifies the interface implemented by the service.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;rmi.server.EmployeeImpl&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Now let us look at an example...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let us have simple employee recruitment service exposed through EmployeeI interface which contains methods to add, remove and get number of employees.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;Serializable&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;address&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;,&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;address&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;name&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;address&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;address&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;font color="#444444"&gt;// getters and setters&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;addEmployee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;removeEmployee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;    &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;getEmployees&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;  &lt;br /&gt;    &lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here is the implementation for EmployeeI interface.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmployeeImpl&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;    &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;    &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;addEmployee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="#2040a0"&gt;employees&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;removeEmployee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;     &lt;font color="#2040a0"&gt;employees&lt;/font&gt;.&lt;font color="#2040a0"&gt;remove&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;    &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;getEmployees&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;        &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;On the server side, we need to configure Spring to export the service through RMI. As we discussed earlier we can do it by RmiServiceExporter.&lt;br /&gt;The interface can be accessed by using RmiProxyFactoryBean, or via plain RMI in case of a traditional RMI service. The RmiServiceExporter explicitly supports the exposing of any non-RMI services via RMI invokers. Of course, we first have to set up our service in the Spring container:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;rmi.server.EmployeeImpl&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;        &lt;br /&gt;    &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.remoting.rmi.RmiServiceExporter&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;serviceName&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employee-service&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;service&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;ref&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;serviceInterface&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;rmi.common.EmployeeI&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;registryPort&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1234&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now to run the server side service you need Spring context initialization.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmpServerDemo&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;main&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;args&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;ApplicationContext&lt;/font&gt; &lt;font color="#2040a0"&gt;ctx&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ClassPathXmlApplicationContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;rmi/server/rmi-server-context.xml&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now let us have a look at client side.&lt;br /&gt;&lt;br /&gt;To link in the service on the client, we'll create a separate Spring container, containing the simple object and the service linking configuration bits:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.remoting.rmi.RmiProxyFactoryBean&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;serviceUrl&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;rmi://localhost:1234/employee-service&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;serviceInterface&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;rmi.common.EmployeeI&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You can make client calls through the below code...&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmpClientDemo&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;main&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;args&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;ApplicationContext&lt;/font&gt; &lt;font color="#2040a0"&gt;ctx&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ClassPathXmlApplicationContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;rmi/client/rmi-client-context.xml&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;EmployeeI&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;ctx&lt;/font&gt;.&lt;font color="#2040a0"&gt;getBean&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeService&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;employee&lt;/font&gt;.&lt;font color="#2040a0"&gt;addEmployee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Prashant&amp;quot;&lt;/font&gt;, &lt;font color="#008000"&gt;&amp;quot;address1&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;employee&lt;/font&gt;.&lt;font color="#2040a0"&gt;addEmployee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Sneha&amp;quot;&lt;/font&gt;, &lt;font color="#008000"&gt;&amp;quot;address2&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;employee&lt;/font&gt;.&lt;font color="#2040a0"&gt;getEmployees&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Total number of employees: &amp;quot;&lt;/font&gt; &lt;font color="4444FF"&gt;+&lt;/font&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt;.&lt;font color="#2040a0"&gt;size&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;Iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;it&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;employees&lt;/font&gt;.&lt;font color="#2040a0"&gt;iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;while&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;it&lt;/font&gt;.&lt;font color="#2040a0"&gt;hasNext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;Employee&lt;/font&gt; &lt;font color="#2040a0"&gt;emp&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Employee&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;it&lt;/font&gt;.&lt;font color="#2040a0"&gt;next&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot; &amp;quot;&lt;/font&gt; &lt;font color="4444FF"&gt;+&lt;/font&gt; &lt;font color="#2040a0"&gt;emp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;First run the EmpServerDemo in one console to launch the RMI server and then run the EmpClientDemo in another console. &lt;br /&gt;&lt;br /&gt;RMI with Spring is as simple as that.&lt;br /&gt;&lt;br /&gt;Hope it will be useful....&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;References : &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.manning.com/walls2/" target="_blank"&gt;Spring in Action&lt;/a&gt; by Craig Walls.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://cosminaru.ro/blog/2006/11/02/spring-by-examples-rmi/" target="_blank"&gt;http://cosminaru.ro/blog/2006/11/02/spring-by-examples-rmi/&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-2443354257308389925?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/2443354257308389925/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=2443354257308389925' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/2443354257308389925'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/2443354257308389925'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/05/rmi-using-spring.html' title='RMI using Spring'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-820039533041760505</id><published>2009-05-07T06:51:00.000-07:00</published><updated>2009-05-07T06:59:55.557-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Persistence in Spring'/><category scheme='http://www.blogger.com/atom/ns#' term='Configuring a data source in Spring'/><category scheme='http://www.blogger.com/atom/ns#' term='Exceptions in Spring'/><title type='text'>Persistence in Spring</title><content type='html'>Last week I read &lt;a href="http://www.manning.com/walls2/" target="_blank"&gt;Spring in Action&lt;/a&gt; by Craig Walls.&lt;br /&gt;&lt;br /&gt;It was very good book and I found it very informative.&lt;br /&gt;&lt;br /&gt;I would like to share few quotations from the book, which gives generic idea about Persistance in Spring.&lt;br /&gt;&lt;br /&gt;Spring comes with a family of data access frameworks that integrate with a variety of data access technologies. Whether you are persisting your data via direct JDBC, iBATIS, or an object relational mapping (ORM) framework like Hibernate, Spring removes the tedium of data access from your persistence code. Instead, you can lean on Spring to handle the low-level data access work for you so that you can turn your attention to managing your application’s data.&lt;br /&gt;&lt;br /&gt;Spring supports persistence mechanisms like JDBC, Hibernate, the Java Persistence API (JPA), iBATIS etc.&lt;br /&gt;&lt;br /&gt;DAO stands for data access object, which perfectly describes a DAO’s role in an application. DAOs exist to provide a means to read and write data to the database. &lt;br /&gt;&lt;br /&gt;They should expose this functionality through an interface by which the rest of the application will access them. This has a couple of advantages. First, it makes your service objects easily testable since they are not coupled to a specific data access implementation.&lt;br /&gt;&lt;br /&gt;In addition, the data access tier is accessed in a persistence technology–agnostic manner. That is, the chosen persistence approach is isolated to the DAO while only the relevant data access methods are exposed through the interface. This makes for a flexible application design and allows the chosen persistence framework to be swapped out with minimal impact to the rest of the application. If the implementation details of the data access tier were to leak into other parts of the application, the entire application would become coupled with the data access&lt;br /&gt;tier, leading to a rigid application design.&lt;br /&gt;&lt;br /&gt;One way Spring helps you insulate your data access tier from the rest of your application is by providing you with a consistent exception hierarchy that is used across all of its DAO frameworks.&lt;br /&gt;&lt;br /&gt;In JDBC , you can’t do anything with JDBC without being forced to catch java.sql.SQLException. SQLException is treated as a “one size&lt;br /&gt;fits all” exception for problems related to data access. Rather than have a different exception type for each possible problem, SQLException is sthe exception that’s thrown for all data access problems.&lt;br /&gt;&lt;br /&gt;Even so, Hibernate’s exceptions are specific to Hibernate. As stated before, we’d like to isolate the specifics of the persistence mechanism to the data access layer. If Hibernate-specific exceptions are being thrown then the fact that we’re dealing with Hibernate will leak into the rest of the application. Either that, or you’ll be forced to catch persistence platform exceptions and rethrow them as platform-agnostic exceptions.&lt;br /&gt;&lt;br /&gt;On one hand, JDBC’s exception hierarchy is too generic—in fact, it’s not much of a hierarchy at all. On the other hand, Hibernate’s exception hierarchy is proprietary to Hibernate. What we need is a hierarchy of data access exceptions that are descriptive but not directly associated with a specific persistence framework.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Spring’s persistence platform agnostic exceptions : &lt;/span&gt;&lt;br /&gt;Spring JDBC provides a hierarchy of data access exceptions that solve both problems. In contrast to JDBC, Spring provides several data access exceptions, each descriptive of the problem.&lt;br /&gt;&lt;br /&gt;Even though Spring’s exception hierarchy is far more rich than JDBC’s simple SQLException, it isn’t associated with any particular persistence solution. This means that you can count on Spring to throw a consistent set of exceptions, regardless of which persistence provider you choose. This helps to keep your persistence choice confined to the data access layer.&lt;br /&gt;&lt;br /&gt;Spring promotes the use of unchecked exceptions. This leaves the decision of whether to catch an exception in the developer’s hands. To take advantage of Spring’s data access exceptions, you must use one of Spring’s supported data access templates.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Spring applies Template method Pattern to data access.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It separates the fixed and variable parts of the data access process into two distinct classes: templates and callbacks. Templates manage the fixed part (controlling transactions, managing resources, and handling exceptions)of the process while your custom data access code (creating statements, binding parameters, and marshaling result sets) is handled in the callbacks.&lt;br /&gt;&lt;br /&gt;Spring comes with several templates to choose from, depending on your persistence platform choice. If you’re using straight JDBC then you’ll want to use JdbcTemplate. But if you favor one of the object-relational mapping frameworks then perhaps HibernateTemplate or JpaTemplate is more suitable. Direct wiring of the templates is fine, but Spring also provides a set of convenient DAO base classes that can manage the template for you.&lt;br /&gt;&lt;br /&gt;The data access templates are not all there is to Spring’s data access framework. Each template also provides convenience methods that simplify data access without the need to create an explicit callback implementation. Furthermore, on top of the template-callback design, Spring provides DAO support classes that are meant to be subclassed by your own DAO classes.&lt;br /&gt;&lt;br /&gt;When writing your application DAO implementation, you can subclass a DAO support class and call a template retrieval method to have direct access to the underlying data access template. For example, if your applicationDAO subclasses JdbcDaoSupport then you only need to call getJdbcTemplate() to get a JdbcTemplate to work with.&lt;br /&gt;&lt;br /&gt;Just as Spring provides several data access template implementations, it also provides several DAO support classes—one for each template.&lt;br /&gt;&lt;br /&gt;Spring’s persistence support options will depend on a data source. So, let us know how to configure Spring with a data source for the DAOs to access the database.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Configuring a data source&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Regardless of which form of Spring DAO support you use, you’ll likely need to configure a reference to a data source. Spring offers several options for configuring data source beans in your Spring application, including:&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;1.&lt;/span&gt; Data sources that are defined by a JDBC driver&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2.&lt;/span&gt; Data sources that are looked up by JNDI&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3.&lt;/span&gt; Data sources that pool connections&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;1. Let us look at using JNDI data sources :&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Spring applications will quite often be deployed to run within a JEE application server such as WebSphere, JBoss, or even a web container like Tomcat. These servers allow you to configure data sources to be retrieved via JNDI. The benefit of configuring data sources in this way is that they can be managed completely external to the application, leaving the application to simply ask for a data source when it’s ready to access the database. Moreover, data sources managed in an application server are often pooled for greater performance and can be hotswapped&lt;br /&gt;by system administrators. &lt;br /&gt;&lt;br /&gt;With Spring, we can configure a reference to a data source that is kept in JNDI and wire it into the classes that need it as if it were just another Spring bean. Spring’s JndiObjectFactoryBean makes it possible to retrieve any object, including data sources, from JNDI and make it available as a Spring bean.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;How does JndiObjectFactoryBean retrieve a data source from JNDI:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;dataSource&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.jndi.JndiObjectFactoryBean&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;scope&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;singleton&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;jndiName&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;/jdbc/SampleDatasource&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;resourceRef&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;true&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The jndiName attribute is used to specify the name of the resource in JNDI. If only the jndiName property is set then the data source will be looked up as is. But if the application is running within a Java application server then you’ll want to set the resourceRef property to true.&lt;br /&gt;&lt;br /&gt;When resourceRef is true, the value of jndiName will be prepended with java:comp/env/ to retrieve the data source as a Java resource from the application server’s JNDI directory. Consequently, the actual name used will be java:comp/env/jdbc/SampleDatasource.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;2 Using a pooled data source :&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Configuring a BasicDataSource bean is like&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;dataSource&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.apache.commons.dbcp.BasicDataSource&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;driverClassName&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;com.mysql.jdbc.Driver&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;url&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;jdbc:mysql://localhost/test&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;username&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;root&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;password&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;initialSize&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;5&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;maxActive&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;10&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The first four properties are elemental to configuring a BasicDataSource. The driverClassName property specifies the fully qualified name of the JDBC driver class. Here we’ve configured it with the JDBC driver for the Hypersonic database. The url property is where we set the complete JDBC URL for the database. Finally, the username and password properties are used to authenticate when you’re connecting to the database. Those four basic properties define connection information for BasicData- Source. In addition, several properties can be used to configure the data source pool itself.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;3. JDBC driver-based data source&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The simplest data source you can configure in Spring is one that is defined through a JDBC driver. Spring offers two such data source classes to choose from (both in the org.springframework.jdbc.datasource package):&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;-&gt;&lt;/span&gt; DriverManagerDataSource—Returns a new connection every time that a connection is requested. Unlike DBCP’s BasicDataSource, the connections&lt;br /&gt;provided by DriverManagerDataSource are not pooled.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;-&gt;&lt;/span&gt; SingleConnectionDataSource—Returns the same connection every time that a connection is requested. Although SingleConnectionDataSource&lt;br /&gt;isn’t exactly a pooled data source, you can think of it as a data source with a pool of exactly one connection.&lt;br /&gt;&lt;br /&gt;Configuring either of these data sources is similar to how we configured DBCP’s BasicDataSource as shown above. The only difference is that since neither DriverManagerDataSource nor Single-ConnectionDataSource provides a connection pool, there are no pool configuration&lt;br /&gt;properties to set.&lt;br /&gt;&lt;br /&gt;Although SingleConnectionDataSource and DriverManagerDataSource are great for small applications and running in development, you should seriously consider the implications of using either in a production application. Because SingleConnectionDataSource has one and only one database connection to work with, it doesn’t work well in a multithreaded application. At the same time, even though DriverManagerDataSource is capable of supporting multiple threads, it incurs a performance cost for creating a new connection each time a connection is requested. Because of these limitations, using pooled data sources strongly recommend.&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-820039533041760505?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/820039533041760505/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=820039533041760505' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/820039533041760505'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/820039533041760505'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/05/persistence-in-spring.html' title='Persistence in Spring'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-8304970535275809655</id><published>2009-05-06T08:52:00.000-07:00</published><updated>2009-05-26T11:52:03.272-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Integrating Hibernate with Spring'/><title type='text'>Integrating Hibernate with Spring</title><content type='html'>I tried to learn how to integrate Hibernate with Spring and found very useful information in Spring in Action by Craig Walls.&lt;br /&gt;&lt;br /&gt;It was very good book and I found it very informative.&lt;br /&gt;&lt;br /&gt;I would like to share few quotations from the book specific to this topic (Integrating Hibernate with Spring).&lt;br /&gt;&lt;br /&gt;Before getting started let me tell you about the example I have used in this article. &lt;br /&gt;&lt;br /&gt;&lt;em&gt;I am having a very basic table in database&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;CREATE&lt;/strong&gt; &lt;strong&gt;TABLE&lt;/strong&gt;  sample (&lt;br /&gt;  id int&lt;strong&gt;(&lt;/strong&gt;10) unsigned &lt;strong&gt;NOT&lt;/strong&gt; &lt;strong&gt;NULL&lt;/strong&gt; AUTO_INCREMENT,&lt;br /&gt;  name &lt;font color="#2040a0"&gt;&lt;strong&gt;varchar&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;(&lt;/strong&gt;45) &lt;strong&gt;NOT&lt;/strong&gt; &lt;strong&gt;NULL&lt;/strong&gt;,&lt;br /&gt;  city &lt;font color="#2040a0"&gt;&lt;strong&gt;varchar&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;(&lt;/strong&gt;45) &lt;strong&gt;NOT&lt;/strong&gt; &lt;strong&gt;NULL&lt;/strong&gt;,&lt;br /&gt;  &lt;strong&gt;PRIMARY&lt;/strong&gt; &lt;strong&gt;KEY&lt;/strong&gt; &lt;strong&gt;(&lt;/strong&gt;id)&lt;br /&gt;) ;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;em&gt;and here is Sample.hbm.xml&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="#2040a0"&gt;xml&lt;/font&gt; &lt;font color="#2040a0"&gt;version&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;1.0&amp;quot;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="4444FF"&gt;!&lt;/font&gt;&lt;font color="#2040a0"&gt;DOCTYPE&lt;/font&gt; &lt;font color="#2040a0"&gt;hibernate&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;mapping&lt;/font&gt; &lt;font color="#2040a0"&gt;PUBLIC&lt;/font&gt;&lt;br /&gt;        &lt;font color="#008000"&gt;&amp;quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&amp;quot;&lt;/font&gt;&lt;br /&gt;        &lt;font color="#008000"&gt;&amp;quot;http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;hibernate&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;mapping&lt;/font&gt; &lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernate.Sample&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;table&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;sample&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;lazy&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;false&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;id&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;column&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;java.lang.Long&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;            &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;generator&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;increment&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;name&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;string&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;city&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;type&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;string&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;        &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;hibernate&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;mapping&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;em&gt;and here is Sample bean.&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Sample&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;Serializable&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;Long&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;city&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;font color="#444444"&gt;// getters and setters&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Now let us get into detail... &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The standard way to get a reference to a Hibernate Session object is through an implementation of Hibernate’s SessionFactory interface. Among other things, SessionFactory is responsible for opening, closing, and managing Hibernate Sessions.&lt;br /&gt;&lt;br /&gt;The first thing you’ll need to do is to configure a Hibernate session factory bean in Spring.&lt;br /&gt;&lt;br /&gt;Spring’s &lt;strong&gt;HibernateTemplate&lt;/strong&gt; provides an abstract layer over a Hibernate Session. HibernateTemplate’s main responsibility is to simplify the work of opening and closing Hibernate Sessions and to convert Hibernate-specific exceptions to one of the Spring ORM exceptions (In the case of Hibernate 2, this means converting a checked HibernateException to an unchecked Spring exception.)&lt;br /&gt;&lt;br /&gt;The following XML shows how to configure a HibernateTemplate in Spring:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernateTemplate&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.orm.hibernate3.HibernateTemplate&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;sessionFactory&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;ref&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;sessionFactory&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The sessionFactory property takes a reference to an implementation of org.hibernate.SessionFactory. Here you have a few options, depending on&lt;br /&gt;how you use Hibernate to map your objects to database tables.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; If you are using Hibernate’s classic XML mapping files, you’ll want to use Spring’s LocalSessionFactoryBean. &lt;br /&gt;&lt;br /&gt;LocalSessionFactoryBean is a Spring factory bean that produces a local Hibernate SessionFactory instance that draws its mapping metadata from one or more XML mapping files.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;how to configure a LocalSessionFactory-Bean that loads the mapping files for the RoadRantz domain objects:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;sessionFactory&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;dataSource&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;ref&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;dataSource&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;mappingResources&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;list&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;hibernate&lt;/font&gt;/&lt;font color="#2040a0"&gt;Sample&lt;/font&gt;.&lt;font color="#2040a0"&gt;hbm&lt;/font&gt;.&lt;font color="#2040a0"&gt;xml&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;list&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernateProperties&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;props&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;prop&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernate.dialect&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;org&lt;/font&gt;.&lt;font color="#2040a0"&gt;hibernate&lt;/font&gt;.&lt;font color="#2040a0"&gt;dialect&lt;/font&gt;.&lt;font color="#2040a0"&gt;MySQLDialect&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;prop&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;prop&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;show_sql&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;prop&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;props&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; For annotation-based Hibernate, Spring’s AnnotationSessionFactoryBean works much like LocalSessionFactoryBean, except that it creates a SessionFactory based on annotations in one or more domain classes.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The XML required to configure an AnnotationSessionFactoryBean in Spring is similar to the XML for LocalSessionFactoryBean:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;sessionFactory&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;dataSource&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;ref&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;dataSource&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;annotatedClasses&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;list&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;hibernate&lt;/font&gt;.&lt;font color="#2040a0"&gt;Sample&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;list&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernateProperties&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;props&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;prop&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernate.dialect&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;org&lt;/font&gt;.&lt;font color="#2040a0"&gt;hibernate&lt;/font&gt;.&lt;font color="#2040a0"&gt;dialect&lt;/font&gt;.&lt;font color="#2040a0"&gt;MySQLDialect&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;prop&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;prop&lt;/font&gt; &lt;font color="#2040a0"&gt;key&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;show_sql&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;prop&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;props&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The dataSource and hibernateProperties properties serve the same purpose with AnnotationSessionFactoryBean as with LocalSessionFactoryBean. However, instead of configuring one or more mapping files, we must configure AnnotationSessionFactoryBean with one or more classes that are annotated for persistence with Hibernate.&lt;br /&gt;&lt;br /&gt;With the HibernateTemplate bean declared and wired with a session factory, we’re ready to start using it to persist and retrieve objects from the database.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Injected HibernateTemplate into HibernateSampleDao. &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;HibernateSampleDAO&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;HibernateTemplate&lt;/font&gt; &lt;font color="#2040a0"&gt;hibernateTemplate&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setHibernateTemplate&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;HibernateTemplate&lt;/font&gt; &lt;font color="#2040a0"&gt;hibernateTemplate&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;hibernateTemplate&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;hibernateTemplate&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;HibernateSampleDAO accepts a HibernateTemplate reference via setter injection. So configure it.&lt;br /&gt;&lt;br /&gt; &lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibSampleDAO&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernate.HibernateSampleDAO&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernateTemplate&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;ref&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernateTemplate&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;saveSample() method that is used to persist a Sample object to the database:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;saveSample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Sample&lt;/font&gt; &lt;font color="#2040a0"&gt;sample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;hibernateTemplate&lt;/font&gt;.&lt;font color="#2040a0"&gt;saveOrUpdate&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;sample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The saveOrUpdate() method inspects the object to see if its ID field is null. If it is, it must be a new object and thus it is inserted into the database. If it’s not null, it is assumed that it is an existing object and its data is updated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now let us look at findSampleByCity() method that uses HibernateTemplate’s find() method to retrieve a Sample by querying by the cityName.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;Sample&lt;/font&gt; &lt;font color="#2040a0"&gt;findSampleByCity&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;city&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Sample&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;results&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;hibernateTemplate&lt;/font&gt;.&lt;font color="#2040a0"&gt;find&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;from Sample sample&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;br /&gt;   &lt;font color="#008000"&gt;&amp;quot; where city = ?&amp;quot;&lt;/font&gt;,&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;city&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;results&lt;/font&gt;.&lt;font color="#2040a0"&gt;size&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#FF0000"&gt;0&lt;/font&gt; ? &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Sample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;results&lt;/font&gt;.&lt;font color="#2040a0"&gt;get&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;strong&gt;null&lt;/strong&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And here’s how you might use HibernateTemplate’s load() method to load a specific instance of a Sample by the sample’s ID field:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;Sample&lt;/font&gt; &lt;font color="#2040a0"&gt;getSampleById&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Long&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;return&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Sample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;hibernateTemplate&lt;/font&gt;.&lt;font color="#2040a0"&gt;load&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Sample&lt;/font&gt;.&lt;strong&gt;class&lt;/strong&gt;, &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So far, the configuration of HibernateSampleDAO involves four beans. The datasource is wired into the session factory bean (either LocalSessionFactoryBean or AnnotationSessionFactoryBean). The session factory bean is wired into the HibernateTemplate. Finally, the HibernateTemplate is wired into HibernateSampleDAO, where it is used to access the database.&lt;br /&gt;&lt;br /&gt;To simplify things slightly, Spring offers &lt;strong&gt;HibernateDaoSupport&lt;/strong&gt;, a convenience DAO support class, that enables you to wire a session factory bean directly into the DAO class. Under the covers, HibernateDaoSupport creates a HibernateTemplate that the DAO can use.&lt;br /&gt;&lt;br /&gt;Let’s rework HibernateSampleDAO to use HibernateDaoSupport. The first step is to change HibernateSampleDAO to extend HibernateDaoSupport:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;HibernateSampleDAO&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;HibernateDaoSupport&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;…&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;HibernateSampleDAO no longer needs a HibernateTemplate property as it did in previous example. Instead, you can use the getHibernateTemplate() method to get a HibernateTemplate that HibernateDaoSupport creates for you. So, the next step is to change all the data access methods in HibernateSampleDAO to use getHibernateTemplate().&lt;br /&gt;&lt;br /&gt;For example, here’s the saveSample() method updated for the new HibernateDaoSupport-based HibernateSampleDAO:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;saveSample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Sample&lt;/font&gt; &lt;font color="#2040a0"&gt;sample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;getHibernateTemplate&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;saveOrUpdate&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;sample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;HibernateSampleDAO no longer needs a HibernateTemplate reference, so you can remove it. Instead of a Hibernate-Template, you can directly give reference of a Hibernate SessionFactory so that it can produce a HibernateTemplate internally.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibSampleDAO&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernate.HibernateSampleDAO&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;sessionFactory&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;ref&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;sessionFactory&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If you think of extending Spring specific class as an intrusion of Spring into your application code, then you can take advantage of Hibernate 3’s support for contextual sessions to remove Spring-specific dependencies from your DAOs.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Using Hibernate 3 contextual sessions:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Contextual sessions, introduced in Hibernate3, are a way in which Hibernate itself manages one Session per transaction.&lt;br /&gt;&lt;br /&gt;There’s no need for HibernateTemplate to ensure this behavior. So, instead of wiring a HibernateTemplate into your DAO, you can wire a Hibernate Session-Factory.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;HibernateSampleDAO&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; …&lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;SessionFactory&lt;/font&gt; &lt;font color="#2040a0"&gt;sessionFactory&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setSessionFactory&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;SessionFactory&lt;/font&gt; &lt;font color="#2040a0"&gt;sessionFactory&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;sessionFactory&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;sessionFactory&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In this new HibernateSampleDAO, a SessionFactory reference is injected into the sessionFactory property. Since SessionFactory comes from the Hibernate API, HibernateSampleDAO no longer depends on the Spring Framework. Instead of using HibernateTemplate to perform persistence operations, you now ask the Session-Factory for the current session.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;saveSample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Sample&lt;/font&gt; &lt;font color="#2040a0"&gt;sample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;sessionFactory&lt;/font&gt;.&lt;font color="#2040a0"&gt;getCurrentSession&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;saveOrUpdate&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;sample&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Configuring this HibernateRantDao is similar to that of HibernateDaoSupport-based version.&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;Both HibernateDaoSupport and our new pure-Hibernate version of HibernateSampleDao require a Hibernate SessionFactory to be wired into their sessionFactory property. In either case, the sessionFactory bean (which is a SessionFactory-producing LocalSessionFactoryBean or AnnotationSession-FactoryBean) is suitable&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibSampleDAO&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hibernate.HibernateSampleDAO&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;sessionFactory&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;ref&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;sessionFactory&amp;quot;&lt;/font&gt; /&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note :&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; If you’re using Hibernate 2 then you have no other option than to use HibernateTemplate.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; The main benefit of Hibernate contextual sessions is that they decouple your DAO implementations from Spring.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; The primary drawback of contextual sessions is that they throw Hibernatespecific exceptions. Although HibernateException is a runtime exception, the exception hierarchy is specific to Hibernate and not as ORM-agnostic as Spring’s persistence exception hierarchy. This may hinder migration to a different ORM solution.&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-8304970535275809655?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/8304970535275809655/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=8304970535275809655' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/8304970535275809655'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/8304970535275809655'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/05/integrating-hibernate-with-spring.html' title='Integrating Hibernate with Spring'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-7668607932697354825</id><published>2009-04-16T13:07:00.000-07:00</published><updated>2009-04-16T13:18:15.983-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ApplicationContext in Spring'/><title type='text'>More about ApplicationContext in Spring</title><content type='html'>While the beans package provides basic functionality for managing and manipulating beans, often in a programmatic way, the context package adds ApplicationContext, which enhances BeanFactory functionality in a more framework-oriented style. &lt;br /&gt;&lt;br /&gt;A bean factory is fine for simple applications, but to take advantage of the full power of the Spring Framework, you’ll probably want to load your application beans using Spring’s more advanced container, the application context.&lt;br /&gt;&lt;br /&gt;Many users will use ApplicationContext in a completely declarative fashion, not even having to create it manually, but instead relying on support classes such as ContextLoader to automatically start an ApplicationContext as part of the normal startup process of a J2EE web-app. Of course, it is still possible to programmatically create an ApplicationContext.&lt;br /&gt;&lt;br /&gt;The basis for the context package is the ApplicationContext interface, located in the org.springframework.context package. Deriving from the BeanFactory interface, it provides all the functionality of BeanFactory. To allow working in a more framework-oriented fashion, using layering and hierarchical contexts, the context package also provides the following:&lt;br /&gt;&lt;br /&gt;In most cases, you’ll use the ApplicationContext, which adds more enterprise-level, J2EE functionality, such as &lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; internationalization (i18n)&lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; custom converters (for converting Strings to Object types)&lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; event publication/notification&lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; Access to resources, such as URLs and files&lt;br /&gt;&lt;strong&gt;-&gt;&lt;/strong&gt; Loading of multiple (hierarchical) contexts, allowing each to be focused on one particular layer, for example the web layer of an application.&lt;br /&gt;&lt;br /&gt;You could also implement your own ApplicationContext and add support for loading from other resources (such as a database). While many Contexts are available for loading beans, you’ll only need a few, which are listed below. The others are internal classes that are used by the framework itself.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;1. ClassPathXmlApplicationContext&lt;/strong&gt;: Loads context files from the classpath (that is, WEB-INF/classes or WEB-INF/lib for JARs) in a web application. Initializes using a &lt;pre&gt;&lt;br /&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ClassPathXmlApplicationContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt; where path is the path to the file. The path argument can also be a String array of paths. This is a good context for using in unit tests.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2. FileSystemXmlApplicationContext:&lt;/strong&gt; Loads context files from the file system, which is nice for testing. Initializes using a &lt;pre&gt;&lt;br /&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;FileSystemXmlApplicationContext&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;path&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt; where path is a relative or absolute path to the file. The path argument can also be a String array of paths.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;3. XmlWebApplicationContext:&lt;/strong&gt; Loads context files internally by the ContextLoaderListener, but can be used outside of it. For instance, if you are running a container that doesn’t load Listeners in the order specified in web.xml, you may have to use this in another Listener. Below is the code to use this Loader.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;XmlWebApplicationContext&lt;/font&gt; &lt;font color="#2040a0"&gt;context&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;XmlWebApplicationContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;context&lt;/font&gt;.&lt;font color="#2040a0"&gt;setServletContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ctx&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;context&lt;/font&gt;.&lt;font color="#2040a0"&gt;refresh&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Once you’ve obtained a reference to a context, you can get references to beans using &lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;ctx&lt;/font&gt;.&lt;font color="#2040a0"&gt;getBean&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;“&lt;font color="#2040a0"&gt;beanId&lt;/font&gt;”&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt; You will need to cast it to a specific type, but that’s the easy part. Of the above contexts, ClassPathXmlApplicationContext is the most flexible. It doesn’t care where the files are, as long as they’re in the classpath. This allows you to move files around and simply change the classpath.&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;Aside from the additional functionality offered by application contexts, another big difference between an application context and a bean factory is how singleton beans are loaded. A bean factory lazily loads all beans, deferring bean creation until the getBean() method is called. An application context is a bit smarter and preloads all singleton beans upon context startup. By preloading singleton beans, you ensure that they will be ready to use when needed—your application won’t have to wait for them to be created.&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-7668607932697354825?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/7668607932697354825/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=7668607932697354825' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/7668607932697354825'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/7668607932697354825'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/04/more-about-applicationcontext-in-spring.html' title='More about ApplicationContext in Spring'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-6443378907595858521</id><published>2009-04-16T04:01:00.000-07:00</published><updated>2009-04-16T04:16:32.864-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='BeanFactory in Spring'/><title type='text'>More about BeanFactory in Spring</title><content type='html'>As its name implies, a bean factory is an implementation of the Factory design pattern. That is, it is a class whose responsibility is to create and dispense beans. The BeanFactory is the actual container which instantiates, configures, and manages a number of beans. These beans typically collaborate with one another, and thus have dependencies between themselves. When a bean factory hands out objects, those objects are fully configured, are aware of their collaborating objects, and are ready to use.&lt;br /&gt;&lt;br /&gt;BeanFactory is a workhorse that initializes beans and calls their lifecycle methods. It should be noted that most lifecycle methods only apply to singleton beans. Spring cannot manage prototype (non-singleton) lifecycles. This is because, after they’re created, prototypes are handed off to the client and the container loses track of it. For prototypes, Spring is really just a replacement for the “new” operator.&lt;br /&gt;&lt;br /&gt;A BeanFactory is represented by the interface org.springframework.beans.factory.BeanFactory, and it is having multiple implementations. The most commonly used simple BeanFactory implementation is org.springframework.beans.factory.xml.XmlBeanFactory. (This should be qualified with the reminder that ApplicationContexts are a subclass of BeanFactory, and most users end up using XML variants of ApplicationContext).&lt;br /&gt;&lt;br /&gt;Although for most scenarios, almost all user code managed by the BeanFactory does not have to be aware of the BeanFactory, the BeanFactory does have to be instantiated somehow. This can happen via explicit user code such as:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Resource&lt;/font&gt; &lt;font color="#2040a0"&gt;res&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;FileSystemResource&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;beans.xml&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;XmlBeanFactory&lt;/font&gt; &lt;font color="#2040a0"&gt;factory&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;XmlBeanFactory&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;res&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;ClassPathResource&lt;/font&gt; &lt;font color="#2040a0"&gt;res&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ClassPathResource&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;beans.xml&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;XmlBeanFactory&lt;/font&gt; &lt;font color="#2040a0"&gt;factory&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;XmlBeanFactory&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;res&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;ClassPathXmlApplicationContext&lt;/font&gt; &lt;font color="#2040a0"&gt;appContext&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ClassPathXmlApplicationContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;        &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;applicationContext.xml&amp;quot;&lt;/font&gt;, &lt;font color="#008000"&gt;&amp;quot;applicationContext-part2.xml&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#444444"&gt;// of course, an ApplicationContext is just a BeanFactory&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;BeanFactory&lt;/font&gt; &lt;font color="#2040a0"&gt;factory&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;BeanFactory&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;appContext&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Beans are lazily loaded into bean factories, meaning that while the bean factory will immediately load the bean definitions (the description of beans and their properties), the beans themselves will not be instantiated until they are needed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;em&gt;BeanDefinition includes &lt;/em&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;1.&lt;/strong&gt; class name which is normally the actual implementation class of the bean being described in the bean definition. However, if the bean is to be constructed by calling a static factory method instead of using a normal constructor, this will actually be the class name of the factory class.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2.&lt;/strong&gt; bean behavioral configuration elements, which state how the bean should behave in the container (i.e. prototype or singleton, autowiring mode, dependency checking mode, initialization and destruction methods)&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;3.&lt;/strong&gt; constructor arguments and property values to set in the newly created bean. An example would be the number of connections to use in a bean that manages a connection pool (either specified as a property or as a constructor argument), or the pool size limit.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;4.&lt;/strong&gt; other beans a bean needs to do its work, i.e. collaborators (also specified as properties or as constructor arguments). These can also be called dependencies.&lt;br /&gt;&lt;br /&gt;In the much more common case where the BeanFactory itself directly creates the bean by calling its constructor (equivalent to Java code calling new), the class attribute specifies the class of the bean to be constructed. In the less common case where the BeanFactory calls a static factory method on a class to create the bean, the class attribute specifies the actual class containing the static factory method. (the type of the returned bean from the static factory method may be the same class or another class entirely, it doesn't matter).&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;1. Bean creation via constructor&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;When creating a bean using the constructor approach, all normal classes are usable by Spring and compatible with Spring. That is, the class being created does not need to implement any specific interfaces or be coded in a specific fashion. Just specifying the bean class should be enough. However, depending on what type of IoC you are going to use for that specific bean, you may need a default (empty) constructor.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note :&lt;/strong&gt; Additionally, the BeanFactory isn't limited to just managing true JavaBeans, it is also able to manage virtually any class you want it to manage. Most people using Spring prefer to have actual JavaBeans (having just a default (no-argument) constructor and appropriate setters and getters modeled after the properties) in the BeanFactory, but it it's also possible to have more exotic non-bean-style classes in your BeanFactory. If, for example, you need to use a legacy connection pool that absolutely does not adhere to the JavaBean specification, no worries, Spring can manage it as well.&lt;br /&gt;&lt;br /&gt;Using the XmlBeanFactory you can specify your bean class as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;myBean&amp;quot;&lt;/font&gt;&lt;br /&gt;      &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;spring.MyBean&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2. Bean creation via static factory method&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;When defining a bean which is to be created using a static factory method, along with the class attribute which specifies the class containing the static factory method, another attribute named factory-method is needed to specify the name of the factory method itself. Spring expects to be able to call this method and get back a live object, which from that point on is treated as if it had been created normally via a constructor. One use for such a bean definition is to call static factories in legacy code.&lt;br /&gt;&lt;br /&gt;Following is an example of a bean definition which specifies that the bean is to be created by calling a factory-method. Note that the definition does not specify the type (class) of the returned object, only the class containing the factory method. In this example, createInstance must be a static method.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;myBean&amp;quot;&lt;/font&gt;&lt;br /&gt;      &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;spring.MyBean&amp;quot;&lt;/font&gt;&lt;br /&gt;      &lt;font color="#2040a0"&gt;factory&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;method&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;createInstance&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;3. Bean creation via instance factory method&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;In a fashion similar to instantiation via a static factory method, instantiation using an instance factory method is where a non-static method of an existing bean from the container is invoked to create a new bean. To use this mechanism, the 'class' attribute must be left empty, and the 'factory-bean' attribute must specify the name of a bean in the current (or parent/ancestor) container that contains the instance method that is to be invoked to create the object. The name of the factory method itself must be set using the 'factory-method' attribute.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="4444FF"&gt;!&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt; &lt;font color="#2040a0"&gt;the&lt;/font&gt; &lt;font color="#2040a0"&gt;factory&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt;, &lt;font color="#2040a0"&gt;which&lt;/font&gt; &lt;font color="#2040a0"&gt;contains&lt;/font&gt; &lt;font color="#2040a0"&gt;a&lt;/font&gt; &lt;font color="#2040a0"&gt;method&lt;/font&gt; &lt;font color="#2040a0"&gt;called&lt;/font&gt; &lt;font color="#2040a0"&gt;createInstance&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;serviceLocator&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;com.foo.DefaultServiceLocator&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="4444FF"&gt;!&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt; &lt;font color="#2040a0"&gt;inject&lt;/font&gt; &lt;font color="#2040a0"&gt;any&lt;/font&gt; &lt;font color="#2040a0"&gt;dependencies&lt;/font&gt; &lt;font color="#2040a0"&gt;required&lt;/font&gt; &lt;font color="#2040a0"&gt;by&lt;/font&gt; &lt;strong&gt;this&lt;/strong&gt; &lt;font color="#2040a0"&gt;locator&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="4444FF"&gt;!&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt; &lt;font color="#2040a0"&gt;the&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;to&lt;/font&gt; &lt;font color="#2040a0"&gt;be&lt;/font&gt; &lt;font color="#2040a0"&gt;created&lt;/font&gt; &lt;font color="#2040a0"&gt;via&lt;/font&gt; &lt;font color="#2040a0"&gt;the&lt;/font&gt; &lt;font color="#2040a0"&gt;factory&lt;/font&gt; &lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;exampleBean&amp;quot;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;factory&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;serviceLocator&amp;quot;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;factory&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;method&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;createService&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;To singleton or not to singleton&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Beans are defined to be deployed in one of two modes: singleton or non-singleton (prototype). When a bean is a singleton, only one shared instance of the bean will be managed and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned.&lt;br /&gt;&lt;br /&gt;The non-singleton, prototype mode of a bean deployment results in the creation of a new bean instance every time a request for that specific bean is done. This is ideal for situations where for example each user needs an independent user object or something similar.&lt;br /&gt;&lt;br /&gt;Beans are deployed in singleton mode by default, unless you specify otherwise. Keep in mind that by changing the type to non-singleton (prototype), each request for a bean will result in a newly created bean and this might not be what you actually want. So only change the mode to prototype when absolutely necessary.&lt;br /&gt;&lt;br /&gt;In the example below, two beans are declared of which one is defined as a singleton, and the other one is a non-singleton (prototype). myBean is created each and every time a client asks the BeanFactory for this bean, while myBeanAgain is only created once; a reference to the exact same instance is returned on each request for this bean.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;myBean&amp;quot;&lt;/font&gt;&lt;br /&gt;      &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;spring.MyBean&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;singleton&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;false&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;myBeanAgain&amp;quot;&lt;/font&gt;&lt;br /&gt;      &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;spring.MyBeanAgain&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;singleton&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;true&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A BeanFactory is essentially nothing more than the interface for an advanced factory capable of maintaining a registry of different beans and their dependencies. The BeanFactory enables you to read bean definitions and access them using the bean factory. When using just the BeanFactory you would create one and read in some bean definitions in the XML format as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;InputStream&lt;/font&gt; &lt;font color="#2040a0"&gt;is&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;FileInputStream&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;beans.xml&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;XmlBeanFactory&lt;/font&gt; &lt;font color="#2040a0"&gt;factory&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;XmlBeanFactory&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;is&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The BeanFactory interface has only five methods for clients to call:&lt;br /&gt;&lt;br /&gt;boolean containsBean(String): returns true if the BeanFactory contains a bean definition or bean instance that matches the given name&lt;br /&gt;&lt;br /&gt;Object getBean(String): returns an instance of the bean registered under the given name. Depending on how the bean was configured by the BeanFactory configuration, either a singleton and thus shared instance or a newly created bean will be returned. A BeansException will be thrown when either the bean could not be found (in which case it'll be a NoSuchBeanDefinitionException), or an exception occurred while instantiating and preparing the bean&lt;br /&gt;&lt;br /&gt;Object getBean(String,Class): returns a bean, registered under the given name. The bean returned will be cast to the given Class. If the bean could not be cast, corresponding exceptions will be thrown (BeanNotOfRequiredTypeException). Furthermore, all rules of the getBean(String) method apply (see above)&lt;br /&gt;&lt;br /&gt;boolean isSingleton(String): determines whether or not the bean definition or bean instance registered under the given name is a singleton or a prototype. If no bean corresponding to the given name could not be found, an exception will be thrown (NoSuchBeanDefinitionException)&lt;br /&gt;&lt;br /&gt;String[] getAliases(String): Return the aliases for the given bean name, if any were defined in the bean definition&lt;br /&gt;&lt;br /&gt;Hope it will be useful.....&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-6443378907595858521?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/6443378907595858521/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=6443378907595858521' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/6443378907595858521'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/6443378907595858521'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/04/more-about-beanfactory-in-spring_16.html' title='More about BeanFactory in Spring'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-8690548595320641407</id><published>2009-04-14T13:25:00.001-07:00</published><updated>2009-04-14T13:26:47.518-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='BeanFactory and ApplicationContext in Spring'/><title type='text'>BeanFactory and ApplicationContext in Spring</title><content type='html'>Basis for Spring's Dependency Injection feature is provided by two most fundamental and important packages in Spring namely org.springframework.beans and org.springframework.context packages. &lt;br /&gt;&lt;br /&gt;The BeanFactory from org.springframework.beans provides an advanced configuration mechanism capable of managing beans (objects) of any nature, using potentially any kind of storage facility. &lt;br /&gt;&lt;br /&gt;The ApplicationContext from org.springframework.context package builds on top of the BeanFactory (it's a subclass) and adds other functionality such as easier integration with Springs AOP features, message resource handling (for use in internationalization), event propagation, declarative mechanisms to create the ApplicationContext and optional parent contexts, and application-layer specific contexts such as the WebApplicationContext, among other enhancements.&lt;br /&gt;&lt;br /&gt;BeanFactory is an internal interface that provides the configuration framework and basic functionality, while the ApplicationContext adds enhanced capabilities to it, some of them perhaps more J2EE and enterprise-centric. &lt;br /&gt;&lt;br /&gt;ApplicationContext is a complete superset of a BeanFactory, and any description of BeanFactory capabilities and behavior should be considered to apply to ApplicationContexts as well.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;How to determine which one is  best suited in a particular situation. BeanFactory or ApplicationContext???&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Normally when building most applications in a J2EE-environment, the best option is to use the application context for every application unless the resources of this application are restricted, such as when running in an applet or a mobile device, and you don't need all the features of the ApplicationContext.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-8690548595320641407?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/8690548595320641407/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=8690548595320641407' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/8690548595320641407'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/8690548595320641407'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/04/beanfactory-and-applicationcontext-in.html' title='BeanFactory and ApplicationContext in Spring'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-5746073669579587527</id><published>2009-04-12T12:55:00.000-07:00</published><updated>2009-04-12T13:05:56.476-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Dependency Injection in Spring'/><category scheme='http://www.blogger.com/atom/ns#' term='Dependency Injection'/><title type='text'>Dependency Injection in Spring</title><content type='html'>&lt;strong&gt;What is Dependency Injection:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Originally, dependency injection was commonly referred to by another name inversion of control(IoC). But in an article written in early 2004, &lt;br /&gt;&lt;a href="http://martinfowler.com/articles/injection.html" target="_blank"&gt; Martin Fowler &lt;/a&gt; asked what aspect of control is being inverted. He concluded that it is the acquisition of dependencies that is being inverted. &lt;br /&gt;&lt;br /&gt;In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application.(Ex: Spring) Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name of "Inversion of Control"&lt;br /&gt;&lt;br /&gt;For this new breed of containers the inversion is about how they lookup a plugin implementation.  The approach that these containers use is to ensure that any user of a plugin follows some convention that allows a separate assembler module to inject the implementation into the lister.&lt;br /&gt;&lt;br /&gt;Based on that revelation, he coined the phrase “dependency injection,” a term that better describes what is going on. Inversion of Control is a generic term and the more specific name for this pattern is Dependency Injection.&lt;br /&gt;&lt;br /&gt;Dependency injection is basically giving an object what it needs instead of letting this object get it by itself. It is based on a best practice technique (design pattern). This technique allows a part of application to be changed without causing problems in other areas of the application. A design is inflexible unless it cannot be easily adapted.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Why use Dependency Injection:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Instead of using Depenedency Injection if we hardcode "new" in our code.&lt;br /&gt;What if something changes?&lt;br /&gt;How do we externalize configuration from Java code, important if things change?&lt;br /&gt;&lt;br /&gt;By avoiding use of a custom factory we are reducing extra code to be written in the application&lt;br /&gt;&lt;br /&gt;By applying Dependency Injection in your projects, you’ll find that your code will become significantly simpler, easier to understand, and easier to test.&lt;br /&gt;&lt;br /&gt;Any nontrivial application is made up of two or more classes that collaborate with each other to perform some business logic. Traditionally,&lt;br /&gt;each object is responsible for obtaining its own references to the objects it collaborates with (its dependencies). This can lead to highly coupled and hard-to-test code. When applying Dependency injection(DI), objects are given their dependencies at creation time by some external entity that coordinates each object in the system. In other words, dependencies are injected into objects. So, DI means an inversion of responsibility with regard to how an object obtains references to collaborating objects.&lt;br /&gt;&lt;br /&gt;Software components (Clients), are often a part of a set of collaborating components which depend upon other components (Services) to successfully complete their intended purpose. In many scenarios, they need to know “which” components to communicate with, “where” to locate them, and “how” to communicate with them. When the way such services can be accessed is changed, such changes can potentially require the source of lot of clients to be changed.&lt;br /&gt;&lt;br /&gt;One way of structuring the code is to let the clients embed the logic of locating and/or instantiating the services as a part of their usual logic. Another way to structure the code is to have the clients declare their dependency on services, and have some "external" piece of code assume the responsibility of locating and/or instantiating the services and simply supplying the relevant service references to the clients when needed. In the latter method, client code typically is not required to be changed when the way to locate an external dependency changes. This type of implementation is considered to be an implementation of Dependency Injection and the "external" piece of code referred to earlier is likely to be either hand coded or implemented using one of a variety of DI frameworks.&lt;br /&gt;&lt;br /&gt;Dependency Injection uses the Hollywood Principle: “Don’t call me, I’ll call you.” In other words, your classes don’t look up or instantiate the classes they depend on. The control is inverted and some form of container sets the dependencies. Using IoC often leads to much cleaner code and provides an excellent way to de-couple dependent classes.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;IOC in Spring:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;IoC is a big part of what Spring does. It also offers a number of other capabilities apart from Dependency Injection(DI). &lt;br /&gt;&lt;br /&gt;The core of Spring’s implementation is based on DI, although dependency lookup features are provided as well. When Spring automatically provides collaborators to a dependent object, it does so using DI. In a Spring-based application, it is always preferable to use dependency injection to pass collaborators to dependent objects rather than have the dependent objects obtain the collaborators via lookup. Although DI is the preferred mechanism for wiring together collaborators and dependent objects, you need dependency&lt;br /&gt;lookup to access the dependent objects. In many environments, Spring cannot automatically wire up all of your application components using DI, and you must use dependency lookup to access the initial set of components. When you are building web applications using Spring’s MVC&lt;br /&gt;support, Spring can avoid this by gluing your entire application together automatically. Wherever possible, you should use DI with Spring; otherwise, you can fall back on the dependency lookup capabilities. &lt;br /&gt;&lt;br /&gt;Dependency Injection(DI) is provided by Core package in Spring Framework allowing you to manage bean container functionality. The basic principle is that beans define their dependencies (i.e. the other objects they work with) only through constructor arguments, arguments to a factory method, or properties which are set on the object instance after it has been constructed or returned from a factory method. Then, it is the job of the container to actually inject those dependencies when it creates the bean. This is fundamentally the inverse (hence the name Inversion of Control) of the bean instantiating or locating its dependencies on its own using direct construction of classes, or something like the Service Locator pattern.&lt;br /&gt;&lt;br /&gt;An interesting feature of Spring’s IoC container is that it has the ability to act as an adaptor between its own dependency injection container and external dependency lookup containers. &lt;br /&gt;&lt;br /&gt;Spring supports both constructor and setter injection and bolsters the standard IoC feature set with a whole host of useful additions to make your life easier.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Dependency Injection majorly in two forms:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;1. Setter-based:&lt;/strong&gt; Classes are typically JavaBeans, with a no-arg constructor, and with setters for the IoC container to use when wiring dependencies. This is the variant recommended by Spring. While Spring supports constructor-based injection, a large number of constructor arguments can be difficult to manage.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;Hello&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;sayHello&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;str&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;showEmpDetails&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;HelloImpl&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;Hello&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;greeting&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;EmployeeTO&lt;/font&gt; &lt;font color="#2040a0"&gt;empTO&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;sayHello&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;str&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;greeting&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot; &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;str&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;showEmpDetails&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#008000"&gt;&amp;quot;Employee &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;empTO&lt;/font&gt;.&lt;font color="#2040a0"&gt;getName&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot; with empId = &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;empTO&lt;/font&gt;.&lt;font color="#2040a0"&gt;getId&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;getGreeting&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;greeting&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setGreeting&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;greeting&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Inside setGreeting&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;greeting&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;greeting&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setEmpTO&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;EmployeeTO&lt;/font&gt; &lt;font color="#2040a0"&gt;empTO&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Inside setEmpTO&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;empTO&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;empTO&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="4444FF"&gt;!&lt;/font&gt;&lt;font color="#2040a0"&gt;DOCTYPE&lt;/font&gt; &lt;font color="#2040a0"&gt;beans&lt;/font&gt; &lt;font color="#2040a0"&gt;PUBLIC&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;-//SPRING//DTD BEAN//EN&amp;quot;&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;http://www.springframework.org/dtd/spring-beans.dtd&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hello&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;bean_injection.HelloImpl&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;greeting&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Good&lt;/font&gt; &lt;font color="#2040a0"&gt;Night&lt;/font&gt;&lt;font color="4444FF"&gt;!&lt;/font&gt;...&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;empTO&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;ref&lt;/font&gt; &lt;font color="#2040a0"&gt;local&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeTO&amp;quot;&lt;/font&gt;/&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;employeeTO&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;bean_injection.EmployeeTO&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;           &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;id&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;1&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;property&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;name&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;        &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Prashant&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;    &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;property&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2. Constructor-based:&lt;/strong&gt; Classes contain constructors with a number of arguments. The IoC container discovers and invokes the constructor based on the number of arguments and their object types. This approach guarantees that a bean is not created in an invalid state.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;Hello&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;sayHello&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;str&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;HelloImpl&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;Hello&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;greeting&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;HelloImpl&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;HelloImpl&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;str&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Inside constructor &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;greeting&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;str&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;sayHello&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;str&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;greeting&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot; &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;str&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;bean&lt;/font&gt; &lt;font color="#2040a0"&gt;id&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;hello&amp;quot;&lt;/font&gt; &lt;strong&gt;class&lt;/strong&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;bean_injection.HelloImpl&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;     &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;constructor&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;arg&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Good&lt;/font&gt; &lt;font color="#2040a0"&gt;Morning&lt;/font&gt;&lt;font color="4444FF"&gt;!&lt;/font&gt;...&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;value&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;br /&gt;  &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;constructor&lt;/font&gt;&lt;font color="4444FF"&gt;-&lt;/font&gt;&lt;font color="#2040a0"&gt;arg&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;bean&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;/&lt;font color="#2040a0"&gt;beans&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Which one to use (Setter-based / Constructor-based)?&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Constructor injection is particularly useful when you absolutely must have an instance of the dependency class before your component is used. Many containers, Spring included, provide a mechanism for ensuring that all dependencies are defined when you use setter injection, but by using constructor injection, you assert the requirement for the dependency in a container-agnostic manner.&lt;br /&gt;&lt;br /&gt;Setter injection is useful in a variety of different cases. If you want the component to expose its dependencies to the container but provide its own defaults, setter injection is usually the best way to accomplish this. Another benefit of setter injection is that it allows dependencies to be declared on an interface, although this is not as useful as you might first think. Consider a typical business&lt;br /&gt;interface with one business method, defineMeaningOfLife(). If, in addition to this method, you define a setter for injection such as setEncylopedia(), you are mandating that all implementations must use or at least be aware of the encyclopedia dependency. You do not need to define this setter at all—any decent IoC container, Spring included, can work with the component in terms of the&lt;br /&gt;business interface but still provide the dependencies of the implementing class. &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Benefits of Dependency Injection : &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;1.&lt;/strong&gt; Unit testable&lt;br /&gt;&lt;strong&gt;2.&lt;/strong&gt; Dependencies are explicit&lt;br /&gt;&lt;strong&gt;3.&lt;/strong&gt; Consistent&lt;br /&gt;&lt;strong&gt;4.&lt;/strong&gt; Can wire up arbitrarily complicated graphs&lt;br /&gt;&lt;strong&gt;5.&lt;/strong&gt; You don’t need to write plumbing code&lt;br /&gt;&lt;strong&gt;6.&lt;/strong&gt; Pluggability&lt;br /&gt;&lt;strong&gt;7.&lt;/strong&gt; Reduces cost of programming to interfaces to zero&lt;br /&gt;&lt;br /&gt;As described by Rod Johnson "&lt;strong&gt;Spring provides a factory to end all factories&lt;/strong&gt;".&lt;br /&gt;&lt;br /&gt;Hope it will be useful.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-5746073669579587527?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/5746073669579587527/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=5746073669579587527' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/5746073669579587527'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/5746073669579587527'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/04/dependency-injection-in-spring.html' title='Dependency Injection in Spring'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-5848069955526308177</id><published>2009-02-06T09:44:00.000-08:00</published><updated>2009-02-06T09:54:18.004-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='arrays in javascript'/><title type='text'>Advanced JavaScript by Chuck Easttom</title><content type='html'>Few days before I started reading &lt;a href ="http://www.amazon.com/Advanced-Javascript-Chuck-Easttom/dp/155622852X"&gt; Advanced JavaScript Third Edition &lt;/a&gt; by Chuck Easttom.&lt;br /&gt;&lt;br /&gt;It was very good book and I found the arrays chapter to be very interesting...&lt;br /&gt;&lt;br /&gt;I would like to share few quotations from the book.&lt;br /&gt;&lt;br /&gt;JavaScript is an easy-to-use object-oriented scripting language designed for creating dynamic online applications that link together objects and resources on both clients and servers. While Java is used by programmers to create new objects and applets, JavaScript is designed for use by HTML page authors and enterprise application developers to dynamically script the behavior of objects running on either the client or the server. &lt;br /&gt;&lt;br /&gt;JavaScript’s design and concepts represent the next generation of software for the Internet. JavaScript is:&lt;br /&gt;&lt;br /&gt;1. Designed for creating network-centric applications&lt;br /&gt;2. Complementary to and integrated with Java&lt;br /&gt;3. Complementary to and integrated with HTML&lt;br /&gt;4. Open and cross-platform&lt;br /&gt;&lt;br /&gt;This means that JavaScript works hand in hand with HTML, and will work on any platform (Windows, Linux, Mac, etc.). It is also important to note that you don’t need any special tools to use JavaScript. You can simply write scripts in any text editor, as we will do throughout most of this book.&lt;br /&gt;&lt;br /&gt;With JavaScript, an HTML page might contain a form that processes data on the client side. A server-side JavaScript might pull data out of a relational database and format it in HTML on the fly. A page might contain JavaScript scripts that run on both the client and the server.&lt;br /&gt;&lt;br /&gt;JavaScript supports most of Java’s expression syntax and basic control flow constructs. The only difference is the variable declaration.&lt;br /&gt;&lt;br /&gt;JavaScript and Java are both based on objects, but their implementations differ. In both languages, many built-in functions are implemented as properties and methods of various objects.&lt;br /&gt;&lt;br /&gt;JavaScript resembles Perl in that it is interpreted, not compiled. Java is referred to as a compiled language. Unlike most other programming languages, though, Java is not compiled to a native machine code, but rather to a Java byte code.&lt;br /&gt;&lt;br /&gt;Both Java and JavaScript are based on objects. However, their implementations of objects are different. JavaScript is an object-based language. It supports built-in, extensible objects, but no classes or inheritance. Its object hierarchy is not an inheritance hierarchy as in Java. JavaScript features two types of objects:&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Static objects&lt;/strong&gt; — objects that combine general functions (methods) and data constructs (properties). The values of such objects’ properties are usually read-only. A static object is a single object, and thus does not enable you to create instances of it. For example, the Math object in JavaScript is a static one, because you cannot create instances according to its template.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Dynamic objects&lt;/strong&gt; — objects by which instances are created. A dynamic object resembles a template. You do not touch or use the object directly. In order to take advantage of such an object, you must create an instance of it.&lt;br /&gt;&lt;br /&gt;JavaScript’s object model is somewhat simpler than Java’s equivalent, so JavaScript’s object implementation is much easier to accommodate than Java’s model. &lt;br /&gt;&lt;br /&gt;Another difference between Java and JavaScript is their time of binding. JavaScript features dynamic binding, so all object references are checked at run time. Java, on the other hand, is based on static binding, meaning that all object references must exist and be valid at compile time.&lt;br /&gt;&lt;br /&gt;Another difference between Java and JavaScript is that Java is strongly typed, as opposed to JavaScript, which is loosely typed. That is, when you declare a variable in Java, you must specify its data type. For example, when you create a variable to contain integer values you must use the int keyword. In JavaScript, all variables are declared in the same way.&lt;br /&gt;&lt;br /&gt;A very important difference between Java and JavaScript is that JavaScript is integrated with, and embedded in, HTML.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Internal Scripts:&lt;/strong&gt;&lt;br /&gt;The &lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;SCRIPT&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt; tag is used to enclose JavaScript code in HTML documents.&lt;br /&gt;This is probably the most common method for inserting JavaScript into HTML documents.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;External Scripts:&lt;/strong&gt;&lt;br /&gt;Using &lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;SCRIPT&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt; tag, which enables the use of external scripts; that is, you can use a JavaScript script that is located in another file.&lt;br /&gt;Some people prefer to have their scripts in external files for a few reasons. First, your script is not as immediately and easily viewable as your HTML.&lt;br /&gt;Also, some web developers have common scripts they use on multiple pages.&lt;br /&gt;External scripts are useful when you need to integrate a long, sophisticated JavaScript script into an HTML file.&lt;br /&gt;&lt;br /&gt;But we have some disadvantages too..&lt;br /&gt;No back references: JavaScript scripts are mainly driven by user-initiated events. A click on a button calls a function, a selection of a form element executes another function, and so on. If you design your entire script in an external file, you will have difficulties in referring to the HTML definitions in the original file. For this reason, place only general functions in an external script.&lt;br /&gt;&lt;br /&gt;Additional processing: The JavaScript interpreter evaluates all functions found in your script header, including those defined in the external file, and stores them in memory. Loading unneeded functions in an external file degrades the script performance.&lt;br /&gt;&lt;br /&gt;Additional server access: You know how irritating it is to wait until another page loads in the window, especially with slow connections. That is the problem with the SRC attribute. When the browser comes across the tag that tells it to interpret JavaScript code in another file, it must first load that file. Such an action is time consuming. Most programmers say that efficiency is not as important as maintainability and clarity. However, with web page development, appearance is everything and a slow-loading web site is unlikely to be successful. Always keep in&lt;br /&gt;mind that unnecessary HTTP hits to the server should be avoided.&lt;br /&gt;&lt;br /&gt;JavaScript-only files should have the filename suffix .js, and the server must map the .js suffix to the MIME type application/x-javascript, which it sends back in the HTTP header. If the server does not map the .js filename extension to application/x-javascript, Navigator will not properly load the JavaScript file specified by the SRC attribute. In short, your server must be configured to reflect the proper MIME type; otherwise, the browser does not respond properly with the data coming back in the HTTP response to the SRC-initiated request.&lt;br /&gt;&lt;br /&gt;There are four specific data types in JavaScript: numbers, strings, Booleans, and null values.As opposed to other languages, a variable data type is not declared explicitly but rather implicitly according to its initial value assignment. Also unique to JavaScript, there is no explicit distinction between integers and real-valued numbers.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The Value Null&lt;/strong&gt;&lt;br /&gt;The value null is often used to initialize variables that do not have any special meaning. You assign it to a variable using the standard assignment statement:&lt;br /&gt;&lt;br /&gt;var name = null&lt;br /&gt;&lt;br /&gt;The null value is special in that it is automatically converted to initial values of other data types. When used as a number it becomes 0, when used as a string it becomes "", and when used as a Boolean value it becomes false. Since the source of many JavaScript errors is uninitialized variables, one of the common debugging techniques is to initialize all uninitialized variables, including the meaningless ones, with a null value. &lt;br /&gt;The JavaScript interpreter uses the null value on two occasions: &lt;br /&gt;(1) built-in functions return null under certain circumstances, and &lt;br /&gt;(2) nonexistent properties evaluate to null. &lt;br /&gt;When checking for a null value, you should check if it is false or if it is equal to null.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Type Conversion&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;In JavaScript data types are converted automatically as needed during the course of script execution. This can be quite convenient, and many other programming languages do not allow this. A variable may hold a numeric value at one point of the script and a string at another. The following statements constitute a valid JavaScript script: &lt;br /&gt;&lt;br /&gt;var myVar = 12&lt;br /&gt;myVar = "university"&lt;br /&gt;&lt;br /&gt;An object’s properties hold its data. Obviously you will want to refer to that data at some point, either to put new data in or to get out what the object’s property contains. You can refer to properties using the following syntax:&lt;br /&gt;&lt;br /&gt;objectReference.propertyName&lt;br /&gt;&lt;br /&gt;objectReference is the name of the object that the property belongs to, or any other valid reference to the object.&lt;br /&gt;&lt;br /&gt;A dot separates each object from its property. A hierarchical object structure, with objects and properties at different levels, uses the same&lt;br /&gt;syntax:&lt;br /&gt;&lt;br /&gt;object1.object2Property1.object3Property2.property3&lt;br /&gt;&lt;br /&gt;Notice that all the elements in the middle are both objects and properties.&lt;br /&gt;&lt;br /&gt;Another important concept is that a property belongs to an object, and to only one object. Different objects may have properties of the same name and type, but they are not the same property.&lt;br /&gt;&lt;br /&gt;A variable may be named exactly like a property of an object. This is possible because properties are not of the same scope as variables.&lt;br /&gt;However, a variable cannot have the same name as an object at the same level. For example, the statement var a = a.b.d is not valid (actually it is valid, but the value of a is lost).&lt;br /&gt;&lt;br /&gt;You can refer to properties and methods using either the “dot” syntax or the array notation. In array notation, square brackets replace the dots. For example, the following expression refers to a.b.d:&lt;br /&gt;&lt;br /&gt;a["b"]["d"]&lt;br /&gt;&lt;br /&gt;You can use the array notation for both properties and methods. The general syntax is:&lt;br /&gt;&lt;br /&gt;objectReference["propertyName"]&lt;br /&gt;objectReference["methodName"]([arguments])&lt;br /&gt;&lt;br /&gt;It is important to understand this alternative syntax, because you cannot always use the traditional dot syntax. For example, the first character of a property name cannot be a digit when using the dot syntax. When you create an array using the built-in Array object, you can only refer to the elements of the array via the array notation (e.g., myArray[0], myArray[99]). You must always use double quotes when you refer to a property of an object that is not an array. Here are some examples for using the array notation to reference methods and properties:&lt;br /&gt;&lt;br /&gt;document["write"]("hello!")&lt;br /&gt;window["alert"]("howdy!") // note: alert() == window.alert()&lt;br /&gt;Math["PI"]&lt;br /&gt;&lt;br /&gt;Sometimes you can only use the array notation. Suppose the variable str holds the string "write". You can use the following syntax instead of document.&lt;br /&gt;&lt;br /&gt;write():&lt;br /&gt;document[str]()&lt;br /&gt;&lt;br /&gt;An object does not inherit from other objects, but it can be contained by another object if it is a property of that object.&lt;br /&gt;&lt;br /&gt;JavaScript is completely based on dynamic binding. That is, object references are checked at run time.&lt;br /&gt;&lt;br /&gt;The following script’s output might surprise you a little:&lt;br /&gt;&lt;br /&gt;var num = 4&lt;br /&gt;function funct1()&lt;br /&gt;{&lt;br /&gt;alert(num)&lt;br /&gt;var num = 2&lt;br /&gt;}&lt;br /&gt;funct1()&lt;br /&gt;&lt;br /&gt;will give alert message saying 'undefined'.&lt;br /&gt;&lt;br /&gt;The term “undefined” means that a variable does not have a value at the desired point, although it has previously been declared. Generally speaking, JavaScript scans the entire script before any execution begins. During this scan, also called parsing, JavaScript evaluates each line of code, testing it for basic syntax errors. It also determines the scope of each variable, regardless of whether or not it is in a function. If it encounters a variable declaration with the keyword var, that variable is considered local, even if a global variable with the same name exists. A variable’s scope can be either the entire script or just one function. In the preceding script segment, a global variable num is declared by initializing it to 4. The function consists of one local variable declaration. Note that the variable num inside the function has no&lt;br /&gt;connection to the global variable num. The function is executed, as usual, from top to bottom. The first statement instructs JavaScript to display the value of num, the local variable inside the function. JavaScript already knows that the local variable is declared inside the function, so it does not generate an error. However, at that point, the variable does not retain any value, so it is undefined.&lt;br /&gt;&lt;br /&gt;If you declare global variables inside a function, you must assign an initial value to the variable; otherwise, JavaScript returns an error. The&lt;br /&gt;following script segment and screen capture demonstrate this fact:&lt;br /&gt;&lt;br /&gt;function foo()&lt;br /&gt;{&lt;br /&gt;// local variable declarations&lt;br /&gt;var a // without initialization&lt;br /&gt;var b = 0 // with initialization&lt;br /&gt;// global variable declarations&lt;br /&gt;c // without initialization — error!&lt;br /&gt;d = 0 // with initialization&lt;br /&gt;}&lt;br /&gt;foo()&lt;br /&gt;&lt;br /&gt;// Runtime error occurs.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Using the arguments Array&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;JavaScript supports functions that accept a variable number of arguments. The first argument is functionName.arguments[0], the second one is&lt;br /&gt;functionName.arguments[1], the third one is functionName.arguments[2], and so on. The number of arguments handed to the function is&lt;br /&gt;stored in the length property of the arguments object:&lt;br /&gt;&lt;br /&gt;functionName.arguments.length&lt;br /&gt;&lt;br /&gt;The following script demonstrates this concept:&lt;br /&gt;&lt;br /&gt;function calc()&lt;br /&gt;{&lt;br /&gt; document.write("The first argument is ", calc.arguments[0], "&lt;BR&gt;")&lt;br /&gt; document.write("The fourth argument is ", calc.arguments[3], "&lt;BR&gt;")&lt;br /&gt; document.write("There are ", calc.arguments.length, " arguments&lt;BR&gt;")&lt;br /&gt;}&lt;br /&gt;var company = "Yahoo"&lt;br /&gt;calc(2, 999, "internet", company, 0.0)&lt;br /&gt;&lt;br /&gt;The script’s output is:&lt;br /&gt;&lt;br /&gt;The first argument is 2&lt;br /&gt;The fourth argument is Yahoo&lt;br /&gt;There are 5 arguments&lt;br /&gt;&lt;br /&gt;The division operator also performs the usual arithmetic conversion. However, since JavaScript is loosely typed, this operator does not act exactly as in C, Perl, Java and other strictly typed programming languages. In those languages, integer division is different from floating-point division in that the result of integer division is always an integer number. JavaScript, on the other hand, does not explicitly distinguish between integers and real-valued numbers, and therefore, the result of a division operation is not guaranteed to be an integer number. In fact, most floating-point numbers are the result of a division operator. While debugging a script, it may be helpful to remember&lt;br /&gt;that the division operation in JavaScript generates the same value as your pocket calculator. You should also remember that the remainder of a&lt;br /&gt;division operation is never discarded. &lt;br /&gt;&lt;br /&gt;When the operands are floating-point numbers and cannot be represented in binary notation, division expressions often evaluate to inaccurate&lt;br /&gt;results.&lt;br /&gt;&lt;br /&gt;3 / 4 // evaluates to 0.75&lt;br /&gt;3.6 / 0.1 // evaluates to 36&lt;br /&gt;–20 / 4 // evaluates to –5&lt;br /&gt;11.1 / 2.22 // evaluates to 4.999999999999999&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Data Type Operator&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;typeof operand1&lt;br /&gt;or&lt;br /&gt;typeof (operand1)&lt;br /&gt;&lt;br /&gt;JavaScript provides an operator to check the data type of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. The operator returns the data type. The expression includes the word typeof followed by the literal or identifier. Here are some examples:&lt;br /&gt;&lt;br /&gt;typeof foo == "undefined" // when foo is undefined&lt;br /&gt;typeof eval == "function" // eval is a built-in function&lt;br /&gt;typeof null == "object" // null is an object&lt;br /&gt;typeof 3.14 == "number"&lt;br /&gt;typeof true == "Boolean"&lt;br /&gt;typeof "a string" == "string"&lt;br /&gt;// all of the expressions are true, of course&lt;br /&gt;&lt;br /&gt;The typeof operator is very useful for debugging. Until strong debugging tools are available, you must do all debugging by hand, and detecting the data type of a structure is sometimes essential.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Events :&lt;/strong&gt;&lt;br /&gt;Events are actions that occur in response to a specific user action on the web site. For example, clicking on a form element will trigger a click event. The events occur regardless of whether you provide code to respond to them. JavaScript provides you with a method to respond to these events and to take action based on them. In this way your web page becomes very dynamic, responding to user interaction.&lt;br /&gt;&lt;br /&gt;Not all actions that take place in your browser are events. Events are only actions that occur in the area of the HTML page, such as loading images and selecting form elements. If an action occurs directly in the boundary of the browser’s display zone, it is an event as well. In short, an event is only concerned with action on the web page itself, not on the browser.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Event Handlers&lt;/strong&gt;&lt;br /&gt;Event handlers correspond to their associated events. They are functions that execute automatically when events occur. Many JavaScript functions are called by event handlers. You saw earlier that events are actions that do not have any direct influence; they only serve the event handlers. Each event handler is associated with an event.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Arrays in Javascript:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;I found this topic very intresting&lt;br /&gt;&lt;br /&gt;In JavaScript, specifying the length of the array or the number of elements is not vital. An array is an object like any other object. There is no explicit array type in JavaScript.&lt;br /&gt;&lt;br /&gt;Here are some arrays:&lt;br /&gt;&lt;br /&gt;var day = new Array(31)&lt;br /&gt;var month = new Array(12)&lt;br /&gt;var year = new Array() // number of years "not known"&lt;br /&gt;&lt;br /&gt;All elements of an array are initially null. This is important because such elements do not have any influence on your script. An element with a null value actually does not exist.&lt;br /&gt;&lt;br /&gt;Strongly typed programming languages require that you specify the number of elements in an array. In such languages all elements of an array&lt;br /&gt;must contain values of the same type (int, char, …). An array is an explicit data structure in those languages. From the moment an array is declared, it takes up the required storage area in memory. It does not matter if you initialized its values or not. JavaScript, however, does not require you to initialize the elements in an array when you create it. Theoretically, if you created an array without specifying the number of elements, it would be as if you created one with zero elements. Extending an array in such languages is usually not possible; therefore, you must specify the length in all array declarations.&lt;br /&gt;&lt;br /&gt;The null value is given by default to each element of an array in JavaScript. When the value is null, the element is not stored in the computer’s  memory. So creating an array with an infinite number of elements is fine in JavaScript. However, doing so is not recommended since it would be confusing to people who read your code, and not particularly useful. Keep in mind that elements holding a null value do not take up space in memory, but they are considered elements in the array, so they are added to the array’s length property.&lt;br /&gt;&lt;br /&gt;With JavaScript (unlike C++), it doesn’t matter whether the elements of an array are of the same data type. They can be a mix of numbers, strings, Boolean values, objects, and even arrays (which are actually objects).&lt;br /&gt;&lt;br /&gt;Referring to an Array’s Elements Creating an array is fine, but you must be able to use it. To use an array, whether you are putting data in or taking it out, you need to access the elements of that array. Keep in mind that arrays in JavaScript are objects. Like all other objects, they have properties and methods:&lt;br /&gt;&lt;br /&gt;arrayObjectName[subscript] // ar1[4]&lt;br /&gt;&lt;br /&gt;The subscript follows the array name and is enclosed in square brackets. Subscripts are simple integers that start at zero.&lt;br /&gt;&lt;br /&gt;Here is a simple array:&lt;br /&gt;&lt;br /&gt;var ar = new Array(5)&lt;br /&gt;&lt;br /&gt;This array has five elements: ar[0], ar[1], ar[2], ar[3], and ar[4]. &lt;br /&gt;&lt;br /&gt;After you create an array you can increase its length by specifying a value for the highest subscript element. The following code creates an array of length zero, then assigns a null value to element 99. This changes the length of the array to 100.&lt;br /&gt;&lt;br /&gt;accounts = new Array() // array of zero elements&lt;br /&gt;accounts[99] = null // array of 100 elements&lt;br /&gt;&lt;br /&gt;Note that the array does not take up any storage space, even after it is extended.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Creating Dense Arrays&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;Recall that we stated earlier that you need not initialize the elements in an array before you use it. However, nothing says you cannot initialize them. This is often referred to as a dense array. A dense array is one in which each element has a value. You can construct a dense array of two or more elements starting with index (subscript) 0, if you define initial values for all elements. They are very popular in many scripting languages. You can populate an array by specifying the values of its elements:&lt;br /&gt;&lt;br /&gt;var bb = "baseball"&lt;br /&gt;var sports = new Array("football", bb, "basketball", "soccer")&lt;br /&gt;&lt;br /&gt;You can refer to the elements of this array with the common syntax:&lt;br /&gt;&lt;br /&gt;sports[0] == "football"&lt;br /&gt;sports[1] == "baseball"&lt;br /&gt;var val = 2&lt;br /&gt;sports[val] == "basketball"&lt;br /&gt;sports[3] == "soccer"&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Array Types&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;You must keep in mind that JavaScript is a loosely typed language. It should not be surprising, therefore, that elements of an array can be of different types. Some elements of a given array can be strings, while others can be numbers, Boolean values, and even objects. Basically, there are five types of arrays:&lt;br /&gt;&lt;br /&gt;1. String arrays&lt;br /&gt;2. Number arrays&lt;br /&gt;3. Boolean arrays&lt;br /&gt;4. Object arrays (including null arrays, because null is an object)&lt;br /&gt;5. Mixed arrays&lt;br /&gt;&lt;br /&gt;Sometimes you want to know what type of array you are dealing with. JavaScript does not include any tool to facilitate this. However, using a prototype we can add a property to all arrays (remember, arrays are objects by which you can create instances) that will return the type of the array. Here is the desired method:&lt;br /&gt;&lt;br /&gt;&lt;script&gt;&lt;br /&gt;&lt;br /&gt;sports = new Array("football", 1, true, "soccer");&lt;br /&gt;&lt;br /&gt;function getType(){&lt;br /&gt;&lt;br /&gt; var arrayType = typeof sports[0]&lt;br /&gt; for (var i = 0; i &lt; sports.length; i++){&lt;br /&gt;  if (typeof sports[i] != arrayType){&lt;br /&gt;   arrayType = "mixed";&lt;br /&gt;   break;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;alert(arrayType);&lt;br /&gt;return arrayType;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Array.prototype.getType = getType;&lt;br /&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;We can improve the efficiency of the above function by changing the loop statement.&lt;br /&gt;&lt;br /&gt;&lt;script&gt;&lt;br /&gt;&lt;br /&gt;sports = new Array("football", 1, true, "soccer");&lt;br /&gt;&lt;br /&gt;function getType(){&lt;br /&gt;&lt;br /&gt; var arrayType = typeof sports[0]&lt;br /&gt; for (var i in sports){&lt;br /&gt;  if (typeof sports[i] != arrayType){&lt;br /&gt;   arrayType = "mixed";&lt;br /&gt;   break;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;alert(arrayType);&lt;br /&gt;return arrayType;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Array.prototype.getType = getType;&lt;br /&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;The improvement in this function is the type of loop used.&lt;br /&gt;&lt;br /&gt;The for...in construct loops only through the existing properties, or elements, not including the null ones. The function works just like the previous one. &lt;br /&gt;&lt;br /&gt;At first, the data type of the first element of the array is assigned to arrayType. In every iteration of the loop, if a different type of element is found, the value of arrayType is changed to "mixed" because at least two different data types have been found in the array. Once a "mixed" array is detected, the loop is immediately terminated using a break statement.&lt;br /&gt;&lt;br /&gt;Sometimes you want to refer to all elements of a uniform-type array. The following method is designed to be a prototype of the Array object,&lt;br /&gt;returning an array of element subscripts where the specified value has been found. Here is the method:&lt;br /&gt;&lt;br /&gt;function getSubscripts(type)&lt;br /&gt;{&lt;br /&gt; var ar = new Array()&lt;br /&gt; var arSub = 0&lt;br /&gt; for (var i = 0; i &lt; this.length; ++i){&lt;br /&gt;  if (typeof this[i] == type){&lt;br /&gt;   ar[arSub] = this[i];&lt;br /&gt;   ++arSub;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;return ar&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Array.prototype.getSubscripts = getSubscripts&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Array Properties&lt;/em&gt;&lt;br /&gt;Although you can add your own properties to the Array object, not all tasks can be achieved by high-level programming via JavaScript. The only built-in property of the Array object is length. Fortunately, this is also the most commonly needed property for any array. When you create an instance of an object (an array), the number of properties (elements) is stored in the length property. You can refer to it as a regular property.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Array Methods&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;JavaScript features a number of methods, three of which are built-in methods:&lt;br /&gt;&lt;br /&gt;1. join()&lt;br /&gt;2. reverse()&lt;br /&gt;3. sort()&lt;br /&gt;&lt;br /&gt;chop()&lt;br /&gt;The chop() method chops off the last character of all strings that are elements of an array. Here is an example:&lt;br /&gt;&lt;br /&gt;function chop(){&lt;br /&gt; for (var i in this){&lt;br /&gt;  if (typeof this[i] == "string")&lt;br /&gt;   this[i] = this[i].substring(0, this[i].length – 1)&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Array.prototype.chop = chop&lt;br /&gt;&lt;br /&gt;// EXAMPLE&lt;br /&gt;var line = new Array("red", "green", "blue");&lt;br /&gt;line.chop();&lt;br /&gt;document.write(line.getList(" "));&lt;br /&gt;&lt;br /&gt;The script’s output is:&lt;br /&gt;re gree blu&lt;br /&gt;&lt;br /&gt;The general syntax is:&lt;br /&gt;arrayInstance.chop()&lt;br /&gt;&lt;br /&gt;grep()&lt;br /&gt;&lt;br /&gt;The term “grep” is borrowed from the Unix world where grep is a standard search function. We use that same concept here in JavaScript. The grep() method evaluates the expression (expr) for each element of the array. It returns another array consisting of those elements for which the expression evaluated to true (the pattern was matched). To understand this method you should know that if string2 is found in string1,&lt;br /&gt;string1.indexOf(string2) is not equal to –1. Here is the method:&lt;br /&gt;&lt;br /&gt;function grep(str){&lt;br /&gt; var ar = new Array()&lt;br /&gt; var arSub = 0&lt;br /&gt; for (var i in this){&lt;br /&gt;  if (typeof this[i] == "string" &amp;&amp; this[i].indexOf(str) != –1){&lt;br /&gt;   ar[arSub] = this[i];&lt;br /&gt;   arSub++;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;return ar;&lt;br /&gt;}&lt;br /&gt;Array.prototype.grep = grep&lt;br /&gt;var line = new Array("mask", "Mascot", "mine", "amass", "hot")&lt;br /&gt;document.write(line.grep("mas").getList(" "))&lt;br /&gt;document.write("&lt;BR&gt;")&lt;br /&gt;document.write(line.grep("mas").length)&lt;br /&gt;&lt;br /&gt;The output of this script is:&lt;br /&gt;mask amass&lt;br /&gt;2&lt;br /&gt;&lt;br /&gt;The general syntax is:&lt;br /&gt;arrayInstance.grep(expr)&lt;br /&gt;&lt;br /&gt;Like this you can keep on writing your own prototypes... and add methods and properties dynamically.&lt;br /&gt;&lt;br /&gt;Associative Arrays:&lt;br /&gt;Associative arrays use strings as subscripts. For example, an element of the array can be:&lt;br /&gt;color["yellow"] = "FFFF00"&lt;br /&gt;&lt;br /&gt;Such arrays group together related data. These arrays are actually objects, except that you use square brackets instead of a dot. Another important difference is that array specification (square brackets) enables you to refer to the value of a variable as the property of method specification rather than the actual literal. For example:&lt;br /&gt;&lt;br /&gt;var col = "yellow"&lt;br /&gt;color[col] = "FFFF00"&lt;br /&gt;Here is another interesting example:&lt;br /&gt;var w = "write"&lt;br /&gt;document[w]("Hello!") // prints "Hello!"&lt;br /&gt;&lt;br /&gt;If you replace the array notation with the regular dot specification you receive an error:&lt;br /&gt;&lt;br /&gt;var w = "write"&lt;br /&gt;document.w("Hello!")&lt;br /&gt; &lt;br /&gt;The dot notation requires the actual literal specified, as it does not evaluate the written value. You can use associative arrays to create nested objects resembling multidimensional arrays. For example, you can create an associative array where its subscripts are names of students in a class, so each element of the associative array is an object containing fields such as the student’s grade, age, and so on.&lt;br /&gt;&lt;br /&gt;Note that associative arrays cannot be created as instances of the built-in Array object. You must use a constructor function to create one.&lt;br /&gt;Here is an example using students: &lt;br /&gt;&lt;br /&gt;function studentClass(){&lt;br /&gt; for (var i = 0; i &lt; arguments.length; ++i){&lt;br /&gt;  this[studentClass.arguments[i]] = new student()&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function student(){&lt;br /&gt;&lt;br /&gt;// this.grade = null&lt;br /&gt;// this.age = null&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;var students = new studentClass("Bob", "John", "Frank", "Alfred","Sheila", "Mary")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;students["Bob "].grade = 40&lt;br /&gt;students["John "].age = 11&lt;br /&gt;students["Frank "].grade = "N/A"&lt;br /&gt;alert(students["Frank "].grade)&lt;br /&gt;&lt;br /&gt;Creating an associative array is not difficult. Array elements are created according to the names of the students accepted as parameters by the function studentClass(). The second function is rather strange—it contains no statements. When you create an instance you can use an “empty” constructor function. You may recall from an earlier discussion that objects may be extended by simply assigning values to them. So, the statement this[studentClass.arguments[i]] = new student() just makes sure each element of the associative array is an object. The global statements later create the properties simply by assigning them values.&lt;br /&gt;&lt;br /&gt;Another important concept to remember is that associative arrays are not explicit arrays. You can also refer to them using the dot syntax, as in:&lt;br /&gt;&lt;br /&gt;students.John.age&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The Date Object in Javascript&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;JavaScript comes with a built-in Date object that allows you to easily work with date and time data and related functions. Both dates and times in JavaScript are derived from the Date object, which behaves much like any other object. To implement the current date or time in your script, you must first create a new instance of the object. You can then extract the desired data from that particular instance. JavaScript handles dates in a manner very similar to Java. Many methods are implemented in both languages, resulting in an observable parallelism.&lt;br /&gt;&lt;br /&gt;var dateInstance = new Date()&lt;br /&gt;&lt;br /&gt;JavaScript provides JavaScript programmers with a variety of methods to deal with instances of the Date object. However, extracting and manipulating their data makes them one of the most important elements of the language.&lt;br /&gt;&lt;br /&gt;getYear()&lt;br /&gt;getMonth() //0 to 11&lt;br /&gt;getDate()&lt;br /&gt;getDay() //Sunday, Monday, Tuesday,...&lt;br /&gt;getHours()&lt;br /&gt;getMinutes()&lt;br /&gt;getSeconds()&lt;br /&gt;getTime()&lt;br /&gt;&lt;br /&gt;//setters for the above...&lt;br /&gt;&lt;br /&gt;&amp; many more methods.&lt;br /&gt;&lt;br /&gt;Time-Related Methods of Other Objects&lt;br /&gt;&lt;br /&gt;setTimeout()&lt;br /&gt;&lt;br /&gt;The setTimeout() method evaluates an expression after a specified number of milliseconds have elapsed. Its general syntax is:&lt;br /&gt;&lt;br /&gt;timeoutID = setTimeout(expression, msec)&lt;br /&gt;&lt;br /&gt;timeoutID is an identifier used to identify the current timeout. expression is a string expression or property of an existing object. It is&lt;br /&gt;normally a simple statement that is to be executed after the specified time has ticked off.&lt;br /&gt;&lt;br /&gt;msec is a numeric value, a numeric string, or a property of an existing object in millisecond units.&lt;br /&gt;&lt;br /&gt;The setTimeout() method evaluates an expression after a specified amount of time. Take a look at the following example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;HTML&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;HEAD&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;TITLE&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;setTimeout() method&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/TITLE&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;SCRIPT&lt;/font&gt; &lt;font color="#2040a0"&gt;LANGUAGE&lt;/font&gt; &lt;font color="#2040a0"&gt;=&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;JavaScript&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;function displayAlert()&lt;br /&gt;{&lt;br /&gt;alert(&amp;quot;5 seconds have elapsed since the button was clicked.&amp;quot;)&lt;br /&gt;}&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/SCRIPT&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/HEAD&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;BODY&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;FORM&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;Click the button on the left for a reminder in 5 seconds;&lt;br /&gt;click the button on the right to cancel the reminder before&lt;br /&gt;it is displayed.&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;P&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;INPUT&lt;/font&gt; &lt;font color="#2040a0"&gt;TYPE&lt;/font&gt; &lt;font color="#2040a0"&gt;=&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;button&amp;quot;&lt;/font&gt; &lt;font color="#2040a0"&gt;VALUE&lt;/font&gt; &lt;font color="#2040a0"&gt;=&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;5-second reminder&amp;quot;&lt;/font&gt;&lt;font color="#2040a0"&gt;&lt;br /&gt;NAME&lt;/font&gt; &lt;font color="#2040a0"&gt;=&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;remind_button&amp;quot;&lt;/font&gt;&lt;font color="#2040a0"&gt;&lt;br /&gt;onClick&lt;/font&gt; &lt;font color="#2040a0"&gt;=&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;timerID = setTimeout('displayAlert()',5000)&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/FORM&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/BODY&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;&lt;strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;/HTML&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;When you click the button, the event handler’s script sets a timeout. The timeout specifies that after 5,000 milliseconds, or five seconds, the function displayAlert() is called. Therefore, five seconds after you click the button an alert box is displayed.&lt;br /&gt;&lt;br /&gt;This method does not repeatedly execute the specified statement. That is, it does not execute it every five seconds. When the time limit specified has ticked down, the statement is executed and the timeout does not exist anymore. setTimeout() is a method of the window or frame object, depending on the basic structure of the HTML document it is used in.&lt;br /&gt;&lt;br /&gt;and also we have some math related methods also like parseInt(), parseFloat() also.&lt;br /&gt;&lt;br /&gt;and also we have some string functions like... charAt(), indexOf(), substring(), toString()&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The Document Object Model&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The Document Object Model (DOM), one of the most powerful tools available to a web developer, is a hierarchy of objects that represent the web&lt;br /&gt;browser, the browser’s history, the currently loaded web page, and other important elements.&lt;br /&gt;&lt;br /&gt;hierarchy in DOM is...&lt;br /&gt;1. Window&lt;br /&gt;1.a.navigator&lt;br /&gt;1.b.screen&lt;br /&gt;1.c.history&lt;br /&gt;1.d.location&lt;br /&gt;1.e.document&lt;br /&gt;&lt;br /&gt;The window Object&lt;br /&gt;&lt;br /&gt;The window object is at the top of the DOM hierarchy. This object represents the browser window. You will find that you don’t often deal directly with the window object, but rather as the container for the other objects you use directly on a regular basis. This doesn’t mean that you never use the window object directly, merely that it is not as commonly used as other objects.&lt;br /&gt;&lt;br /&gt;The window object contains properties that apply to the entire window. For example, the status bar of the browser is a property of this object. It also includes a property that is actually the object itself.&lt;br /&gt;&lt;br /&gt;few examples of ways you might use the window object.&lt;br /&gt;&lt;br /&gt;var newwindow = window.open("http://www.chuckeasttom.com") //Open a new browser&lt;br /&gt;window.newwindow.focus                                     //Set focus to that window.&lt;br /&gt;newwindow.close                                            //Close that window.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The navigator Object&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;This object provides information about the browser being used. This can be quite useful in gathering information about people who visit your web site. In fact, the amount of information you can derive from this object is really rather astounding.&lt;br /&gt;&lt;br /&gt;some of the methods and properties of the navigator object.&lt;br /&gt;&lt;br /&gt;appName                     //The name of the end user’s browser.&lt;br /&gt;appVersion                  //The version number of the end user’s browser.&lt;br /&gt;cookieEnabled               //Whether or not the end user’s browser has cookies enabled.&lt;br /&gt;platform                    //The end user’s operating system.&lt;br /&gt;javaEnabled()               //A method that returns a Boolean telling if the browser has JavaScript enabled.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The document Object&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;By far the most useful object of the DOM is the document object. It contains properties for the current page loaded in the window. The properties of this object are content-dependent because each and every page has its own outline and elements. Almost everything in the page is a property of the document object, including links, images, forms and their elements, anchors, and more. Because each frame is a window (window object), it contains a document object as well. Even the background and the title are properties of this object.&lt;br /&gt;&lt;br /&gt;write()                  //This is a very commonly used method and writes to the HTML page whatever is passed to the write function.&lt;br /&gt;fileCreatedDate          //This property shows when the loaded HTML file was created.&lt;br /&gt;linkColor                //This property shows the color of HTML links in the document. It is specified in the BODY tag.&lt;br /&gt;open()                   //This method opens a new document in a new window. &lt;br /&gt;embeds                   //An array containing all the plug-ins in a document.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The history Object&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The history object is also a property of the window object. It contains properties of the URLs the user has previously visited. This information is stored in a history list, and is accessible through the browser’s menu. If the document consists of child documents in frames, each frame has its own history list, or history object. This object also contains methods enabling you to send the user’s browser to a URL found in the history list.&lt;br /&gt;&lt;br /&gt;some of the history object’s most important properties and methods are below.&lt;br /&gt;&lt;br /&gt;current                    //The current document URL.&lt;br /&gt;previous                   //The URL of the last document in the history list.&lt;br /&gt;next                       //The URL of the next document in the history list.&lt;br /&gt;length                     //The number of entries currently stored in the history object.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The location Object&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The location object contains properties of the current URL. Although you may think of URLs as simple standard structures, they are far more than&lt;br /&gt;that. There are many types of protocols and various sections in every URL. There are also optional sections in the URL, such as anchor names and queries.&lt;br /&gt;&lt;br /&gt;host                         //The URL hostname and port.&lt;br /&gt;href                         // The entire URL.&lt;br /&gt;port                         //The port being used.&lt;br /&gt;search                        //The URL query string section (i.e., the section after and including the question mark).&lt;br /&gt;reload                        //The location.reload method forces a reload of the window’s current document.&lt;br /&gt;replace                       //The replace() method is also a property of the location, or window.location, object. It overwrites the current history entry with the specified URL.  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each property of the location object represents a different portion of the URL. The following syntax of a URL shows the relationship between the object’s properties:&lt;br /&gt;&lt;br /&gt;protocol//hostname:port pathname search hash&lt;br /&gt;&lt;br /&gt;Since the Document Object Model is a W3C specification, this means its behavior and usage are not dependent on a particular vendor. The DOM should be the same with Internet Explorer, Mozilla, or any browser. An important objective for any W3C standard and for the Document Object Model is to provide a standard that can be used in a wide variety of environments and applications. The DOM is designed to be used with any programming language.&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-5848069955526308177?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/5848069955526308177/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=5848069955526308177' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/5848069955526308177'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/5848069955526308177'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/02/advanced-javascript-by-chuck-easttom.html' title='Advanced JavaScript by Chuck Easttom'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-8864315393951065438</id><published>2009-02-06T08:47:00.000-08:00</published><updated>2009-02-06T08:58:49.735-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Class Loaders in Java'/><title type='text'>Class Loaders in Java</title><content type='html'>Yesterday I was to know about Class Loaders and I found it very interesting. I would like to share with you the same.&lt;br /&gt;&lt;br /&gt;The Class loader concept, one of the cornerstones of the Java virtual machine, describes the behavior of converting a named class into the bits responsible for implementing that class. Because class loaders exist, the Java run time does not need to know anything about files and file systems when running Java programs. &lt;br /&gt;&lt;br /&gt;Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM. So how is the very first class loaded? The very first class is specially loaded with the help of static main() method declared in your class. All the subsequently loaded classes are loaded by the classes, which are already loaded and running. A class loader creates a namespace. All JVMs include at least one class loader that is embedded within the JVM called the primordial (or bootstrap) class loader. It is somewhat special because the virtual machine assumes that it has access to a repository of trusted classes which can be run by the VM without verification. &lt;br /&gt;&lt;br /&gt;When a new JVM instance is started , the bootstrap class loader is responsible for loading key java classes like java.lang.Object and other runtime code into memory. The runtime classes are packaged inside jre/lib/rt.jar file. We cannot find the details of the bootstrap class loader in the java language specification, since this is a native implementation. For this reason the behavior of the bootstrap class loader will differ across JVMs. &lt;br /&gt;&lt;br /&gt;Java can change its class storage model simply by changing the set of functions that implements the class loader. &lt;br /&gt;&lt;br /&gt;Now let’s look at non-primordial class loaders. The JVM has hooks in it to allow user defined class loaders to be used in place of primordial class loader. &lt;br /&gt;&lt;br /&gt;ex : Bootstrap (primordial) Loads JDK internal classes, java.* packages. (as defined in the sun.boot.class.path system property, typically loads rt.jar and i18n.jar) and is the parent in the Class loaders hierarchy.&lt;br /&gt;&lt;br /&gt;next comes the Extensions (Loads jar files from JDK extensions directory (as defined in the java.ext.dirs system property – usually lib/ext directory of the JRE) and then System (Loads classes from system classpath (as defined by the java.class.path property, which is set by the CLASSPATH environment variable or –classpath or –cp command line options) ClassLoaders.&lt;br /&gt;&lt;br /&gt;Classes loaded by Bootstrap class loader have no visibility into classes loaded by its descendants (ie Extensions and Systems class loaders). &lt;br /&gt;&lt;br /&gt;The classes loaded by system class loader have visibility into classes loaded by its parents (ie Extensions and Bootstrap class loaders).&lt;br /&gt;&lt;br /&gt;If there were any sibling class loaders they cannot see classes loaded by each other. They can only see the classes loaded by their parent class loader. For example Sibling1 class loader cannot see classes loaded by&lt;br /&gt;Sibling2 class loader &lt;br /&gt;&lt;br /&gt;Both Sibling1 and Sibling2 class loaders have visibilty into classes loaded by their parent class loaders (eg: System, Extensions, and Bootstrap)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Class loaders are hierarchical and use a delegation model when loading a class. Class loaders request their parent to load the class first before attempting to load it themselves. When a class loader loads a class, the child class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note : Two objects loaded by different class loaders are never equal even if they carry the same values, which mean a class is uniquely identified in the context of the associated class loader. This applies to singletons too, where each class loader will have its own singleton.&lt;br /&gt;&lt;br /&gt;At its simplest, a class loader creates a flat name space of class bodies that are referenced by a string name. The method definition is: &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Class r = loadClass(String className, boolean resolveIt); &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The variable className contains a string that is understood by the class loader and is used to uniquely identify a class implementation. The variable resolveIt is a flag to tell the class loader that classes referenced by this class name should be resolved (that is, any referenced class should be loaded as well). &lt;br /&gt;&lt;br /&gt;We can build our own ClassLoader by being a subclass of java.lang.ClassLoader. The only abstract method that must be implemented is loadClass(). &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Static Class Loading :&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Classes are statically loaded with Java’s 'new' operator.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;MyClass&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;main&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;args&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;Car&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Car&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A NoClassDefFoundException is thrown if a class is referenced with Java’s 'new' operator (i.e. static loading) but the runtime system cannot find the referenced class.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Dynamic class loading:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. &lt;br /&gt;&lt;br /&gt;we can load classes dynamically by...&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Class&lt;/font&gt;.&lt;font color="#2040a0"&gt;forName&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;className&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;//static method which returns a Class&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The above static method returns the class object associated with the class name. The string className can be supplied dynamically at run time. Unlike the static loading, the dynamic loading will decide whether to load the class Car or the class Jeep at runtime based on a properties file and/or other runtime conditions. Once the class is dynamically loaded the following method returns an instance of the loaded class. It’s just like creating a class object with no arguments.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;class&lt;/strong&gt;.&lt;font color="#2040a0"&gt;newInstance&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;//A non-static method, which creates an instance of a class (i.e. creates an object).&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Jeep&lt;/font&gt; &lt;font color="#2040a0"&gt;myJeep&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;null&lt;/strong&gt; &lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#444444"&gt;//myClassName should be read from a properties file or Constants interface.&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#444444"&gt;//stay away from hard coding values in your program. &lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;myClassName&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot;au.com.Jeep&amp;quot;&lt;/font&gt; &lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Class&lt;/font&gt; &lt;font color="#2040a0"&gt;vehicleClass&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;Class&lt;/font&gt;.&lt;font color="#2040a0"&gt;forName&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;myClassName&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;myJeep&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Jeep&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;vehicleClass&lt;/font&gt;.&lt;font color="#2040a0"&gt;newInstance&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;myJeep&lt;/font&gt;.&lt;font color="#2040a0"&gt;setFuelCapacity&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;50&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;A ClassNotFoundException is thrown when an application tries to load in a class through its string name using the following methods but no definition for the class with the specified name could be found:&lt;br /&gt;1. The forName(..) method in class - Class.&lt;br /&gt;2. The findSystemClass(..) method in class - ClassLoader.&lt;br /&gt;3. The loadClass(..) method in class - ClassLoader.&lt;br /&gt;&lt;br /&gt;The Java virtual machine has hooks in it to allow a user-defined class loader to be used in place of the primordial one. Furthermore, since the user class loader gets first crack at the class name, the user is able to implement any number of interesting class repositories, not the least of which is HTTP servers -- which got Java off the ground in the first place. &lt;br /&gt;&lt;br /&gt;There is a cost, however, because the class loader is so powerful (for example, it can replace java.lang.Object with its own version), Java classes like applets are not allowed to instantiate their own loaders. (This is enforced by the class loader, by the way.) This column will not be useful if you are trying to do this stuff with an applet, only with an application running from the trusted class repository (such as local files). &lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-8864315393951065438?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/8864315393951065438/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=8864315393951065438' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/8864315393951065438'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/8864315393951065438'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/02/class-loaders-in-java.html' title='Class Loaders in Java'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-893240845020245132</id><published>2009-01-26T04:08:00.000-08:00</published><updated>2009-01-26T04:24:09.328-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Generics in Java'/><title type='text'>Generics in the Java Programming Language by Gilid Bracha</title><content type='html'>Yesterday I read Generics in the Java Programming Language by Gilid Bracha. &lt;br /&gt;&lt;br /&gt;I would like to thank author for producing such an excellent information.&lt;br /&gt;&lt;br /&gt;I really enjoyed reading each and every point and would like to share them with you.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;myIntList&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;LinkedList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt;&lt;font color="#2040a0"&gt;myIntList&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt; &lt;font color="#2040a0"&gt;x&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;myIntList&lt;/font&gt;.&lt;font color="#2040a0"&gt;iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;next&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Notice the type declaration for the variable myIntList. It specifies that this is not just an arbitrary List, but a List of Integer, written List&lt;Integer&gt;. We say that List is a generic interface that takes a type parameter - in this case, Integer. We also specify a type parameter when creating the list object.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;E&lt;/font&gt; &lt;font color="#2040a0"&gt;x&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;Iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;Iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;font color="#2040a0"&gt;E&lt;/font&gt; &lt;font color="#2040a0"&gt;next&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;boolean&lt;/strong&gt; &lt;font color="#2040a0"&gt;hasNext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Type parameters can be used throughout the generic declaration, pretty much where you would use ordinary types.&lt;br /&gt;&lt;br /&gt;As we saw invocations of the generic type declaration List, such as List&lt;Integer&gt;. In the invocation (usually called a parameterized type), all occurrences of the formal type parameter (E in this case) are replaced by the actual type argument (in this case, Integer). You might imagine that List&lt;Integer&gt; stands for a version of List where E has been uniformly replaced by Integer:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;IntegerList&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt; &lt;font color="#2040a0"&gt;x&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;Iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;A generic type declaration is compiled once and for all, and turned into a single class file, just like an ordinary class or interface declaration.&lt;br /&gt;&lt;br /&gt;Type parameters are analogous to the ordinary parameters used in methods or constructors. Much like a method has formal value parameters that describe the kinds of values it operates on, a generic declaration has formal type parameters. When a method is invoked, actual arguments are substituted for the formal parameters, and the method body is evaluated. When a generic declaration is invoked, the actual type arguments&lt;br /&gt;are substituted for the formal type parameters.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Generics and Subtyping&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;What about the below code???&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;ls&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;lo&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;ls&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Line 1 is certainly legal. The trickier part of the question is line 2. &lt;br /&gt;The Java compiler will prevent this from happening of course. Line 2 will cause a compile time error.&lt;br /&gt;This boils down to the question: is a List of String a List of Object. Most people’s instinct is to answer:&lt;br /&gt;“sure!”.&lt;br /&gt;Well, take a look at below code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;lo&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;s&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;ls&lt;/font&gt;.&lt;font color="#2040a0"&gt;get&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here we’ve aliased ls and lo. Accessing ls, a list of String, through the alias lo, we can insert arbitrary objects into it. As a result ls does not hold just Strings anymore, and when we try and get something out of it, we get a rude surprise. &lt;br /&gt;&lt;br /&gt;In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G&lt;Foo&gt; is a subtype of G&lt;Bar&gt;. This is probably the hardest thing you need to learn about generics, because it goes against our deeply held intuitions.&lt;br /&gt;&lt;br /&gt;We will look further how to cope with this sort of situation&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Wildcards&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Consider the problem of writing a routine that prints out all the elements in a collection.&lt;br /&gt;&lt;br /&gt;Here’s how you might write it in an older version of the language:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;printCollection&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;Iterator&lt;/font&gt; &lt;font color="#2040a0"&gt;i&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;.&lt;font color="#2040a0"&gt;iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;for&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;k&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#2040a0"&gt;k&lt;/font&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;.&lt;font color="#2040a0"&gt;size&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#2040a0"&gt;k&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;.&lt;font color="#2040a0"&gt;next&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And here is a naive attempt at writing it using generics (and the new for loop syntax):&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;printCollection&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;for&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="#2040a0"&gt;e&lt;/font&gt; &lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;e&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The problem is that this new version is much less useful than the old one. Whereas the old code could be called with any kind of collection as a parameter, the new code only takes Collection&amp;lt;Object&amp;gt;, which, as we’ve just demonstrated, is not a supertype of all kinds of collections!&lt;br /&gt;&lt;br /&gt;So what is the supertype of all kinds of collections? It’s written Collection&lt;?&gt; (pronounced “collection of unknown”) , that is, a collection whose element type matches anything. It’s called a wildcard type for obvious reasons. We can write:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;printCollection&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;for&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="#2040a0"&gt;e&lt;/font&gt; &lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;e&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;and now, we can call it with any type of collection. Notice that inside printCollection(), we can still read elements from c and give them type Object. This is always safe, since whatever the actual type of the collection, it does contain objects. It isn’t safe to add arbitrary objects to it however:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;c&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// compile time error&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Since we don’t know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection. When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don’t know what type that is, we cannot pass anything in. The sole exception is null, which is a&lt;br /&gt;member of every type. &lt;br /&gt;&lt;br /&gt;On the other hand, given a List&lt;?&gt;, we can call get() and make use of the result. The result type is an unknown type, but we always know that it is an object. It is 5 therefore safe to assign the result of get() to a variable of type Object or pass it as a parameter where the type Object is expected.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Bounded Wildcards&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Consider a simple drawing application that can draw shapes such as rectangles and circles.&lt;br /&gt;&lt;br /&gt;To represent these shapes within the program, you could define a class hierarchy such as this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Shape&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;draw&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Canvas&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Circle&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Shape&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;x&lt;/font&gt;, &lt;font color="#2040a0"&gt;y&lt;/font&gt;, &lt;font color="#2040a0"&gt;radius&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;draw&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Canvas&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; ... &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Rectangle&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Shape&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;x&lt;/font&gt;, &lt;font color="#2040a0"&gt;y&lt;/font&gt;, &lt;font color="#2040a0"&gt;width&lt;/font&gt;, &lt;font color="#2040a0"&gt;height&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;draw&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Canvas&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; ... &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;These&lt;/font&gt; &lt;font color="#2040a0"&gt;classes&lt;/font&gt; &lt;font color="#2040a0"&gt;can&lt;/font&gt; &lt;font color="#2040a0"&gt;be&lt;/font&gt; &lt;font color="#2040a0"&gt;drawn&lt;/font&gt; &lt;font color="#2040a0"&gt;on&lt;/font&gt; &lt;font color="#2040a0"&gt;a&lt;/font&gt; &lt;font color="#2040a0"&gt;canvas&lt;/font&gt;&lt;font color="4444FF"&gt;:&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Canvas&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;draw&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Shape&lt;/font&gt; &lt;font color="#2040a0"&gt;s&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;s&lt;/font&gt;.&lt;font color="#2040a0"&gt;draw&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Any drawing will typically contain a number of shapes. Assuming that they are represented as a list, it would be convenient to have a method in Canvas that draws them all:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;drawAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Shape&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;shapes&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;for&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Shape&lt;/font&gt; &lt;font color="#2040a0"&gt;s&lt;/font&gt;&lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;font color="#2040a0"&gt;shapes&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;s&lt;/font&gt;.&lt;font color="#2040a0"&gt;draw&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now, the type rules say that drawAll() can only be called on lists of exactly Shape: it cannot, for instance, be called on a List&lt;Circle&gt;. That is unfortunate, since all the method does is read shapes from the list, so it could just as well be called on a List&lt;Circle&gt;. What we really want is for the method to accept a list of any kind of shape:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;drawAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;? &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Shape&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;shapes&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; ... &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;There is a small but very important difference here: we have replaced the type List&lt;Shape&gt; with List&lt;? extends Shape&gt;. Now drawAll() will accept lists of any subclass of Shape, so we can now call it on a List&lt;Circle&gt; if we want. &lt;br /&gt;&lt;br /&gt;List&lt;? extends Shape&gt; is an example of a bounded wildcard. The ? stands for an unknown type, just like the wildcards we saw earlier. However, in this case, we know that this unknown type is in fact a subtype of Shape1. We say that Shape is the upper bound of the wildcard.&lt;br /&gt;&lt;br /&gt;There is, as usual, a price to be paid for the flexibility of using wildcards. That price is that it is now illegal to write into shapes in the body of the method. For instance, this is not allowed:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;addRectangle&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;? &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Shape&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;shapes&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;shapes&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;0&lt;/font&gt;, &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Rectangle&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// compile-time error!&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You should be able to figure out why the code above is disallowed. The type of the second parameter to shapes.add() is ? extends Shape - an unknown subtype of Shape. Since we don’t know what type it is, we don’t know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn’t safe to pass a Rectangle there.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Generic Methods&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Consider writing a method that takes an array of objects and a collection and puts all objects in the array into the collection.&lt;br /&gt;&lt;br /&gt;Here is a first attempt:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;fromArrayToCollection&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;a&lt;/font&gt;, &lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;for&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt; &lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;font color="#2040a0"&gt;a&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;  &lt;font color="#2040a0"&gt;c&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;o&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// compile time error&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;By now, you will have learned to avoid the beginner’s mistake of trying to use Collection&amp;lt;Object&amp;gt; as the type of the collection parameter. You may or may not have recognized that using Collection&amp;lt;?&amp;gt; isn’t going to work either. Recall that you cannot just shove objects into a collection of unknown type.&lt;br /&gt;&lt;br /&gt;The way to do deal with these problems is to use generic methods. Just like type declarations, method declarations can be generic - that is, parameterized by one or more type parameters.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;static&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;fromArrayToCollection&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;a&lt;/font&gt;, &lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;for&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt; &lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;font color="#2040a0"&gt;a&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;c&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;o&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// correct&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We can call this method with any kind of collection whose element type is a supertype of the element type of the array.&lt;br /&gt;&lt;br /&gt;Notice that we don’t have to pass an actual type argument to a generic method. The compiler infers the type argument for us, based on the types of the actual arguments. It will generally infer the most specific type argument that will make the call type-correct.&lt;br /&gt;&lt;br /&gt;when should I use generic methods, and when should I use wildcard types? &lt;br /&gt;&lt;br /&gt;To understand the answer, let’s examine a few methods from the Collection libraries.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;boolean&lt;/strong&gt; &lt;font color="#2040a0"&gt;containsAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;boolean&lt;/strong&gt; &lt;font color="#2040a0"&gt;addAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;? &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We could have used generic methods here instead:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;strong&gt;boolean&lt;/strong&gt; &lt;font color="#2040a0"&gt;containsAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;strong&gt;boolean&lt;/strong&gt; &lt;font color="#2040a0"&gt;addAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;type variables can have bounds too!&lt;br /&gt;&lt;br /&gt;However, in both containsAll and addAll, the type parameter T is used only once. The return type doesn’t depend on the type parameter, nor does any other argument to the method (in this case, there simply is only one argument). This tells us that the type argument is being used for polymorphism; its only effect is to allow a variety of actual argument types to be used at different invocation sites. If that is the case, one&lt;br /&gt;should use wildcards. Wildcards are designed to support flexible subtyping, which is what we’re trying to express here. &lt;br /&gt;&lt;br /&gt;Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn’t such a dependency, a generic method should not be used.&lt;br /&gt;&lt;br /&gt;It is possible to use both generic methods and wildcards in tandem.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Using Legacy Code in Generic Code&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Calling legacy code from generic code is inherently dangerous; once you mix generic code with non-generic legacy code, all the safety guarantees that the generic type system usually provides are void. However, you are still better off than you were without using generics at all. At least you know the code on your end is consistent. &lt;br /&gt;&lt;br /&gt;At the moment there’s a lot more non-generic code out there then there is generic code, and there will inevitably be situations where they have to mix. &lt;br /&gt;If you find that you must intermix legacy and generic code, pay close attention to the unchecked warnings. Think carefully how you can justify the safety of the code that gives rise to the warning.&lt;br /&gt;&lt;br /&gt;What happens if you still made a mistake, and the code that caused a warning is indeed not type safe? Let’s take a look at such a situation. In the process, we’ll get some insight into the workings of the compiler.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Erasure and Translation&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;loophole&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt; &lt;font color="#2040a0"&gt;x&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;ys&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;LinkedList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt; &lt;font color="#2040a0"&gt;xs&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;ys&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;xs&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;x&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// compile-time unchecked warning&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;ys&lt;/font&gt;.&lt;font color="#2040a0"&gt;iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;next&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here, we’ve aliased a list of strings and a plain old list. We insert an Integer into the list, and attempt to extract a String. This is clearly wrong. If we ignore the warning and try to execute this code, it will fail exactly at the point where we try to use the wrong type. At run time, this code behaves like:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;loophole&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt; &lt;font color="#2040a0"&gt;x&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt; &lt;font color="#2040a0"&gt;ys&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;LinkedList&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt; &lt;font color="#2040a0"&gt;xs&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;ys&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;xs&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;x&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;return&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;ys&lt;/font&gt;.&lt;font color="#2040a0"&gt;iterator&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;next&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// run time error&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;When we extract an element from the list, and attempt to treat it as a string by casting it to String, we will get a ClassCastException. The exact same thing happens with the generic version of loophole(). The reason for this is, that generics are implemented by the Java compiler as a front-end conversion called erasure. You can (almost) think of it as a source-to-source translation, whereby the generic version of loophole() is converted to the non-generic version.&lt;br /&gt;&lt;br /&gt;As a result, the type safety and integrity of the Java virtual machine are never at risk, even in the presence of unchecked warnings. Basically, erasure gets rid of (or erases) all generic type information. All the type information betweeen angle brackets is thrown out, so, for example, a parameterized type like List&lt;String&gt; is converted into List. All remaining uses of type variables are replaced by the upper bound of the type variable (usually Object). And, whenever the resulting code isn’t type-correct, a cast to the appropriate type is inserted, as in the last&lt;br /&gt;line of loophole.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;A Generic Class is Shared by all its Invocations.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;l1&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;l2&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;l1&lt;/font&gt;.&lt;font color="#2040a0"&gt;getClass&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;l2&lt;/font&gt;.&lt;font color="#2040a0"&gt;getClass&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It prints true, because all instances of a generic class have the same run-time class, regardless of their actual type parameters.&lt;br /&gt;&lt;br /&gt;Indeed, what makes a class generic is the fact that it has the same behavior for all of its possible type parameters; the same class can be viewed as having many different types.&lt;br /&gt;&lt;br /&gt;As consequence, the static variables and methods of a class are also shared among all the instances. That is why it is illegal to refer to the type parameters of a type declaration in a static method or initializer, or in the declaration or initializer of a static variable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Generic class is shared among all its instances, is that it usually makes no sense to ask an instance if it is an instance of a particular&lt;br /&gt;invocation of a generic type:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt; &lt;font color="#2040a0"&gt;cs&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;if&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;cs&lt;/font&gt; &lt;strong&gt;instanceof&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; ...&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt; &lt;font color="#444444"&gt;// illegal&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;similarly, a cast such as &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;cstr&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;cs&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// unchecked warning&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;gives an unchecked warning, since this isn’t something the run time system is going to check for you. The same is true of type variables&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;badCast&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;t&lt;/font&gt;, &lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;return&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;font color="#444444"&gt;// unchecked warning&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Type variables don’t exist at run time. This means that they entail no performance overhead in either time nor space, which is nice. Unfortunately, it also means that you can’t reliably use them in casts.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Arrays&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The component type of an array object may not be a type variable or a parameterized type, unless it is an (unbounded) wildcard type.You can declare array types whose element type is a type variable or a parameterized type, but not array objects.&lt;br /&gt;&lt;br /&gt;This is annoying, to be sure. This restriction is necessary to avoid situations like:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;lsa&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;10&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// not really allowed&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;lsa&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;oa&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;li&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;li&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;3&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;oa&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;1&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;li&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// unsound, but passes run time store check&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;s&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;lsa&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;1&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;get&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// run-time error - ClassCastException&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If arrays of parameterized type were allowed, the example above would compile without any unchecked warnings, and yet fail at run-time. We’ve had type-safety as a primary design goal of generics. In particular, the language is designed to guarantee that if your entire application has been compiled without unchecked warnings using javac -source 1.5, it is type safe.&lt;br /&gt;&lt;br /&gt;However, you can still use wildcard arrays. Here are two variations on the code above. The first forgoes the use of both array objects and array types whose element type is parameterized. As a result, we have to cast explicitly to get a String out of the array.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;lsa&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;10&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// ok, array of unbounded wildcard type&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;lsa&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;oa&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;li&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;li&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;3&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;oa&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;1&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;li&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// correct&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;s&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;lsa&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;1&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;get&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// run time error, but cast is explicit&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In the next variation, we refrain from creating an array object whose element type is parameterized, but still use an array type with a parameterized element type. This is legal, but generates an unchecked warning. Indeed, the code is unsafe, and eventually an error occurs.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;lsa&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;10&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// unchecked warning - this is unsafe!&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;lsa&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;oa&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;o&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;li&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;ArrayList&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;li&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Integer&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;3&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;oa&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;1&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;li&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// correct&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;s&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;lsa&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;1&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;get&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// run time error, but we were warned&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Similarly, attempting to create an array object whose element type is a type variable causes a compile-time error:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;makeArray&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;t&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;100&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// error&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Since type variables don’t exist at run time, there is no way to determine what the actual array type would be.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;More Fun with Wildcards&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;We’ve seen several examples where bounded wildcards were useful when reading from a data structure. Now consider the inverse, a write-only data structure. &lt;br /&gt;&lt;br /&gt;The interface Sink is a simple example of this sort.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;Sink&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;flush&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;t&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We can imagine using it as demonstrated by the code below. The method writeAll() is designed to flush all elements of the collection coll to the sink snk, and return the last element flushed.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;writeAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;coll&lt;/font&gt;, &lt;font color="#2040a0"&gt;Sink&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;snk&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;last&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;for&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;t&lt;/font&gt; &lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;font color="#2040a0"&gt;coll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;last&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;t&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;snk&lt;/font&gt;.&lt;font color="#2040a0"&gt;flush&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;last&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;last&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;...&lt;br /&gt;&lt;font color="#2040a0"&gt;Sink&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Object&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;s&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;cs&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;str&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;writeAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;cs&lt;/font&gt;, &lt;font color="#2040a0"&gt;s&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// illegal call&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As written, the call to writeAll() is illegal, as no valid type argument can be inferred; neither String nor Object are appropriate types for T, because the Collection element and the Sink element must be of the same type.&lt;br /&gt;&lt;br /&gt;We can fix this by modifying the signature of writeAll() as shown below, using a wildcard.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;writeAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;? &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;, &lt;font color="#2040a0"&gt;Sink&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;...&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;...&lt;br /&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;str&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;writeAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;cs&lt;/font&gt;, &lt;font color="#2040a0"&gt;s&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// call ok, but wrong return type&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The call is now legal, but the assignment is erroneous, since the return type inferred is Object because T matches the element type of s, which is Object.&lt;br /&gt;&lt;br /&gt;The solution is to use a form of bounded wildcard we haven’t seen yet: wildcards with a lower bound. The syntax ? super T denotes an unknown type that is a supertype of T. It is the dual of the bounded wildcards we’ve been using, where we use ? extends T to denote an unknown type that is a subtype of T.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;writeAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;coll&lt;/font&gt;, &lt;font color="#2040a0"&gt;Sink&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;? &lt;strong&gt;super&lt;/strong&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;snk&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;...&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;...&lt;br /&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;str&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;writeAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;cs&lt;/font&gt;, &lt;font color="#2040a0"&gt;s&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// Yes!&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Using this syntax, the call is legal, and the inferred type is String, as desired.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Wildcard Capture&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;Set&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;unknownSet&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;HashSet&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;&lt;font color="#444444"&gt;&lt;i&gt;/** Add an element t to a Set s */&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;addToSet&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Set&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;s&lt;/font&gt;, &lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;t&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;...&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;The&lt;/font&gt; &lt;font color="#2040a0"&gt;call&lt;/font&gt; &lt;font color="#2040a0"&gt;below&lt;/font&gt; &lt;font color="#2040a0"&gt;is&lt;/font&gt; &lt;font color="#2040a0"&gt;illegal&lt;/font&gt;.&lt;br /&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;addToSet&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;unknownSet&lt;/font&gt;, “&lt;font color="#2040a0"&gt;abc&lt;/font&gt;”&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// illegal&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It makes no difference that the actual set being passed is a set of strings; what matters is that the expression being passed as an argument is a set of an unknown type, which cannot be guaranteed to be a set of strings, or of any type in particular.&lt;br /&gt;&lt;br /&gt;Now, consider&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collections&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;...&lt;br /&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="#2040a0"&gt;Set&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;unmodifiableSet&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Set&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;set&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; ... &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;...&lt;br /&gt;&lt;font color="#2040a0"&gt;Set&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;?&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;s&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;Collections&lt;/font&gt;.&lt;font color="#2040a0"&gt;unmodifiableSet&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;unknownSet&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;font color="#444444"&gt;// this works! Why?&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It seems this should not be allowed; yet, looking at this specific call, it is perfectly safe to permit it. After all, unmodifiableSet() does work for any kind of Set, regardless of its element type.&lt;br /&gt;&lt;br /&gt;Because this situation arises relatively frequently, there is a special rule that allows such code under very specific circumstances in which the code can be proven to be safe. This rule, known as wildcard capture, allows the compiler to infer the unknown type of a wildcard as a type argument to a generic method.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Converting Legacy Code to Use Generics&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Let us look at “generifying” old code.&lt;br /&gt;&lt;br /&gt;If you decide to convert old code to use generics, you need to think carefully about how you modify the API.&lt;br /&gt;&lt;br /&gt;You need to make certain that the generic API is not unduly restrictive; it must continue to support the original contract of the API. Consider again some examples from java.util.Collection. &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#444444"&gt;//The pre-generic API looks like:&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collection&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;boolean&lt;/strong&gt; &lt;font color="#2040a0"&gt;containsAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;boolean&lt;/strong&gt; &lt;font color="#2040a0"&gt;addAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#444444"&gt;//A naive attempt to generify it is:&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;boolean&lt;/strong&gt; &lt;font color="#2040a0"&gt;containsAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;boolean&lt;/strong&gt; &lt;font color="#2040a0"&gt;addAll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;E&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;c&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;While this is certainly type safe, it doesn’t live up to the API’s original contract. The containsAll() method works with any kind of incoming collection. It will only succeed if the incoming collection really contains only instances of E, but:&lt;br /&gt;&lt;br /&gt;1. The static type of the incoming collection might differ, perhaps because the caller doesn’t know the precise type of the collection being passed in, or perhaps because it is a Collection&amp;lt;S&amp;gt;,where S is a subtype of E.&lt;br /&gt;&lt;br /&gt;2. It’s perfectly legitimate to call containsAll() with a collection of a different type. The routine should work, returning false.&lt;br /&gt;&lt;br /&gt;In the case of addAll(), we should be able to add any collection that consists of instances of a subtype of E. We already saw how to handle this situation correctly.&lt;br /&gt;&lt;br /&gt;You also need to ensure that the revised API retains binary compatibility with old clients. This implies that the erasure of the API must be the same as the original, ungenerified API. In most cases, this falls out naturally, but there are some subtle cases. We’ll examine one of the subtlest cases we’ve encountered, the method Collections.max(). &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#444444"&gt;//signature for max() is:&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Comparable&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;? &lt;strong&gt;super&lt;/strong&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;max&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;coll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This is fine, except that the erasure of this signature is&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="#2040a0"&gt;Comparable&lt;/font&gt; &lt;font color="#2040a0"&gt;max&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt; &lt;font color="#2040a0"&gt;coll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;which is different than the original signature of max():&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="#2040a0"&gt;max&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt; &lt;font color="#2040a0"&gt;coll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;One could certainly have specified this signature for max(), but it was not done, and all the old binary class files that call Collections.max() depend on a signature that returns Object.&lt;br /&gt;&lt;br /&gt;We can force the erasure to be different, by explicitly specifying a superclass in the bound for the formal type parameter T.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="4444FF"&gt;&amp;amp;&lt;/font&gt; &lt;font color="#2040a0"&gt;Comparable&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;? &lt;strong&gt;super&lt;/strong&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;max&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;coll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This is an example of giving multiple bounds for a type parameter, using the syntax T1&amp; T2 ... &amp; Tn. A type variable with multiple bounds is known to be a subtype of all of the types listed in the bound. When a multiple bound is used, the first type mentioned in the bound is used as the erasure of the type variable.&lt;br /&gt;&lt;br /&gt;Finally, we should recall that max only reads from its input collection, and so is applicable to collections of any subtype of T.&lt;br /&gt;&lt;br /&gt;This brings us to the actual signature used in the JDK:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Object&lt;/font&gt; &lt;font color="4444FF"&gt;&amp;amp;&lt;/font&gt; &lt;font color="#2040a0"&gt;Comparable&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;? &lt;strong&gt;super&lt;/strong&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="#2040a0"&gt;T&lt;/font&gt; &lt;font color="#2040a0"&gt;max&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collection&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;? &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;T&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;coll&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It’s very rare that anything so involved comes up in practice, but expert library designers should be prepared to think very carefully when converting existing APIs.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-893240845020245132?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/893240845020245132/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=893240845020245132' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/893240845020245132'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/893240845020245132'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/01/generics-in-java-programming-language.html' title='Generics in the Java Programming Language by Gilid Bracha'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-8085445805552725675</id><published>2009-01-18T23:44:00.000-08:00</published><updated>2009-01-18T23:45:36.533-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Visitor Pattern in Java'/><title type='text'>Visitor Pattern in Java</title><content type='html'>Visitor Pattern is a way of separating an algorithm from an object structure upon which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. Thus, using the visitor pattern helps conformance with the open/closed principle.&lt;br /&gt;&lt;br /&gt;The Visitor must visit each element of the Composite that functionally is in a Traverser object.&lt;br /&gt;&lt;br /&gt;Visitor is guided by the Traverser to gather state from all of the objects in the Composite.&lt;br /&gt;&lt;br /&gt;Once the state has been gathered, the Client can have the Visitor perform various operations on the state. When new functionality is required, only the Visitor must be enhanced.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;When to use Visitor Pattern:&lt;/strong&gt;&lt;br /&gt;1. Use Visitor Pattern when you want to add capabilities to a composite of objects and encapsulation is not important.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Visitor Benifits:&lt;/strong&gt;&lt;br /&gt;1. Allows you to add operations to a Composite structure without changing the structure iteself.&lt;br /&gt;2. Adding new operations is relatively easy.&lt;br /&gt;3. The code for operations performed by the Visitor is centralized.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Visitor Drawbacks:&lt;/strong&gt;&lt;br /&gt;1. Encapsulation of the Composite classes is broken when the Visitor is used.&lt;br /&gt;2. Because the traversal function is involved, changes to the Composite structure are more difficult.&lt;br /&gt;&lt;br /&gt;To implement the Visitor pattern, you create a Visitor interface for the visitor, and a Visitable interface for the collection to be visited. You then have concrete classes that implement the Visitor and Visitable interfaces.&lt;br /&gt;&lt;br /&gt;Let us look at an example...&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="#444444"&gt;// Visitor&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeatureVisitor&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;visit&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collections&lt;/font&gt; &lt;font color="#2040a0"&gt;collections&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;visit&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Generics&lt;/font&gt; &lt;font color="#2040a0"&gt;generics&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;visitJavaFeatures&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Java&lt;/font&gt; &lt;font color="#2040a0"&gt;java&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#444444"&gt;// Visitable&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeature&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;accept&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;JavaFeatureVisitor&lt;/font&gt; &lt;font color="#2040a0"&gt;visitor&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#444444"&gt;// Concrete Visitables&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collections&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeature&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;font color="#2040a0"&gt;Collections&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;name&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;getName&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;name&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;accept&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;JavaFeatureVisitor&lt;/font&gt; &lt;font color="#2040a0"&gt;visitor&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;visitor&lt;/font&gt;.&lt;font color="#2040a0"&gt;visit&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Generics&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeature&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;accept&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;JavaFeatureVisitor&lt;/font&gt; &lt;font color="#2040a0"&gt;visitor&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;visitor&lt;/font&gt;.&lt;font color="#2040a0"&gt;visit&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Java&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;font color="#2040a0"&gt;JavaFeature&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;javaFeatures&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeature&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;getJavaFeatures&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;javaFeatures&lt;/font&gt;.&lt;font color="#2040a0"&gt;clone&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;Java&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;javaFeatures&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeature&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;                                 &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collections&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot; List &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;, &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collections&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot; Set &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;,&lt;br /&gt;    &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Collections&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot; Map &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; ,&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Generics&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color="#444444"&gt;// Concrete Visitors&lt;/font&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeatureReadVisitor&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeatureVisitor&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;visit&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collections&lt;/font&gt; &lt;font color="#2040a0"&gt;collections&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;      &lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Going through &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt; &lt;font color="#2040a0"&gt;collections&lt;/font&gt;.&lt;font color="#2040a0"&gt;getName&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;font color="4444FF"&gt;+&lt;/font&gt; &lt;font color="#008000"&gt;&amp;quot; Collections&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;visit&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Generics&lt;/font&gt; &lt;font color="#2040a0"&gt;generics&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Going through  generics&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;visitJavaFeatures&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Java&lt;/font&gt; &lt;font color="#2040a0"&gt;java&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;&lt;font color="#77dd77"&gt;\n&lt;/font&gt;Learning Java&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;for&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;JavaFeature&lt;/font&gt; &lt;font color="#2040a0"&gt;javaFeature&lt;/font&gt; &lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;font color="#2040a0"&gt;java&lt;/font&gt;.&lt;font color="#2040a0"&gt;getJavaFeatures&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;javaFeature&lt;/font&gt;.&lt;font color="#2040a0"&gt;accept&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Got an Idea of Java Language&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeatureImplVisitor&lt;/font&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeatureVisitor&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;visit&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Collections&lt;/font&gt; &lt;font color="#2040a0"&gt;collections&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;using Collections &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt; &lt;font color="#2040a0"&gt;collections&lt;/font&gt;.&lt;font color="#2040a0"&gt;getName&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;visit&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Generics&lt;/font&gt; &lt;font color="#2040a0"&gt;generics&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;using Generics&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;visitJavaFeatures&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;Java&lt;/font&gt; &lt;font color="#2040a0"&gt;java&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;&lt;font color="#77dd77"&gt;\n&lt;/font&gt;Implementing Java Features&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;for&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;JavaFeature&lt;/font&gt; &lt;font color="#2040a0"&gt;javaFeature&lt;/font&gt; &lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;font color="#2040a0"&gt;java&lt;/font&gt;.&lt;font color="#2040a0"&gt;getJavaFeatures&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;javaFeature&lt;/font&gt;.&lt;font color="#2040a0"&gt;accept&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Very useful features&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;VisitorDemo&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;main&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;args&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;Java&lt;/font&gt; &lt;font color="#2040a0"&gt;java&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;Java&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;JavaFeatureVisitor&lt;/font&gt; &lt;font color="#2040a0"&gt;learnJavaFeature&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeatureReadVisitor&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;JavaFeatureVisitor&lt;/font&gt; &lt;font color="#2040a0"&gt;implJavaFeature&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;JavaFeatureImplVisitor&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;learnJavaFeature&lt;/font&gt;.&lt;font color="#2040a0"&gt;visitJavaFeatures&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;java&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;implJavaFeature&lt;/font&gt;.&lt;font color="#2040a0"&gt;visitJavaFeatures&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;java&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-8085445805552725675?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/8085445805552725675/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=8085445805552725675' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/8085445805552725675'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/8085445805552725675'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/01/visitor-pattern-in-java.html' title='Visitor Pattern in Java'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-6593997583043044090</id><published>2009-01-17T14:29:00.000-08:00</published><updated>2009-01-17T14:31:32.595-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Flyweight Patternin Java'/><title type='text'>Flyweight Patternin Java</title><content type='html'>A Flyweight is an object that minimizes memory occupation by sharing as much data as possible with other similar objects.&lt;br /&gt;&lt;br /&gt;The reusable and variable parts of a class are broken into two classes to save resources.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Problem:&lt;/strong&gt;&lt;br /&gt;Designing objects down to the lowest levels of system "granularity" provides optimal flexibility,but can be unacceptably expensive in terms of performance and memory usgae.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Structure Summary:&lt;/strong&gt;&lt;br /&gt;1) Choose a class from which so many instances will be created that performance will suffer.&lt;br /&gt;2) Identify the state associated with the class that will not vary from one instance to another,and the state that is peculiar to each individual instance. &lt;br /&gt;3) State that is peculiar i.e cannot be shared will be maintained and supplied by client.&lt;br /&gt;4) Hundreds of objects can now be exercised by caching and reusing a few "flyweight" objects.&lt;br /&gt;&lt;br /&gt;Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost.Each "Flyweight" object is divided into 2 pieces.&lt;br /&gt;The state dependent(extrensic) part and the state-independent(intrinsic) part.&lt;br /&gt;&lt;br /&gt;Intrinsic state is stored(shared) in the Flyweight object.&lt;br /&gt;Extrensic state is stored or computed by client objects,and passed to the Flyweight when its operations are invoked.&lt;br /&gt;&lt;br /&gt;Flyweights are shared objects and that using them can result in substantial performance gains.&lt;br /&gt;Flyweights are typically instantiated by a flyweight factory that creates a limited number of flyweights and doles them out,one at a time to clients.&lt;br /&gt;&lt;br /&gt;For example, you might have a pool of line objects that know how to draw lines. In that case, the flyweight factory could create one line object for each line color, such as one object for white lines and another for blue lines.&lt;br /&gt;Those lines, which are flyweights, get reused whenever you draw white or blue lines. &lt;br /&gt;If you have a drawing with 1,000 white lines and 6,000 blue lines, only two lines—instead of 7,000—are actually instantiated.&lt;br /&gt;&lt;br /&gt;In java strings specified at compile-time are flyweights—strings that contain the same character sequence are shared. That sharing can greatly reduce memory footprints, and therefore, increase performance. Strings computed at runtime, are not flyweights by default; however, you can force the issue with the String.intern() which returns flyweights for strings computed at runtime. &lt;br /&gt;&lt;br /&gt;Flyweights are stored in a Factory's repository.&lt;br /&gt;The client restrains themselves from creating Flyweighs directly,and requests them from the Factory.&lt;br /&gt;Each Flyweight cannot stand on its own.&lt;br /&gt;Any Attributes that would make sharing impossible must be supplied by the client whenever a request is made of the Flyweight.&lt;br /&gt;If the context lends itself to "economy of scale"(i.e the client can easily compute or look-up the necessary attributes),then the Flyweight pattern offers appropriate leverage.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;CheckList:&lt;/strong&gt;&lt;br /&gt;1) Ensure the object overhead is an issue needing attention,and the client of the class is able and willing to absorb responsibility realignment.&lt;br /&gt;2) Divide the target class's state into: shareable(intrinsic) state,and non-shareable(extrensic) state.&lt;br /&gt;3) Remove the non-shareable state from the class attributes,and add it to the calling argument list of affected methods.&lt;br /&gt;4) Create a factory that can cache and reuse existing class instances.&lt;br /&gt;5) The client must use the Factory instead of the new operator to request objects.&lt;br /&gt;6) The client(or a third party) must look-up or compute the non-shareable state,and supply that state to class methods.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Flyweight Considerations:&lt;/strong&gt;&lt;br /&gt;The effectiveness of Flyweight pattern as a caching mechanism depends heavily on certain characteristics of the data we are caching.&lt;br /&gt;&lt;br /&gt;-&gt; Application uses a large number of objects.&lt;br /&gt;-&gt; Storage(Memory) Cost is high to replicate this large number of multiple users.&lt;br /&gt;-&gt; Either the objects are immutable or their state can be made external.&lt;br /&gt;-&gt; Relatively few shared objects may replace many groups of objects.&lt;br /&gt;-&gt; Application does not depend on object identity.While the user may think they are getting a unique object, they actually have a reference from the cache. &lt;br /&gt;&lt;br /&gt;The flyweight design pattern is not recommended when the objects in the cache change rapidly or unexpectedly.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Rules Of Thumb:&lt;/strong&gt;&lt;br /&gt;Whereas flyweight shows how to make lots of little objects,Facade shows how to make a single object represent an entire subsystem.[GoF,p138]&lt;br /&gt;&lt;br /&gt;Flyweight is often combined with Composite to implement shared leaf nodes.[GoF,p206]&lt;br /&gt;&lt;br /&gt;Terminal symbols within Interpreter's abstract syntax tree can be shared with Flyweight. [GoF. p255]&lt;br /&gt;&lt;br /&gt;Flyweights explain when and how State objects can be shared.[GoF,p313]&lt;br /&gt;&lt;br /&gt;Below is an example implemented using "FlyWeight" Pattern to book admissions in a school.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;AdmissionContext&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;std&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;AdmissionContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;std&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;std&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;std&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;  &lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;getStd&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;std&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Admission&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;admitStudent&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;AdmissionContext&lt;/font&gt; &lt;font color="#2040a0"&gt;admissionContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Admission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;admissionType&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;null&lt;/strong&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;admissionType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;admissionType&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;admissionType&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;getAdmissionType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;admissionType&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;admitStudent&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;AdmissionContext&lt;/font&gt; &lt;font color="#2040a0"&gt;admissionContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Admissions type = &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;admissionType&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot; for std &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;admissionContext&lt;/font&gt;.&lt;font color="#2040a0"&gt;getStd&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmissionFactory&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;admissions&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;10&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;seats&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt; &lt;font color="#2040a0"&gt;getGeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;admissionType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;if&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;seats&lt;/font&gt; &lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;strong&gt;for&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="4444FF"&gt;=&lt;/font&gt;&lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;seats&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;    &lt;strong&gt;if&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;admissionType&lt;/font&gt;.&lt;font color="#2040a0"&gt;equals&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;admissions&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;getAdmissionType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;     &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;admissions&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;admissions&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;seats&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;admissionType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;admissions&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;seats&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;getAdmissions&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;admissions&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setAdmissions&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;admissions&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;admissions&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;admissions&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;getSeats&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;seats&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setSeats&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;seats&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;seats&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;seats&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;FlightweightPatternDemo&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;generalAdmissions&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;20&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="#2040a0"&gt;AdmissionContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;admissionContexts&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;AdmissionContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;20&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;seatRequest&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmissionFactory&lt;/font&gt; &lt;font color="#2040a0"&gt;generalAdmissionFactory&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;apply&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;std&lt;/font&gt;, &lt;font color="#2040a0"&gt;String&lt;/font&gt; &lt;font color="#2040a0"&gt;admissionType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;generalAdmissions&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;seatRequest&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;generalAdmissionFactory&lt;/font&gt;.&lt;font color="#2040a0"&gt;getGeneralAdmission&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;admissionType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;admissionContexts&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;seatRequest&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;AdmissionContext&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;std&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;main&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;args&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;generalAdmissionFactory&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;GeneralAdmissionFactory&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;apply&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;2&lt;/font&gt;,&lt;font color="#008000"&gt;&amp;quot;General&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;apply&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#FF0000"&gt;5&lt;/font&gt;,&lt;font color="#008000"&gt;&amp;quot;VIP&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;strong&gt;for&lt;/strong&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;int&lt;/strong&gt; &lt;font color="#2040a0"&gt;i&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#FF0000"&gt;0&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;seatRequest&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;generalAdmissions&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;.&lt;font color="#2040a0"&gt;admitStudent&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;admissionContexts&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;i&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;Total seat Requests = &amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;+&lt;/font&gt;&lt;font color="#2040a0"&gt;generalAdmissionFactory&lt;/font&gt;.&lt;font color="#2040a0"&gt;getSeats&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;When to use Flightweight Pattern:&lt;/strong&gt;&lt;br /&gt;1. Use the Flightweight Pattern when one instance of a class can be used to provide many virtual instances.&lt;br /&gt;2. Flightweight Pattern is used when a class has many instances, and they all can be controlled identically.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Benifits of Flightweight Pattern:&lt;/strong&gt;&lt;br /&gt;1. Reduces the number of object instances at runtime saving memory.&lt;br /&gt;2. Centralizes state for many virtual objects into a single location.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Drawbacks of Flightweight Pattern:&lt;/strong&gt;&lt;br /&gt;1. Drawback is that once you implemented it, single logical instances of the class will not be able to behave independently from the other instances.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Related Patterns:&lt;/strong&gt;&lt;br /&gt;Abstract Factory,Singleton and Template Method patterns fall under this category.Flyweight pattern is often combined with the Composite Pattern to implement a logically hierarchical structure in terms of a directed-acyclic graph with shared lead nodes.Flyweight is a strategy in which you keep a pool of objects available and create references into the pool of objects for particular views.It uses the idea of canonical objects.A canonical object is a single representative object that represents all other objects for particular type. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Hope it will be useful...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5210551511410992690-6593997583043044090?l=snehaprashant.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://snehaprashant.blogspot.com/feeds/6593997583043044090/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5210551511410992690&amp;postID=6593997583043044090' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/6593997583043044090'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5210551511410992690/posts/default/6593997583043044090'/><link rel='alternate' type='text/html' href='http://snehaprashant.blogspot.com/2009/01/flyweight-patternin-java.html' title='Flyweight Patternin Java'/><author><name>sneha prashant</name><uri>http://www.blogger.com/profile/15279504922041176105</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5210551511410992690.post-38127394629037680</id><published>2009-01-17T14:25:00.000-08:00</published><updated>2009-01-17T14:28:50.103-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Bridge Pattern in Java'/><title type='text'>Bridge Pattern in Java</title><content type='html'>The Bridge Pattern allows you to vary the implementation and abstraction by placing the two in seperate hierarchies.&lt;br /&gt;&lt;br /&gt;Decouple an abstraction or interface from its implementation so that the two can vary independently. &lt;br /&gt;&lt;br /&gt;The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Abstraction&lt;/strong&gt; &lt;br /&gt;1. defines the abstract interface &lt;br /&gt;2. maintains the Implementor reference &lt;br /&gt;&lt;strong&gt;Refined Abstraction &lt;/strong&gt;&lt;br /&gt;1. extends the interface defined by Abstraction &lt;br /&gt;&lt;strong&gt;Implementor&lt;/strong&gt; &lt;br /&gt;1. defines the interface for implementation classes &lt;br /&gt;&lt;strong&gt;ConcreteImplementor &lt;/strong&gt;&lt;br /&gt;1. implements the Implementor interface &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Uses and Benefits :&lt;/strong&gt;&lt;br /&gt;1. Want to separate abstraction and implementation permanently &lt;br /&gt;2. Share an implementation among multiple objects &lt;br /&gt;3. Want to improve extensibility &lt;br /&gt;4. Hide implementation details from clients &lt;br /&gt;&lt;br /&gt;Bridge might be a situation where the programmer thought it would be best to isolate the handling of the system-dependent stuff from the handling of the system-independent stuff.&lt;br /&gt;&lt;br /&gt;The collections class framework in the Java API provides several examples of use of the bridge pattern. Both the ArrayList and LinkedList concrete classes implement the List interface. The List interface provides common, abstract concepts, such as the abilities to add to a list and to ask for its size. The implementation details vary between ArrayList and LinkedList, mostly with respect to when memory is allocated for elements in the list.&lt;br /&gt;&lt;br /&gt;Your application can take advantage of this common interface: Wherever you would have a reference variable of either ArrayList or LinkedList type, replace the variable's type with the List interface type:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;CarTypeAssembler&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;assembleCarType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;CarType&lt;/font&gt; &lt;font color="#2040a0"&gt;src&lt;/font&gt;,&lt;font color="#2040a0"&gt;CarTypeTO&lt;/font&gt; &lt;font color="#2040a0"&gt;dest&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;dest&lt;/font&gt;.&lt;font color="#2040a0"&gt;setId&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;src&lt;/font&gt;.&lt;font color="#2040a0"&gt;getId&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;dest&lt;/font&gt;.&lt;font color="#2040a0"&gt;setDescription&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;src&lt;/font&gt;.&lt;font color="#2040a0"&gt;getDescription&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;dest&lt;/font&gt;.&lt;font color="#2040a0"&gt;setCarImageUrl&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;src&lt;/font&gt;.&lt;font color="#2040a0"&gt;getCarImageUrl&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;dest&lt;/font&gt;.&lt;font color="#2040a0"&gt;setLuggageCapacity&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;src&lt;/font&gt;.&lt;font color="#2040a0"&gt;getLuggageCapacity&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;dest&lt;/font&gt;.&lt;font color="#2040a0"&gt;setSeatingCapacity&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;src&lt;/font&gt;.&lt;font color="#2040a0"&gt;getSeatingCapacity&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;assembleCarType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;CarType&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;src&lt;/font&gt;,&lt;font color="#2040a0"&gt;List&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#2040a0"&gt;CarTypeTO&lt;/font&gt;&lt;font color="4444FF"&gt;&amp;gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;dest&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;for&lt;/strong&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;CarType&lt;/font&gt; &lt;font color="#2040a0"&gt;carType&lt;/font&gt; &lt;font color="4444FF"&gt;:&lt;/font&gt; &lt;font color="#2040a0"&gt;src&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;CarTypeTO&lt;/font&gt; &lt;font color="#2040a0"&gt;carTypeTO&lt;/font&gt;  &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;CarTypeTO&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;   &lt;font color="#2040a0"&gt;assembleCarType&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;carType&lt;/font&gt;,&lt;font color="#2040a0"&gt;carTypeTO&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;   &lt;br /&gt;   &lt;font color="#2040a0"&gt;dest&lt;/font&gt;.&lt;font color="#2040a0"&gt;add&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;carTypeTO&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The logic in the assembleCarType method doesn't care if the list of CarType is implemented as an array or a linked list. This use of the bridge pattern improves the maintainability of your code. If all of your code is structured this way, you can change your code from using an ArrayList to a LinkedList, or vice versa, in one place.&lt;br /&gt;&lt;br /&gt;Now let us look at one more implementation of Bridge Pattern:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;Furniture&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;FurnitureImp&lt;/font&gt; &lt;font color="#2040a0"&gt;furnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;FurnitureImp&lt;/font&gt; &lt;font color="#2040a0"&gt;getInstance&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;furnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;setFurnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;furnitureImp&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;FurnitureImpSingleton&lt;/font&gt;.&lt;font color="#2040a0"&gt;getInstance&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;buyFurniture&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;FurnitureImpSingleton&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;private&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="#2040a0"&gt;FurnitureImp&lt;/font&gt; &lt;font color="#2040a0"&gt;furnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;FurnitureImpSingleton&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;FurnitureImp&lt;/font&gt; &lt;font color="#2040a0"&gt;furImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;furnitureImp&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;font color="#2040a0"&gt;furImp&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;font color="#2040a0"&gt;FurnitureImp&lt;/font&gt; &lt;font color="#2040a0"&gt;getInstance&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;return&lt;/strong&gt; &lt;font color="#2040a0"&gt;furnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;FurnitureImp&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;buyFurnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;TelevisionImp&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;FurnitureImp&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;TelevisionImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;buyFurnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;bought Television...&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;RefrigeratorImp&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;FurnitureImp&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;font color="#2040a0"&gt;RefrigeratorImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;buyFurnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;System&lt;/font&gt;.&lt;font color="#2040a0"&gt;out&lt;/font&gt;.&lt;font color="#2040a0"&gt;println&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;quot;bought Refrigerator...&amp;quot;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt; &lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;MoreFurniture&lt;/font&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;font color="#2040a0"&gt;Furniture&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;font color="#2040a0"&gt;MoreFurniture&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;setFurnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;buyFurniture&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;  &lt;font color="#2040a0"&gt;FurnitureImp&lt;/font&gt; &lt;font color="#2040a0"&gt;furnitureImp&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;this&lt;/strong&gt;.&lt;font color="#2040a0"&gt;getInstance&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;furnitureImp&lt;/font&gt;.&lt;font color="#2040a0"&gt;buyFurnitureImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;font color="#2040a0"&gt;BridgePatternExample&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt; &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;font color="#2040a0"&gt;main&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="#2040a0"&gt;String&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt; &lt;font color="#2040a0"&gt;args&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;font color="4444FF"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;br /&gt;  &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;FurnitureImpSingleton&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;TelevisionImp&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;&lt;br /&gt;  &lt;font color="#2040a0"&gt;MoreFurniture&lt;/font&gt; &lt;font color="#2040a0"&gt;moreFurniture&lt;/font&gt; &lt;font color="4444FF"&gt;=&lt;/font&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;font color="#2040a0"&gt;MoreFurniture&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;&lt;font color="4444FF"&gt;;&lt;/font&gt;   &lt;br /&gt;  &lt;font color="#2040a0"&gt;moreFurniture&lt;/font&gt;.&lt;font color="#2040a0"&gt;buyFurniture&lt;/font&gt;&lt;font color="4444FF"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;
