< Prev
Next >

First Spring Program

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


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.
package decodejava;

public class Tennis 
	public void message() 
		System.out.println("Hello World! Do you like Tennis?");


Adding the Utility class that calls the Spring API

The Utility class uses the ApplicationContext container(an interface) of Spring Framework by creating its instance using its implemented class FileSystemXmlApplicationContext to -
package decodejava;

import org.springframework.context.ApplicationContext;

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

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 an id, "TennisBean", which should be the same as the name of the Tennis class bean that we have specified while calling the getBean() method in Utility class(for reference, please take a look at

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns=""

<bean id ="TennisBean" class="decodejava.Tennis"> </bean>


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 -

Adding JARs

  • 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 prepareRefresh
INFO: Refreshing 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]
Hello World! Do you like Tennis?

Please share this article -

< Prev
Next >
< Download and Install Spring Framework
Spring with Annotations >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition