Spring application in Eclipse IDE

Hi friends, This is Trilochan Tarai, JAVA expert from SILAN Software Pvt. Ltd. BBSR, Odisha. Here, I am going to create a simple spring applicationusing eclipse IDE. Let's see the following steps to create the spring application in Eclipse IDE.

  • • create the java project
  • • add(configure) spring jar files
  • • create the bean class
  • • create the xml file to provide the values
  • • create the test class(client logic file)

Step-1: Create a Java Project:

Go to File menu - New - Java Project. Write the project name e.g. here I have specified SpringCoreModule - Finish. Now the java project named as SpringCoreModuleis created.

Step-2 : Add or configure Spring jar files :

The following jar files required to run this application.

  • • 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

download the core jar files for spring

To configure the spring jar files in eclipse IDE, Right click on src folder - BuildPath – Configure Build Path – Libraries – Add External Jars… - select all the required jar files - finish..Also configure log4j.jar file.

Step-3 : Create a JAVA bean class:

We know, a JAVA bean is a class containing private data members and containing getXXX() and setXXX() method

getXXX() : getter method      setXXX() : setter method

Here in our context, getXXX() is optional but setXXX() is mandatory.

To create the java class, Right click on src - New - class - Write the class name
e.g.Employee - finish. Write the following code:


Employee.java

Packagecom.silan;

publicclass Employee {

	private String name;
	
	public String getName() {
		returnname;
	}
	
	publicvoidsetName(String name) {
		this.name = name;
	}
	
	publicvoidshow()
	{
		System.out.println("Hii"+" "+name);
	}
}

This is simple bean class, containing only one property name with its getters and setters method. This class contains one extra method named as show() that prints the employee name by the Hii message.

Step-4: Create a XML file:

To create the xml file click on src - new - file - give the file name such as spconfig.xml -

finish. Open the spconfig.xml file, and write the following code:

<?xmlversion="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">

	<beanid="id1"class="com.silan.Employee">
		<propertyname="name"value="SanghamitraMohanty">
	</property>

</bean>

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

Step-5 : Create the client logic file:

Example1.java

package com.silan;
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 Example1 {

	public static void main(String[] args) {
		
		//Create a Resource object
		Resource res=new ClassPathResource("spconfig.xml");
		
		//Create a BeanFactory object
		BeanFactory bf=new XmlBeanFactory(res);
		
		Object obj=bf.getBean("id1");
		
		Employee e=(Employee)obj;
		
		e.show();

	}

}

Now run this class, we will get the output HiiSanghamitra Mohanty.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext