logo

Creating Spring Application In Eclipse IDE


Show

Here, we will provide you with a detailed glimpse of how you can create a Spring Application in Eclipse IDE. First of all, you have to set up your Spring environment properly as defined in the Spring Environment setup. We also expect from you that you have some working knowledge of Eclipse IDE.

The step by step procedure to create a Spring application in Eclipse IDE is given below:

  • create the java project
  • add spring jar files
  • create the class
  • create the XML file to provide the values
  • create the test class

Now we will follow the given steps in detail:

Create the Java Project

This is the first step to create a simple Java Project using Eclipse IDE. Follow the steps as illustrated below:

Go to File menu - click on New - click on project - then select Java Project. Write the project name e.g. firstspring - click on Finish. Now the java project is created.

  • Add spring jar files

The three main jar files that are required to run this application are listed below

  • org.springframework.core-3.0.1.RELEASE-A
  • com.springsource.org.apache.commons.logging-1.1.1
  • org.springframework.beans-3.0.1.RELEASE-A

For the Spring core application, you can download the required jar files. In this example, you are only required to load spring core jar files. To load the jar files in eclipse IDE, follow the steps

Right-click on your project - click on Build Path - Add external archives - now select all the required jar files - then click finish.

In this example, you can add the following core JARs from the Spring Framework and 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.RELEAS

Create Java class

In this example, we are creating the Student class with the name property. The student’s name will be provided by the XML file. Now to create the java class, follow the below steps:

Right-click on src in the package explorer

Click new > class

Write the class name e.g. Student

Click on finish

Write the following code:

package com.intellinuts;  

public class Student {  
private String name;  

public String getName() {  
    return name;  
}  

public void setName(String name) {  
    this.name = name;  
}  

public void displayInfo(){  
    System.out.println("Hello: "+name);  
}  

}  

This is a simple bean class that contains only one property name with its getters and setters method. It also includes an extra method named displayInfo() that displays the student name in the hello message.

  • Create the XML file

Now you have to create an XML file also called Bean Configuration file that binds the classes together. From the developer's point of view, this file is also called Beans.xml. But you can choose any name you like. Make sure that this file is available in CLASSPATH. You have to use the same name in the main application while creating an application context as mentioned in the MainApp.java file.

The XML file assigns a unique ID to different beans that control the object’s creation with different values without affecting any source file of Spring.

Now, lets create an xml file by clicking on src - new - file - provide the file name as applicationContext.xml, then click on finish. Now open the applicationContext.xml file, and write the code shown below:

   
<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.intellinuts.Student">  
<property name="name" value="Nitin Kaushik"></property>  

</beans> 

Here the bean element defines the bean for the class given. The property, which is a subelement of the bean, defines the property of the Student class ‘name’. The value mentioned in the property element will be set in the Student class object by the Inversion of Control (IOC) container.

  • Create the test class

Now create the java class (in this example we are creating a test class). We will retrieve the object of the Student class from the IOC container with the help of the getBean() method of BeanFactory. You will see the code of the test class as shown below:

package com.intellinuts;  

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 Test {  
public static void main(String[] args) {  
    Resource resource=new ClassPathResource("applicationContext.xml");  
    BeanFactory factory=new XmlBeanFactory(resource);  

    Student student=(Student)factory.getBean("studentbean");  
    student.displayInfo();  
}  
}  

Now to run this class, use either the Run option available in the Eclipse IDE or use Ctrl + F11. But before compiling or running this file, you need to keep the MainApp.Java file tab active.

After the successful running of this application, you will see the result as

Hello: Nitin Kaushik