Thursday, 21 April 2016

Spring 3 - Hello world example


Step 1

The Spring 3.0 at least requires JDK 5.
So, make sure you have JDK 5 or above. Open DOSprompt if you are using windows and type java -version.
This will display the version of Java installed on your machine.

Step 2

Download the latest version of Spring 3 from http://www.springsource.org/download.
For this tutorial we have downloaded spring-framework-3.0.0.RELEASE-with-docs.zip, which contains the documentation also.
Extract it in a directory.

Step 3

Create a new project in Eclipse IDE and then add the library files of Spring3.
  • Create a new folder "lib" in the project space to hold the Spring 3.0 libraries.
  • Now we will add the Spring 3. libraries to the project.
  • Extract the "spring-framework-3.0.0.RELEASE-with-docs.zip" file if you have not extracted.
  • Now go to the "dist" directory of the and then copy all the jar files (Ctrl+C) and paste on the lib directory (of our project) in the Eclipse IDE.
  • Then find the commons-logging.jar from extracted folder and also copy this file into Eclipse IDE.

Step 4

Now add all the libraries to "Java Build Path".
Right click on the "Spring3" in project explorer and then select properties.
Then select "Java Build Path" --> Libraries and then click on the "Add JARs" button.
And add all the libraries to Java Build Path.


Step 5

Create a new package shaan.pkg to hold the java files.
Right click on the "Spring3" and then select New --> Package.
Then provide the package name as genius.pkg and click on the "Finish" button.


Step 6

Create a new Java file Spring3HelloWorld.java under the package genius.pkg and add the following code:
package genius.pkg;
public class Spring3HelloWorld {
  public void sayHello(){
    System.out.println("Hello Spring 3.0");
  }
}

In the above class we have created a method sayHello() which prints the "Hello Spring 3.0" on the console.
In this section we will use the Spring framework to manage the Spring3HelloWorld bean, and then get the bean from the Spring runtime environment (Spring context) and the call the sayHello() method.
In the next step we will xml file which will be used as Metadata to configure the bean.


Step 7

Now create a new xml (SpringHelloWorld.xml) file using Eclipse IDE.
Add the following code to the xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.w3.org/2001/XMLSchema-instance" target="_blank" rel="nofollow">http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/beans" target="_blank" rel="nofollow">http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <bean id="Spring3HelloWorldBean
        class="genius.pkg.Spring3HelloWorld" />
</beans>

The above xml file declares the spring bean "Spring3HelloWorldBean" of the class genius.pkg.Spring3HelloWorld.
The tag is used to declare a bean in the xml file.

Spring uses the xml file to configure the spring run-time environment.
Spring framework manages the beans in our program.
Note: You should move the SpringHelloWorld.xml file into src directory of the project.
Jut use mouse to drag and dop in the src folder.


Step 8

Now create a java file (Spring3HelloWorldTest.java) into genius.pkg package and add the following code:
package genius.pkg;
import java.util.Map;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;
public class Spring3HelloWorldTest {
  public static void main(String[] args) {
  XmlBeanFactory beanFactory = new XmlBeanFactory(
          new ClassPathResource("SpringHelloWorld.xml"));
  Spring3HelloWorld myBean = (Spring3HelloWorld)  
          beanFactory.getBean("Spring3HelloWorldBean");
  myBean.sayHello();}
}

In the above code we have created the instance of XmlBeanFactory and the retrieved the "Spring3HelloWorldBean".
Then we can call the sayHello() method on the bean.
The XmlBeanFactory  class is extension of DefaultListableBeanFactory that reads bean definitions from an XML document.
In our case it reads the bean definitions from SpringHelloWorld.xml file.


Step 9

To run the code in Eclipse open Spring3HelloWorldTest.java in the editor and then right click and select Run as --> Java Application.
This execute the Spring3HelloWorldTest.java file and following output will be displayed in the console.
Jan 1, 2010 6:49:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [SpringHelloWorld.xml]
Hello Spring 3.0

No comments:

Post a Comment

Note: only a member of this blog may post a comment.