Spring hello world example

  • Facebook
  • Google
  • LinkedIn
  • Twitter

There are 5 steps to create the first spring application using eclipse IDE.

1. Create Java Project

Go to File menu → New → project → Java Project. Write the project name e.g. HelloSpring → Finish. Now the java project is created.

2. Add required spring jar files

Now add Spring Framework and common logging API libraries in your project. Now, right click on your project name HelloSpring and then follow the following option available in context menu: Build Path → Configure Build Path to display the Java Build Path window as follows:

Now use Add External JARs button available under Libraries tab to add the following core JARs from Spring Framework and Common Logging installation directories:

  • commons-logging-1.1.1
  • spring-aop-4.1.6.RELEASE
  • spring-aspects-4.1.6.RELEASE
  • spring-beans-4.1.6.RELEASE
  • spring-context-4.1.6.RELEASE
  • spring-context-support-4.1.6.RELEASE
  • spring-core-4.1.6.RELEASE
  • spring-expression-4.1.6.RELEASE
  • spring-instrument-4.1.6.RELEASE
  • spring-instrument-tomcat-4.1.6.RELEASE
  • spring-jdbc-4.1.6.RELEASE
  • spring-jms-4.1.6.RELEASE
  • spring-messaging-4.1.6.RELEASE
  • spring-orm-4.1.6.RELEASE
  • spring-oxm-4.1.6.RELEASE
  • spring-test-4.1.6.RELEASE
  • spring-tx-4.1.6.RELEASE
  • spring-web-4.1.6.RELEASE
  • spring-webmvc-4.1.6.RELEASE
  • spring-webmvc-portlet-4.1.6.RELEASE
  • spring-websocket-4.1.6.RELEASE

3. Create Java class Student.java

you have to create the Student class have name property. The name of the student will be provided by the applicationContext.xml file. Now create the java class, Right click on src → New → class → Write the class name e.g. Student → finish.

package com.javatutsworld;  
  
public class Student {  
private String name;  
  
public String getName() {  
    return name;  
}  
  
public void setName(String name){  
   this.name = name;  
}  
  
public void displayName(){  
   System.out.println("Hello: "+name);  
}  
}  

This is simple pojo class, containing only one property name with its getters and setters method. This class contains method named displayName() that prints the name of student by the hello message.

4. Create the applicationContext.xml file

Now click on src → new → file → give the file name such as applicationContext.xml and finish.

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
  xmlns="http://www.springframework.org/schema/beans"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xmlns:p="http://www.springframework.org/schema/p"  
  xsi:schemaLocation="http://www.springframework.org/
  schema/beans  
  http://www.springframework.org/schema/beans/
  spring-beans-3.0.xsd"> 

<bean id="studentbean" class="com.javatutsworld.Student">  
<property name="name" value="Mohd Saaim"></property>  
</bean>    
</beans> 

The bean element is used to define the bean for the given class. The property sub element of bean specifies the property of the Student class named name. The value specified in the property element will be set in the Student class object by the IOC container.

5. Create the MainTest.java class

Here you have to get the object of Student class from the IOC container using the getBean() method of BeanFactory.

package com.javatutsworld;  
  
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  
  
public class MainTest {  
public static void main(String[] args) {  
 Resource resource=new 
 ClassPathResource("applicationContext.xml");  
 BeanFactory factory=new XmlBeanFactory(resource);  
      
 Student student=(Student)factory.getBean("studentbean");  
 student.displayName();  
}  
}  

Now run the MainTest class.

Output

 Hello: Mohd Saaim
      Advertisements