Gadgets

Wednesday, 16 December 2015

First Spring application

Example of spring application
Creating first spring application in eclipse, here we have clear steps to create spring application
1. Create the java project
2. Add spring capabilities
3. Create the class file(POJO class)
4. Create the xml file
5. Create the Driver class
Create New Project:
Go to File menu - New - project - Java Project. Write the Project name and click Finish
Add Spring jar files:
Right click on project – Build Path – configure Build Path – select Libraries tab – Add External JARs


Create POJO class:
Test.java
=======
package test;
public class  Test
{
               public void hello()
               {
                               System.out.println("My first Spring Project");
               }
}

Create xml File:
Open org.springframework.beans-3.0.1.RELEASE-A.jar file with winJAR archiver
 and open folder org\springframework\beans\factory\xml
 in xml folder open spring-beans.dtd file
 in spring-beans.dtd file copy line 35 - 37 below code
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">




spring.xml
========
package resources;
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean class="test.Test" id="t">
</bean>
</beans>
Create Driver Class:
Client.java
========
package test;
class Client{
               public static void main(String[] args)
               {
                               //find xml file
                              Resource r=new ClassPathResource("resources/spring.xml");
                               //load xml file into container
                               BeanFactory factory=new XmlBeanFactory(r);
                               //creat test class object
                               Object o=factory.getBean("t");
                               //type casting object
                               Test t=(Test)o;
                               t.hello();
               }
}
/*
Output:
My first Spring Project
*/



No comments:

Post a Comment