Simple Spring Hello World program

To get started with Spring, the very first thing to do is spin up a basic, functioning Hello World Spring application that does one thing: display a simple greeting.

In this Spring tutorial, we won't worry about advanced concepts such as dependency injection (DI) or inversion of control (IoC). We're just going to build a simple Hello World program that calls the core Spring APIs and displays a simple greeting.

The goal of a Hello World application is to just get something simple configured and running. You must walk before you can run. We'll deal with DI and IoC in future tutorials.

Spring Hello World prerequisites

The only prerequisite to complete this Spring tutorial is a development environment that supports Java, along with a Java build tool such as Maven or Gradle.

All these prerequisites come prepackaged with the Eclipse distribution of the Spring Tools Suite (STS). I highly recommended the use of Eclipse STS, but any modern IDE such as IntelliJ or VS Code will work just as well.

How to create a Spring Hello World app

We will follow these steps to build our simple, Spring Hello World program:

  1. Add a project dependency for the Spring Framework to your build tool.
  2. Create a Greetings class to be managed by the Spring Framework.
  3. Tell the Spring Framework to manage the Greetings class for you.
  4. Ask the Spring Framework for an instance of the Greetings class.
  5. Once obtained, call the instances sayHelloWorld() method.

Add project dependencies

The first step is to add a dependency to your Gradle build file, or your Maven POM, that references the core Spring Framework. The Maven dependency is below:

<!-- Maven Spring Framework Dependency --> 
<dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-context</artifactId>
  <version>6.0.0</version> 
</dependency>

Here is the dependency to add to your build file if you're using Gradle:

dependencies { 
  'org.springframework:spring-context:6.0.0' 
}

Create a bean for Spring to manage

The reason why Spring makes software development easier is that it manages JavaBeans that have complex lifecycle requirements or require complicated configuration.

We'll push off the use of complex and complicated components for future Spring tutorials. For now, let's just create a very simple JavaBean for Spring to manage. The bean will be named Greetings, and it will have a single method named sayHelloWorld() as so:

package com.mcnz.spring;

public class Greetings {
  public void sayHelloWorld() {
    System.out.println("Hello World!");
  }
}

JavaBeans and the Spring Framework

To get a JavaBean from the Spring Framework, you must do two things:

  1. Tell Spring to manage that bean.
  2. Ask Spring for an instance of that managed bean.

There are many ways to tell Spring about the various JavaBeans you want it to manage, including SpringConfig classes and XML files, but the easiest way to do it is to just tell Spring which classes during initialization.

Tell Spring to manage your bean

Just pass the names of the classes as arguments to the constructor that initializes the Spring Framework, and the framework will take care of the rest. Here's how it looks in code:

ApplicationContext spring = new AnnotationConfigApplicationContext(Greetings.class);

For the sake of clarity, I used the variable name spring to refer to the runtime embodiment of the Spring Framework. The proper convention is to call it context, not spring, as the declared type is ApplicationContext.

Get your JavaBean from Spring

To get a managed bean out of the Spring context, simply call the getBean() method and provide the name of the class. With the class in hand, you're free to call any of its methods:

Greetings greetings = spring.getBean(Greetings.class); 
greetings.sayHelloWorld();

When this Spring program runs, Hello World is printed out to the console.

Complete Spring Hello World example

Here is the full code for this Spring Hello World program. I changed the variable name from spring to context to reflect industry standard naming conventions, as mentioned previously. Also, I removed the public modifier from the Greetings class to let the code reside within a single file:

package com.mcnz.spring;

import org.springframework.context.*;
import org.springframework.context.annotation.*;

public class SpringHelloWorldProgram {

  public static void main(String[] args) {
    // variable name changed from spring to context
    ApplicationContext context = 
      new AnnotationConfigApplicationContext(Greetings.class);
    Greetings greetings = context.getBean(Greetings.class);
    greetings.sayHelloWorld();
  }

}

class Greetings {
	
  public void sayHelloWorld() {
    System.out.println("Hello World!");
  }

}

When to use Java vs. Spring?

Keen learners often comment on how the use of Spring seems needlessly complex, especially when all that is needed is the instance of the Greetings class. After all, without the Spring Framework the code could be just two simple lines:

Greetings greetings = new Greetings();
greetings.sayHelloWorld();

In this case, the use of the Spring Framework to access a simple JavaBean is indeed overkill, but acceptable given that this is simply a Spring Hello World example.

However, when JavaBeans require complex configuration or lifecycle management, the value of the Spring IoC container becomes more obvious, as the development of complex enterprise-grade software becomes easier with Spring's dependency injection capabilities.

Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development; DevOps; Spring; and container-based technologies such as Docker, Swarm and Kubernetes.

View All Videos