Spring Hello World Sample

This simple Spring tutorial demonstrates how to retrieve a Spring Bean and print out a message to the screen. The sample was created using the following technologies:

  • Spring 5.0.5
  • Maven Integration in Eclipse
  • Eclipse IDE for Java Developers Version: Oxygen.2 Release (4.7.2)
  • Java JDK 1.8

Let’s start off by defining the simple steps in order to create the Spring application in Eclipse IDE.

  1. Create New Java Project
  2. Add Spring Dependencies
  3. Create Java Application Class
  4. Create Xml file to Provide the Values
  5. Create the Test class

Create New Maven Project

All of my Java development is done in Eclipse using the Maven plugin. You would need to download and install Eclipse and then add the Maven Plugin first.

In Eclipse, select File » New » Other » Maven » Maven Project from the top menu bar. You can then follow the steps to Create New Maven Project from our previous tutorial.

Add Spring Dependencies

Add the following lines to the pom.xml file of your new Maven project so that Maven can automatically pull in the needed Spring dependencies.







Create Java Application Class

We are simply creating a StudentBean Class that has a name variable. The name of the student will be provided in an xml file. You will need to modify the package statement based on what you entered during the create Maven project step.

package com.jcd.spring.tutorials;

public class StudentBean {
	String name; 
	public String getName() { 
		return name; 
	public void setName(String name) { 
		this.name = name; 
	public void displayName() { 
		System.out.println("Hello: "+ name); 


Create Xml file to Provide the Values

Next we create the xml file click in the project. Click on src folder in the package explorer, and then File » New » Other » XML File and name the file StudentConfig.xml. Click Finish. Add the following lines to StudentConfig.xml: (note: you will need to adjust class to reflect your package path)”

<beans xmlns="http://www.springframework.org/schema/beans"
	<bean id="studentbean" class="com.jcd.spring.tutorials.StudentBean"> 
	<property name="name" value="JavaCodeDepot"></property> 

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

Create the Java Test class

Create the Java class called StudentDemo. Here we are getting the object of StudentBean class from the Inversion of Control (IOC) container using the getBean() method of ConfigurableApplicationContext.

package com.jcd.spring.tutorials;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentDemo {
	public static void main(String[] args) {
		ConfigurableApplicationContext appCon = new ClassPathXmlApplicationContext("StudentConfig.xml");
		StudentBean factory = (StudentBean)appCon.getBean("studentbean"); 


When you run StudentDemo Class, you will see the output Hello: JavaCodeDepot In the Eclipse Console.

Spring Hello World

Add a Comment

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