< 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

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;
import org.springframework.context.support.FileSystemXmlApplicationContext;

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 a unique id and an OutdoorSport instance is injected into the Tennis bean using the property named sport.

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<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


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]
Playing Tennis
-An Outdoor Sport-

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< Download and Install Spring Framework
Spring with Annotations >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next