Today we are going to understand how to put a Spring program into execution using the Eclipse Integrated Development Environment(IDE) application.
As you know that Spring Framework allows us to develop scalable and maintainable enterprise applications while keeping the code simple and loosely coupled.
So, in this tutorial, we are going to create a Spring project consisting of a few loosely coupled Java classes also known as beans, configure these
beans in a configuration xml file, load and instantiate the beans using the Spring Framework.
Creating a Java Project in Eclipse
To begin creating you first Spring program in Eclipse, you will have to create a new Java Project by clicking on -
File -->Java Project in the Eclipse window, as shown in the picture below.
Let us name this project SpringFirstProgram and click on Finish button, as shown in the picture below.
Next, after naming this Java project, we will create a package, which will contain all the Java classes
in our Spring project.
In order to create a package, we will have to right click on the src folder in our Java Project and click on New--> Package, as shown
in the picture below.
Next, we name this package - decodejava.
Adding the Java class in our Spring Project
We are going to create all the required Java classes/interface in our Spring Project.
So, let's create our first class under the package, by right clicking decodejava package and click on
New --> Class, as shown in the picture below.
Next, we are going to name this class/interface - Tennis and click on Finish button.
public class Tennis
public void message()
System.out.println("Hello World! Do you like Tennis?");
Adding the Utility class that calls the Spring API
Next, we are going to create another class under the package, by right clicking on decodejava package and click on
New --> Class, as we did in the previous step.
We are going to name this class - Utility and click on Finish button.
The Utility class uses the ApplicationContextcontainer(an interface) of Spring Framework by
creating its instance using its implemented class FileSystemXmlApplicationContext to -
Load the configuration xml file - config.beans.xml,
To access the bean specified in the configuration file by calling the getBean() method of ApplicationContextcontainer,
Instantiate the Tennis class and call its methods.
public class Utility
public static void main(String args)
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
Tennis tennisBean = context.getBean("TennisBean", Tennis.class);
Adding a configuration file
Next, we are going to add a configuration file to our project. In order to create this configuration file, we need to right-click on our package decodejava
and click on New --> File, as shown in the picture below.
Next, we are going to name this configuration file as config.beans.xml
This configuration document is an Extensible Markup Language(XML) file, ending with .xml extension,
and we have named it config.beans.xml. In this file, we have configured a Tennis instance with a unique id and an OutdoorSport
instance is injected into the Tennis bean using the property named sport.
This mapping document has a parent <beans> tag as the root element and its individual child elements, each with a <bean> tag, containing all the attributes such as -
The id attribute within the <bean> element defines a unique id associated with the bean.
The class attribute within the <bean> element defines the name of the class of this bean.
The property child element in <bean> tag refers to a property within the Tennis.
The name child attribute refers to the name of a property in the Tennis class i.e. sport.
The ref child attribute refers to the reference name of this property which is matched with the id
of another bean(another class - OutdoorSport) declared in this configuration file within the <bean> tag.
We are going to add some JARs files to our Java project.
These JARs are required in order to successfully execute a Spring project.
In order to do this, let's right click on our project SpringFirstProgam and click on Properties.
In the next window, we need to click on Java Build Path on the left and click on the Libraries tab on the right side of window.
In this window, we need to click on the Add External JARs, as shown in the picture below.
Next, we are going to go to a folder named libs(within our Spring installation folder)
and select all the JARs in it and click on Open button.
Next, you will be shown the added multiple JARs to your project, to finalize it, click the OK button.
Finally, after executing Utility class, you will get the following output within the Console window.
This output shown below, shows how the Utility class has used the ApplicationContext container of Spring Framework to load the configuration xml file - config.beans.xml,
access the beans specified in it, instantiate the Tennis class and calls its methods.
Jul 06, 2018 1:02:02 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1ee0005: startup date [Fri Jul 06 13:02:02 IST 2018]; root of context hierarchy
Jul 06, 2018 1:02:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
-An Outdoor Sport-