<?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-1312262910397222081</id><updated>2011-12-10T05:16:21.853+05:30</updated><title type='text'>Naman</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>10</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-5375042450504108735</id><published>2011-01-20T14:50:00.003+05:30</published><updated>2011-01-20T14:53:23.624+05:30</updated><title type='text'>What is Cloud Computing?</title><content type='html'>&lt;iframe width="420" height="258" src="http://www.youtube.com/embed/ae_DKNwK_ms?fs=1" frameborder="0" allowFullScreen=""&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-5375042450504108735?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/5375042450504108735/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2011/01/what-is-cloud-computing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/5375042450504108735'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/5375042450504108735'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2011/01/what-is-cloud-computing.html' title='What is Cloud Computing?'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://img.youtube.com/vi/ae_DKNwK_ms/default.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-4738929002710846890</id><published>2011-01-18T16:07:00.003+05:30</published><updated>2011-01-18T16:09:53.121+05:30</updated><title type='text'>Cloud and Multitenant Architecture and thier Usage</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Cloud and Multitenant Architecture and thier Usage&lt;br /&gt;&lt;br /&gt;Looks like there is a confusion or misperception on how Cloud and Multitenant things work together. One thing we need to understand that if I develop any application which can be deployed in cloud doesn’t automatically means that the application is multitenant. At the same time if I develop any application which is multitenant doesn’t automatically means that it can be deployed on cloud.&lt;br /&gt;&lt;br /&gt;So first lets understand what exactly these two things means before going into how these two complements each other.&lt;br /&gt;&lt;br /&gt;Cloud: Cloud provides a readymade infrastructure to host your application along with the ability to increase / decrease the hardware resource requirement based on the requirement. Also there are multiple vendors providing this infrastructure.&lt;br /&gt;&lt;br /&gt;Some of these providers are:&lt;br /&gt;&lt;br /&gt;    * Microsoft Azure&lt;br /&gt;    * Google Apps&lt;br /&gt;    * Force Platform (from Salesforce)&lt;br /&gt;    * …&lt;br /&gt;&lt;br /&gt;Now many of these platforms provide good support for specific languages. &lt;br /&gt;&lt;br /&gt;Multitenant Architecture:  Multitenant architecture is needed when we develop any software to handle a domain requirement which is applicable to multiple customers. In that situation the configurability of the software is more important than customizing of the software (in some cases it cannot be avoided but needs to be handled differently). Majority of the time a single codebase provides services to all the customers. And at application and data storage level, care is taken to make sure that data from Customer A is fully secured from Data from Customer B. Also if there is requirement for any customization for a particular customer, there are patterns to handle this.&lt;br /&gt;&lt;br /&gt;Some of the examples of Multitenant Applications:&lt;br /&gt;&lt;br /&gt;    * SharePoint Online&lt;br /&gt;    * 37 Signals Applications&lt;br /&gt;    * Accompa for Requirements&lt;br /&gt;    * …&lt;br /&gt;&lt;br /&gt;As far as Multitenant Architecture is concerned, this itself is a BIG topic for which we will find many resources on the web which talk about multitenancy at database level, code level etc. &lt;br /&gt;&lt;br /&gt;Usage: Now the question is how these two complements each other. Now we understand that multitenant architecture is to support multiple customers with the single codebase and data structure. Which means that it doesn’t matter if the customer count grows from 10 to 100 as our application can support it any time. However the questions is what about hardware infrastructure. This is where the elastic nature of the cloud comes into the picture and provide the necessary infrastructure which is required for scaling of the business.&lt;br /&gt;&lt;br /&gt;E.g. Just by changing the configuration file we can change the number of instance of our application hosted in the cloud. At the same time if the business is not doing good we can very well reduce the resource usage and pay as per the requirement. &lt;br /&gt;&lt;br /&gt;In cloud computing, multi-tenant is the phrase used to describe multiple customers using the same public cloud. &lt;br /&gt;&lt;br /&gt;The idea of this blog is not to explain what is the best approach to achieve multitenancy.And make sure that we understand the difference between Multitenancy and Cloud and how they can used to get best of that.&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-4738929002710846890?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/4738929002710846890/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2011/01/cloud-and-multitenant-architecture-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/4738929002710846890'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/4738929002710846890'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2011/01/cloud-and-multitenant-architecture-and.html' title='Cloud and Multitenant Architecture and thier Usage'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-3945149772563383732</id><published>2010-04-07T14:30:00.003+05:30</published><updated>2010-04-07T14:34:22.070+05:30</updated><title type='text'>HotSpot JVM Options</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;To learn more about the HotSpot JVM options – &lt;/span&gt;&lt;br /&gt;&lt;a style="color: rgb(0, 0, 153);" href="http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;I found a great resource that lists various JVM versions and the corresponding options -&lt;/span&gt;&lt;br /&gt;&lt;a style="color: rgb(0, 0, 153);" href="http://blogs.sun.com/watt/resource/jvm-options-list.html"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;http://blogs.sun.com/watt/resource/jvm-options-list.html&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-3945149772563383732?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/3945149772563383732/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2010/04/hotspot-jvm-options.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/3945149772563383732'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/3945149772563383732'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2010/04/hotspot-jvm-options.html' title='HotSpot JVM Options'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-364954096218635776</id><published>2010-01-12T14:42:00.003+05:30</published><updated>2010-01-12T15:01:44.734+05:30</updated><title type='text'>Interface injection using Spring</title><content type='html'>&lt;div style="text-align: justify; color: rgb(0, 0, 0);"&gt;I am adding one more sample for Interface Injection using spring. Spring directly does not support interface injection but there is one thing called &lt;span style="font-weight: bold;"&gt;autowire&lt;/span&gt;, that can be used in spring to achieve some sort of interface injection.&lt;br /&gt;&lt;br /&gt;Consider below sample Java classes:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;1. Text.java - interface which has getter and setter for String object&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public interface Text {&lt;br /&gt;   void setTextString(String textString);&lt;br /&gt;   String getTextString();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2. MyText.java - implements above Text interface&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public class MyText implements Text {&lt;br /&gt;&lt;br /&gt;   private String textString;&lt;br /&gt;&lt;br /&gt;   public String getTextString() {&lt;br /&gt;       return textString;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void setTextString(String textString) {&lt;br /&gt;       this.textString = textString;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;3. Service.java - which has getter and setter for above Text object&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public interface Service {&lt;br /&gt;   void setText(Text text);&lt;br /&gt;   Text getText();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;4. MyService.java - implements above Service Interface&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public class MyService implements Service {&lt;br /&gt;   private Text text;&lt;br /&gt; &lt;br /&gt;   public Text getText() {&lt;br /&gt;       return text;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void setText(Text text) {&lt;br /&gt;       this.text = text;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;5. MyServiceApp.java - which has main method to test interface injection&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;import org.springframework.beans.factory.xml.XmlBeanFactory;&lt;br /&gt;import org.springframework.core.io.ClassPathResource;&lt;br /&gt;import static java.lang.System.out;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class MyServiceApp {&lt;br /&gt;&lt;br /&gt;   public static void main(String...strings) {&lt;br /&gt;       XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(&lt;br /&gt;               "myservice.xml"));&lt;br /&gt;       MyService myService = (MyService) beanFactory.getBean("myService");&lt;br /&gt;       out.println( myService.getText().getTextString());&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Now, in above sample to get interface injection major  file is configuration file(myservice.xml).&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Look at below configuration&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Configuration file: myservice.xml&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"&lt;br /&gt;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;xmlns:aop="http://www.springframework.org/schema/aop"&lt;br /&gt;xmlns:p="http://www.springframework.org/schema/p"&lt;br /&gt;xmlns:tx="http://www.springframework.org/schema/tx"&lt;br /&gt;xsi:schemaLocation="http://www.springframework.org/schema/beans&lt;br /&gt;http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&lt;br /&gt;http://www.springframework.org/schema/aop&lt;br /&gt;http://www.springframework.org/schema/aop/spring-aop-2.5.xsd&lt;br /&gt;http://www.springframework.org/schema/tx&lt;br /&gt;http://www.springframework.org/schema/tx/spring-tx.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;bean id="text" class="MyText" p:textString="My First String...!"/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!-- &amp;lt;bean id="myService" class="MyService" p:text-ref="text"/&amp;gt; --&amp;gt;&lt;br /&gt;&amp;lt;!-- The above can be used if you want to inject the dependency yourself--&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!-- The below can be used to leave the dependency injection decision to the IoC container --&amp;gt;&lt;br /&gt;&amp;lt;!--  &amp;lt;bean id="myService" class="MyService" autowire="byName"/&amp;gt; --&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!-- The following line is also almost same as the above except for this case IoC will&lt;br /&gt;detect dependency by type --&amp;gt;&lt;br /&gt;&amp;lt;bean id="myService" class="MyService" autowire="byType"/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;In above sample you can configure interface injection by two ways:&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span style="font-weight: bold;"&gt;1. &amp;lt;bean id="myService" class="MyService" p:text-ref="text"/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Here, injection is done by yourself by giving reference to another bean object.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span style="font-weight: bold;"&gt;2. &amp;lt;bean id="myService" class="MyService" autowire="byName"/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Here, injection decision is taken by IoC container as you have specified autowire attribute. autowire value can be set as byName or byType.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-364954096218635776?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/364954096218635776/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2010/01/interface.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/364954096218635776'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/364954096218635776'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2010/01/interface.html' title='Interface injection using Spring'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-3191048954052630892</id><published>2010-01-07T14:30:00.004+05:30</published><updated>2010-01-07T14:47:38.302+05:30</updated><title type='text'>Constructor Injection using Spring</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;I wrote about Spring &amp;amp; Setter Injection in my previous &lt;a href="http://namanmehta.blogspot.com/2009/12/simple-spring-application.html"&gt;blog&lt;/a&gt; now I am adding one more sample using constructor Injection. Constructor injection is almost same like setter injection except only difference that here IoC container initializes the constructor parameters.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;Consider below sample java class.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;Java Class: TestConstructor.java&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import org.springframework.context.ApplicationContext;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import org.springframework.context.support.ClassPathXmlApplicationContext;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;public class TestConstructor {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public static void main(String[] args) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"constructorinjection.xml"});&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        Inject inject = (Inject) context.getBean( "myInject" );&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        System.out.println( inject.getName() );&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;class Inject {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    private String name;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;   &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public Inject(String name, String text)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        this.name = name + text;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public String getName() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        return this.name;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }  &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;In above class constructor parameter values(name &amp;amp; text) would be injected from constructorinjection.xml configuration file.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;Configuration file: constructorinjection.xml&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"&lt;br /&gt;  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;  xmlns:aop="http://www.springframework.org/schema/aop"&lt;br /&gt;  xmlns:p="http://www.springframework.org/schema/p"&lt;br /&gt;  xmlns:tx="http://www.springframework.org/schema/tx"&lt;br /&gt;  xsi:schemaLocation="http://www.springframework.org/schema/beans&lt;br /&gt;  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&lt;br /&gt;  http://www.springframework.org/schema/aop&lt;br /&gt;  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd&lt;br /&gt;  http://www.springframework.org/schema/tx&lt;br /&gt;  http://www.springframework.org/schema/tx/spring-tx.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;bean id="myInject" class="Inject"&amp;gt;&lt;br /&gt;  &amp;lt;constructor-arg type="java.lang.String" index="1"&amp;gt;&lt;br /&gt;      &amp;lt;value&amp;gt;Welcome to example to constructor injection &amp;lt;/value&amp;gt;&lt;br /&gt;  &amp;lt;/constructor-arg&amp;gt;&lt;br /&gt;  &amp;lt;constructor-arg type="java.lang.String" index="0"&amp;gt;&lt;br /&gt;      &amp;lt;value&amp;gt;A text &amp;lt;/value&amp;gt;&lt;br /&gt;  &amp;lt;/constructor-arg&amp;gt;&lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Here, &lt;constructor-arg&gt;&lt;span style="font-weight: bold;"&gt;&amp;lt;/constructor-arg&amp;gt; has two optional attributes type and index&lt;/span&gt;. Type is not significant in this particular example but can be useful when using some other complex types. Specify type value based on your constructor type. Index is also one useful mechanism to be used within. Based on index value it assigns value to constructor parameter.&lt;/constructor-arg&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;In above sampe output is:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;A text Welcome to example to constructor injection &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;Now if you change index vaue 1 to 0 and 0 to 1 output is:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Welcome to example to constructor injection A text&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-weight: bold;"&gt;If you have noticed&lt;/span&gt; I made one more change in this sample compare to my previous blog. I used ApplicationContext instead of BeanFactory. What is the difference? We will now see why ApplicationContext.&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;The first thing you can notice is that you can (of course optionally) pass more than one XML bean definition/configuration files. Ultimately we are assigning the ApplicatioContetx instance to BeanFactory.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-3191048954052630892?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/3191048954052630892/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2010/01/constructor-injection-using-spring.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/3191048954052630892'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/3191048954052630892'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2010/01/constructor-injection-using-spring.html' title='Constructor Injection using Spring'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-5990069785118644284</id><published>2010-01-06T14:03:00.003+05:30</published><updated>2010-01-06T14:10:55.897+05:30</updated><title type='text'>Use CodeModel to generate Java Source Code</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-weight: bold;"&gt;CodeModel&lt;/span&gt; is a library that allows you to generate Java source code in a type-safe fashion. &lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&lt;br /&gt;When to use CodeModel:&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;If you have huge chunk of data in terms of text file or XML and want to generate some Java class based on those data you can use CodeModel. It also helps when your data changes frequently.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;&lt;br /&gt;To know more about the CodeModel visit following link:&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;a href="http://fisheye5.atlassian.com/browse/%7Eraw,r=1.601/jaxb-architecture-document/www/doc/com/sun/codemodel/package-summary.html"&gt;&lt;span&gt;http://fisheye5.atlassian.com/browse/~raw,r=1.601/jaxb-architecture-document/www/doc/com/sun/codemodel/package-summary.html&lt;/span&gt;&lt;/a&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;Giving you small sample how to use CodeModel to generate your own class.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;Example: CodeFactory.java&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JAnnotationUse;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JBlock;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JClass;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JCodeModel;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JDefinedClass;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JDocComment;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JExpr;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JMethod;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JMod;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JPackage;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JType;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.sun.codemodel.JVar;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/**&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; *&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * @author naman&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;public class CodeFactory {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    // Method to get JType based on any String Value&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public JType getTypeDetailsForCodeModel(JCodeModel jCodeModel, String type) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        if (type.equals("Unsigned32")) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            return jCodeModel.LONG;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        } else if (type.equals("Unsigned64")) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            return jCodeModel.LONG;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        } else if (type.equals("Integer32")) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            return jCodeModel.INT;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        } else if (type.equals("Integer64")) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            return jCodeModel.LONG;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        } else if (type.equals("Enumerated")) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            return jCodeModel.INT;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        } else if (type.equals("Float32")) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            return jCodeModel.FLOAT;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        } else if (type.equals("Float64")) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            return jCodeModel.DOUBLE;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        } else {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            return null;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    // Function to generate CodeModel Class&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public void writeCodeModel(String factroyPackage) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        try {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Creating java code model classes */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            JCodeModel jCodeModel = new JCodeModel();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Adding packages here */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            JPackage jp = jCodeModel._package(factroyPackage);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Giving Class Name to Generate */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            JDefinedClass jc = jp._class("GeneratedFactory");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Adding annotation for the Class */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            jc.annotate(com.myannotation.AnyXYZ.class);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Adding class level coment */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            JDocComment jDocComment = jc.javadoc();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            jDocComment.add("Class Level Java Docs");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Adding method to the Class which is public static and returns com.somclass.AnyXYZ.class */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            String mehtodName = "myFirstMehtod";&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            JMethod jmCreate = jc.method(JMod.PUBLIC | JMod.STATIC, com.somclass.AnyXYZ.class, "create" + mehtodName);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Addign java doc for method */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            jmCreate.javadoc().add("Method Level Java Docs");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Adding method body */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            JBlock jBlock = jmCreate.body();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Defining method parameter */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;             JType jt = getTypeDetailsForCodeModel(jCodeModel, "Unsigned32");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;             if (jt != null) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                 jmCreate.param(jt, "data");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;             } else {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                jmCreate.param(java.lang.String.class, "data");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;             }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Defining some class Variable in mthod body */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            JClass jClassavpImpl = jCodeModel.ref(com.somclass.AnyXYZ.class);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            jvarAvpImpl = jBlock.decl(jClassavpImpl, "varName");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            jvarAvpImpl.init(JExpr._new(jClassavpImpl));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Adding some direct statement */             &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            jBlock.directStatement("varName.setCode(100);");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* returning varibalbe */        &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            jBlock._return(jvarAvpImpl);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            /* Building class at given location */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            jCodeModel.build(new File("generated/src"));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        } catch (JAXBException ex) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            logger.log(Level.SEVERE, "JAXBException:" + ex);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ex.printStackTrace();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        } catch (Exception ex) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            logger.log(Level.SEVERE, "Other Exception which in not catched:" + ex);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;            ex.printStackTrace();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }   &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    // Wirte main mehtod and call writeCodeModel("com.test") function to generate class &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;After running above class &lt;span style="font-weight: bold;"&gt;it generates GeneratedFactory class under generated/src/com/test folder&lt;/span&gt;. It includes all required imports and also format the class as per Java Standard. It generates as described below.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;Generated Class: GeneratedFactory.java&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;package com.test;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.myannotation.AnyXYZ;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import com.somclass.AnyXYZ;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;/**&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * Class Level Java Docs&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; * &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;@com.myannotation.AnyXYZ&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;public class GeneratedFactory {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    /**&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;     * Method Level Java Docs&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    * &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public static com.somclass.AnyXYZ myFirstMehtod(long data) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        com.somclass.AnyXYZ varName = new com.somclass.AnyXYZ();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        varName.setCode(100);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        return varName;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-5990069785118644284?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/5990069785118644284/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2010/01/use-codemodel-to-generate-java-source.html#comment-form' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/5990069785118644284'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/5990069785118644284'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2010/01/use-codemodel-to-generate-java-source.html' title='Use CodeModel to generate Java Source Code'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-7391185561106751399</id><published>2010-01-05T12:16:00.003+05:30</published><updated>2010-01-05T12:19:35.687+05:30</updated><title type='text'>Java Design Patterns</title><content type='html'>&lt;span style="color: rgb(0, 0, 0);"&gt;You can go through below link to get more idea on Different design patterns. You must have to try sample also there.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Link: &lt;/span&gt;&lt;a href="http://www.javacamp.org/designPattern/"&gt;http://www.javacamp.org/designPattern/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;I will add more details on the same later on.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-7391185561106751399?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/7391185561106751399/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2010/01/java-design-patterns.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/7391185561106751399'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/7391185561106751399'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2010/01/java-design-patterns.html' title='Java Design Patterns'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-8603851017682318354</id><published>2010-01-04T12:07:00.002+05:30</published><updated>2010-01-04T12:17:35.693+05:30</updated><title type='text'>How to use Java Persistence API (JPA)?</title><content type='html'>&lt;div style="text-align: justify; color: rgb(0, 0, 0);"&gt;The Java Persistence API, sometimes referred to as JPA, is a Java programming language framework that allows developers to manage relational data in applications. A persistence entity is a lightweight Java class whose state is typically persisted to a table in a relational database. Instances of such an entity correspond to individual rows in the table. Entities typically have relationships with other entities, and these relationships are expressed through object/relational metadata. Object/relational metadata can be specified directly in the entity class file by using annotations, or in a separate XML descriptor file distributed with the application.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Let's start with Simple Java Code.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Consider following table in any rational database.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Table Structure:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;CREATE TABLE `persistencemsg` (&lt;br /&gt;  `messageCode` INT(10) NOT NULL,&lt;br /&gt;  `sessionId` VARCHAR(500) NULL,&lt;br /&gt;  `message` BLOB(4294967295),&lt;br /&gt;  PRIMARY KEY (`messageCode`),&lt;br /&gt;  INDEX `messageCode` (`messageCode`)&lt;br /&gt;)&lt;br /&gt;ENGINE = INNODB;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Now, we will write corresponding entity class (JPIPersistenceMessage.java) for the same. @Table annotation represents table name of the table, in our case it is 'PERSISTENCEMSG'. This entity class is always implements Serializable to write thread safe code. It contains getter and setter method for each column in the table. @Column annotation represents column name corresponding to the each column in the table. Go through below Java code.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Class: JPIPersistenceMessage.java&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;import java.io.Serializable;&lt;br /&gt;import java.nio.ByteBuffer;&lt;br /&gt;import javax.persistence.Column;&lt;br /&gt;import javax.persistence.Entity;&lt;br /&gt;import javax.persistence.Id;&lt;br /&gt;import javax.persistence.Table;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; *&lt;br /&gt; * @author naman&lt;br /&gt; */&lt;br /&gt;@Entity&lt;br /&gt;@Table(name = "PERSISTENCEMSG")&lt;br /&gt;public class JPIPersistenceMessage implements Serializable {&lt;br /&gt;&lt;br /&gt;    private byte [] message;&lt;br /&gt;    private int messageCode;&lt;br /&gt;    private String sessionId;&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @return the message&lt;br /&gt;     */&lt;br /&gt;    @Column(name = "MESSAGE")&lt;br /&gt;    public byte[] getMessage() {&lt;br /&gt;        return message;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @param message the message to set&lt;br /&gt;     */&lt;br /&gt;    public void setMessage(byte[] message) {&lt;br /&gt;        this.message = message;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @return the messageCode&lt;br /&gt;     */&lt;br /&gt;    @Column(name = "MESSAGECODE")&lt;br /&gt;    public int getMessageCode() {&lt;br /&gt;        return messageCode;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @param messageCode the messageCode to set&lt;br /&gt;     */&lt;br /&gt;    public void setMessageCode(int messageCode) {&lt;br /&gt;        this.messageCode = messageCode;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @return the sessionId&lt;br /&gt;     */&lt;br /&gt;    @Column(name = "SESSIONID")&lt;br /&gt;    public String getSessionId() {&lt;br /&gt;        return sessionId;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @param sessionId the sessionId to set&lt;br /&gt;     */&lt;br /&gt;    public void setSessionId(String sessionId) {&lt;br /&gt;        this.sessionId = sessionId;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Now, how do we use this class for add/edit/update/find operation? First, we need to connect to the database so we have to write one XML file called 'persistence.xml' which is part of META-INF folder in your application. It contains properties to connect to the database. Please change as per your requirement.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;XML File: persistence.xml&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;persistence&lt;br /&gt;      xmlns=&amp;quot;http://java.sun.com/xml/ns/persistence&amp;quot;&lt;br /&gt;      xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;br /&gt;      xsi:schemaLocation=&amp;quot;http://java.sun.com/xml/ns/persistence&lt;br /&gt;      http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&amp;quot;&lt;br /&gt;      version=&amp;quot;1.0&amp;quot;&amp;gt;&lt;br /&gt;    &amp;lt;persistence-unit name=&amp;quot;persist_name&amp;quot;&amp;gt;&lt;br /&gt;        &amp;lt;class&amp;gt;JPIPersistenceMessage&amp;lt;/class&amp;gt;&lt;br /&gt;        &amp;lt;properties&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.jdbc.driver&amp;quot; value=&amp;quot;com.mysql.jdbc.Driver&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.jdbc.password&amp;quot; value=&amp;quot;password&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.jdbc.url&amp;quot; value=&amp;quot;jdbc:mysql://localhost:3306/databasename&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.jdbc.user&amp;quot; value=&amp;quot;username&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.jdbc.read-connections.max&amp;quot; value=&amp;quot;3&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.jdbc.read-connections.min&amp;quot; value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.jdbc.read-connections.shared&amp;quot; value=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.jdbc.write-connections.max&amp;quot; value=&amp;quot;5&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.jdbc.write-connections.min&amp;quot; value=&amp;quot;2&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.cache.type.default&amp;quot; value=&amp;quot;Full&amp;quot;/&amp;gt;&lt;br /&gt;            &amp;lt;property name=&amp;quot;toplink.target-database&amp;quot; value=&amp;quot;MySQL4&amp;quot;/&amp;gt;         &lt;br /&gt;        &amp;lt;/properties&amp;gt;&lt;br /&gt;    &amp;lt;/persistence-unit&amp;gt;&lt;br /&gt;&amp;lt;/persistence&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Let's write manager class (JPIPersistenceManager.java) for database operations. We need to load EntityManagerFactory using persistence-unit name given in persistence.xml file, in this case it is 'persist_name'. Now load EntityManager based on EntityManagerFactory. Now you can call all database operations like add/delete/find on this EntityManager object. User must have to start and close the transaction during each operation. Please go through below code.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Class: JPIPersistenceManager.java&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;import javax.persistence.EntityManager;&lt;br /&gt;import javax.persistence.EntityManagerFactory;&lt;br /&gt;import javax.persistence.Persistence;&lt;br /&gt;import javax.persistence.Query;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; *&lt;br /&gt; * @author naman&lt;br /&gt; */&lt;br /&gt;public class JPIPersistenceManager {&lt;br /&gt;&lt;br /&gt;    private static EntityManagerFactory emf;&lt;br /&gt;    private static EntityManager em;&lt;br /&gt;&lt;br /&gt;    public void createEntityManagerFactory(String persistenceUnitName) {&lt;br /&gt;        emf = Persistence.createEntityManagerFactory(persistenceUnitName);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private static void createEntityManager() {&lt;br /&gt;        em = emf.createEntityManager();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private static void closeEntityManager() {&lt;br /&gt;        em.close();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private static void createTransactionalEntityManager() {&lt;br /&gt;        createEntityManager();&lt;br /&gt;        em.getTransaction().begin();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private static void closeTransactionalEntityManager() {&lt;br /&gt;        em.getTransaction().commit();&lt;br /&gt;        closeEntityManager();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void storeMessage(JPIPersistenceMessage persistenceMessage) {&lt;br /&gt;&lt;br /&gt;        createTransactionalEntityManager();&lt;br /&gt;        em.persist(persistenceMessage);&lt;br /&gt;        closeTransactionalEntityManager();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void deleteMessage(long messageCode) {&lt;br /&gt;&lt;br /&gt;        JPIPersistenceMessage persistenceMessageDelete = findMessage(messageCode);&lt;br /&gt;        createTransactionalEntityManager();&lt;br /&gt;        em.remove(em.merge(persistenceMessageDelete));&lt;br /&gt;        closeTransactionalEntityManager();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public JPIPersistenceMessage findMessage(long messageCode) {&lt;br /&gt;&lt;br /&gt;        createTransactionalEntityManager();&lt;br /&gt;        JPIPersistenceMessage jpipm = em.find(JPIPersistenceMessage.class, Long.toString(messageCode));&lt;br /&gt;        closeTransactionalEntityManager();&lt;br /&gt;        return jpipm;&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public JPIPersistenceMessage updateMessage(byte[] message, long messageCode) {&lt;br /&gt;&lt;br /&gt;        createTransactionalEntityManager();&lt;br /&gt;        Query query = em.createQuery(&lt;br /&gt;                "UPDATE JPIPersistenceMessage p SET p.message = :message where p.messageCode = :messageCode");&lt;br /&gt;        query.setParameter("message", message);&lt;br /&gt;        query.executeUpdate();&lt;br /&gt;        closeTransactionalEntityManager();&lt;br /&gt;        return findMessage(new Long(messageCode));&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static void main(String args[]) {&lt;br /&gt;        JPIPersistenceManager persistenceManager = new JPIPersistenceManager();&lt;br /&gt;        persistenceManager.createEntityManagerFactory("persist_name");&lt;br /&gt;       &lt;br /&gt;        // Now call storeMessage,deleteMessage, updateMessage and findMessage on persistenceManager object.&lt;br /&gt;    }&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Hope it should be useful to you. Please let me know any comment on this...&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-8603851017682318354?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/8603851017682318354/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2010/01/how-to-use-java-persistence-api-jpa.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/8603851017682318354'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/8603851017682318354'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2010/01/how-to-use-java-persistence-api-jpa.html' title='How to use Java Persistence API (JPA)?'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-2373758391763713356</id><published>2009-12-29T10:48:00.005+05:30</published><updated>2010-01-07T14:45:58.694+05:30</updated><title type='text'>Simple Spring Application</title><content type='html'>&lt;div style="text-align: justify; color: rgb(0, 0, 0);"&gt;&lt;span style="font-weight: bold;"&gt;Spring&lt;/span&gt; could be (read intended to be) the one-stop-shop for enterprise application development but at the same time it is light-weight, non-intrusive and modular. Spring allows you to use a particular part/module of spring without bothering about the rest of the spring.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;Different types of IoC (Inversion of Control):&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify; color: rgb(0, 0, 0);"&gt;Central to the Spring Framework is its Inversion of Control container, which provides a consistent means of configuring and managing Java objects using callbacks. The container is responsible for managing object life cycles: creating objects, calling initialization methods, and configuring objects by wiring them together.&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Setter Injection: The examples below are based on setter injection. IoC sets the value of the type parameters of setter methods with the help of bean configuration XML file.&lt;/li&gt;&lt;li&gt;Interface Injection: Spring directly does not support interface injection but there is one thing called autowire, that can be used in spring to achieve some sort of interface injection.&lt;/li&gt;&lt;li&gt;Constructor Injection: constructor injection is almost same like setter injection except only difference that here IoC container initializes the constructor parameters.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;Download latest spring framework from &lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;&lt;a href="http://www.springframework.org/"&gt;http://www.springframework.org&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Let's start with sample application using Setter Injection:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;First write simple java file call &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 0);"&gt;Main.java&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;. It has one subclass call &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 0);"&gt;Inject.java&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; which contains some getter and setter methods to set properties. Main class contains main method which loads XML file and set the properties for the Inject class. This code uses spring to load properties from XML file.&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;The advantage is no need to recompile code again to set different properties. Every time it loads data from XML file. So any change needed just change XML file content.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;Code Snapshot:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;pre&gt;&lt;span style="font-weight: bold;"&gt;1. Main.java &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import org.springframework.beans.factory.xml.XmlBeanFactory;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;import org.springframework.core.io.ClassPathResource;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;public class Main {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public static void main(String[] args) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                "context.xml"));&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        Inject demo = (Inject) beanFactory.getBean("mybean");&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        System.out.println(demo);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;class Inject {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    private String name;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    private int age;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    private String company;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    private String email;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    private String address;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public void setAddress(String address) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        this.address = address;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public void setCompany(String company) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        this.company = company;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public void setEmail(String email) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        this.email = email;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public void setAge(int age) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        this.age = age;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public void setName(String name) {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        this.name = name;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    @Override&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    public String toString() {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;        return String.format("Name: %s\n" +&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                "Age: %d\n" +&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                "Address: %s\n" +&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                "Company: %s\n" +&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                "E-mail: %s",&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;                this.name, this.age, this.address, this.company, this.email);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;2. context.xml &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;beans xmlns="http://www.springframework.org/schema/beans"&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns=&amp;quot;http://www.springframework.org/schema/beans&amp;quot;&lt;br /&gt;      xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;br /&gt;      xmlns:p=&amp;quot;http://www.springframework.org/schema/p&amp;quot;&lt;br /&gt;      xsi:schemaLocation=&amp;quot;&lt;br /&gt;      http://www.springframework.org/schema/beans&lt;br /&gt;      http://www.springframework.org/schema/beans/spring-beans.xsd&amp;quot;&amp;gt;&lt;br /&gt;      &amp;lt;bean id=&amp;quot;mybean&amp;quot;&lt;br /&gt;         class=&amp;quot;Inject&amp;quot;&lt;br /&gt;         p:name=&amp;quot;Naman&amp;quot;&lt;br /&gt;         p:age=&amp;quot;30&amp;quot;&lt;br /&gt;         p:address=&amp;quot;Bangalore&amp;quot;&lt;br /&gt;         p:company=&amp;quot;Personal&amp;quot;&lt;br /&gt;         p:email=&amp;quot;naman.mehta@gmail.com&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;bean id="mybean"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;/bean&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;/beans&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Code compilation needs three jar files in your classpath. &lt;/span&gt;&lt;br /&gt;&lt;ol style="color: rgb(0, 0, 0);"&gt;&lt;li&gt;Spring framework jar file&lt;/li&gt;&lt;li&gt;commons-logging.jar&lt;/li&gt;&lt;li&gt;xercesImpl-2.9.1.jar&lt;/li&gt;&lt;/ol&gt;&lt;div style="text-align: left;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;I recommend, download latest eclipse IDE from &lt;a href="http://www.eclipse.org/"&gt;http://www.eclipse.org&lt;/a&gt; for Java EE developers. Keep jar files in your path or download plug-ins for spring framework.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;Will post other samples in next blog... Let me know your comments for the same...&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-2373758391763713356?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/2373758391763713356/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2009/12/simple-spring-application.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/2373758391763713356'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/2373758391763713356'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2009/12/simple-spring-application.html' title='Simple Spring Application'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1312262910397222081.post-3426923472700754819</id><published>2009-12-24T11:16:00.032+05:30</published><updated>2009-12-24T12:39:38.351+05:30</updated><title type='text'>How to use Factory Method?</title><content type='html'>&lt;div style="text-align: justify; color: rgb(0, 0, 0);"&gt;&lt;span style="font-weight: bold;"&gt;Overview:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The factory method pattern is an object-oriented design pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, whose subclasses can then override to specify the derived type of product that will be created. More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;How to create objects runtime?&lt;/span&gt;&lt;br /&gt;&lt;ol style="color: rgb(0, 0, 0);"&gt;&lt;li&gt;Write one Interface which has one method called myMethod().&lt;/li&gt;&lt;li&gt;Write some set of Classes which implements this interface.&lt;/li&gt;&lt;li&gt;Write one Class which has one static method which would return different objects based on the argument pass to that method.&lt;/li&gt;&lt;/ol&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;Code Snap-shot:&lt;/span&gt;&lt;br /&gt;&lt;ol style="color: rgb(0, 0, 0);"&gt;&lt;li&gt;Interface code.&lt;pre&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;span style="font-weight: bold;"&gt;1.1 myinterface.java&lt;/span&gt;&lt;i&gt;&lt;br /&gt;public interface myinterface {&lt;br /&gt;   public void myMethod();&lt;br /&gt;}&lt;/i&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Some set of Classes.&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2.1 a.java&lt;/span&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;public class a implements myinterface {&lt;br /&gt;   public a() {&lt;br /&gt;      System.out.println("I am a...");&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void myMethod() {&lt;br /&gt;      System.out.println("I am myMethod from a...");&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2.2 b.java&lt;/span&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;public class b implements myinterface {&lt;br /&gt;   public b() {&lt;br /&gt;      System.out.println("I am b...");&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void myMethod() {&lt;br /&gt;      System.out.println("I am myMethod from b...");&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2.3 b1.java&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;public class b1 extends b {&lt;br /&gt;   public b1() {&lt;br /&gt;      System.out.println("I am b1...");&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void myMethod() {&lt;br /&gt;      System.out.println("I am myMethod from b1...");&lt;br /&gt;   }&lt;br /&gt;}&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;2.4 b2.java&lt;br /&gt;&lt;/span&gt;public class b2 extends b {&lt;br /&gt;   public b2() {&lt;br /&gt;      System.out.println("I am b2...");&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void myMethod() {&lt;br /&gt;      System.out.println("I am myMethod from b2...");&lt;br /&gt;   }&lt;br /&gt;}&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/pre&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/li&gt;&lt;li&gt;Class which would return object based on the  argument pass.&lt;pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;3.1 test.java&lt;/span&gt;&lt;br /&gt;public class test {&lt;br /&gt;   public test() {&lt;br /&gt;      System.out.println("I am test class...");&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public static myinterface getMyInterface(String ftpType) {&lt;br /&gt;      try {&lt;br /&gt;         if (ftpType.equals("doc")) {&lt;br /&gt;            return (myinterface)Class.forName("a").newInstance();&lt;br /&gt;         } else if (ftpType.equals("html")) {&lt;br /&gt;            return (myinterface)Class.forName("b").newInstance();&lt;br /&gt;         } else if (ftpType.equals("txt")) {&lt;br /&gt;            return (myinterface)Class.forName("b1").newInstance();&lt;br /&gt;         } else if (ftpType.equals("xml")) {&lt;br /&gt;            return (myinterface)Class.forName("b2").newInstance();&lt;br /&gt;         } else {&lt;br /&gt;            return null;&lt;br /&gt;         }&lt;br /&gt;} catch (Exception e) {     &lt;br /&gt;         System.out.println(e);&lt;br /&gt;         return null;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public static void main (String a[]) {&lt;br /&gt;      myinterface m = test.getMyInterface("txt");&lt;br /&gt;      m.myMethod();&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span style="font-size:100%;"&gt;&lt;b style="color: rgb(0, 0, 0);"&gt;&lt;i&gt;Try this and happy blogging &amp;amp; reading. Any comment pls....&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1312262910397222081-3426923472700754819?l=namanmehta.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://namanmehta.blogspot.com/feeds/3426923472700754819/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://namanmehta.blogspot.com/2009/12/how-to-use-factory-method.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/3426923472700754819'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1312262910397222081/posts/default/3426923472700754819'/><link rel='alternate' type='text/html' href='http://namanmehta.blogspot.com/2009/12/how-to-use-factory-method.html' title='How to use Factory Method?'/><author><name>Naman Mehta</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://4.bp.blogspot.com/_jvh8BzebemU/SzMpgd1R_UI/AAAAAAAAHt4/Q428Mh_V0jM/S220/myone.jpg'/></author><thr:total>0</thr:total></entry></feed>
