Home > Spring > Simple Spring Program with example

Simple Spring Program with example

Simple Spring Program with example

Simple Spring Program with example , Follow below step to create it.

Steps to create Simple Spring Program with an example in Eclipse IDE Let’s see the below steps to create the first spring application using eclipse IDE and some spring jars.

1) Create the Java Project

Go to File menu – New – project – Java Project. Write the project name as. Hello_World- Finish. Now the java project is created.

2) Add spring jar files

  1. commons-logging-1.2
  2. spring-beans-5.0.8.RELEASE
  3. spring-context-5.0.8.RELEASE
  4. spring-core-5.0.8.RELEASE
  5. spring-expression-5.0.8.RELEASE

To run this example, you need to load only spring core jar files. To load the jar files in eclipse IDE, Right click on your project – Build Path – Add external archives – select all the required jar files which mentioned above then  – finish..

3) Create Java class

In such case, we are simply creating the Gender class have name and gender property. The name and gender of the Gender will be provided by the xml file.  We will see the actual use of property Dependency Injection.

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

package beans;
public class Gender {
private String gender;
private String name;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public void setGender(String gender) {
this.gender = gender;
public String getGender() {
return gender;

4) Create the xml file

To create the xml file click on src – new – file – give the file name such as spring.xml – finish. Open the spring.xml.xml file, and write the following code:

<bean id ="g" class="beans.Gender">
<property name="gender" value="Mr."/>
<property name="name" value="Ravi"/>

5) Create the test class

Create the java class e.g. Test. Here we are getting the object of Student class from the IOC container using the getBean() method of BeanFactory. Let’s see the code of Client class.

package client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Gender;

public class Client {
public static void main(String[] args) {
String[] file=new String[]{"resources/spring.xml"};
ApplicationContext ap=new ClassPathXmlApplicationContext(file);
Gender g=(Gender)ap.getBean("g");
String s=g.getGender();
System.out.println("Good Morning "+g.getGender()+" "+g.getName());

Good Morning Mr. John

Related Topics:-

You may also like :-
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *