Java

Creating a Hello World Spring Application

In this article, we will learn to create a “Hello World” spring application.

The process of creating this Spring application consists of these following steps:

  1. Create a maven project
  2. Add Spring dependencies
  3. Create a Spring Bean Class
  4. Create a Spring Container XML file
  5. Create a Spring Main class

Create a Maven Project

First, we will create a maven project to develop Spring application. We already have covered this topic in our article in creating maven project. You can refer this to get in depth idea of creating maven project if you are not already familiar in: <editor make link for maven article>.

Let’s start with opening the Eclipse and clicking on the File menu. Then, select the maven project like this: File->New->Maven Project

After selecting, it will ask for some details such as project name, app name, version, packaging type, and etc. The packaging specifies final build bundle type of the project. If the application is web app, it should be war (Web Archive).

We selected JAR here as we are not creating web application rather a simple “hello world” application.

Hit the finish button and it will take couple of seconds to create a project. At first glance the newly created project looks like this:

Add Spring Dependencies

Initially, all the folders are empty. No source file is present. The pom.xml file is the main maven file that contains all the configurations and dependencies of the project. We will use this file to add spring dependencies.

Spring Dependencies:

Here, we are adding only two spring core and spring context for creating simple application. We will add more dependencies only if we need to scale up application otherwise only these two are enough.

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

After adding Spring dependencies, the pom.xml file looks like this:

// pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.linuxhint</groupId>
  <artifactId>springapp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springapp</name>
  <description>a simple spring app</description>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>
<properties>
        <spring.version>5.2.8.RELEASE</spring.version>
</properties>
</project>

vMaven will download all the required files automatically to the project.

After adding these dependencies, you can notice that the maven dependency folder now contains all the JAR files.

Notice: The maven dependencies folder was not present in the initial crafted project, before adding the dependencies. It was added by the maven to collect all the dependencies to a single folder for better management. See the below project structure.

Create Spring Bean Class

Now, let’s create a bean class which is a Java class having private properties and getter, setter methods.

In Spring, bean is a java object which is created by the spring container.

To create a class, click on the File menu and then select class like this:

File->New->Class

Provide the package name as com.linuxhint.beans as we want to place it in separate beans package.

Click finish button and then add some source code to it. We created a string variable msg and its getter, setter methods.

// HelloWorld.java

package com.linuxhint.beans;

public class HelloWorld {
   
    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
   

}

Create Main Class

Similarly, create a SpringApp.java class in com.linuxhint package.

Click the finish button and add source code to it.

This is the main class where spring start execution. Here, we first read the applicationContext.xml file by using the ClassPathXmlApplicationContext constructor and then call its getBean() method to get the “HelloWorld” bean.

We configured the bean into the applicationContext.xml file with a Hello World value. So, to read this we used the getMsg() method of the HelloWorld class.

You don’t worry about this strange code. We will discuss this in detail later in our articles. For now, just use this piece of code in the class.

// SpringApp.java

package com.linuxhint;

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

import com.linuxhint.beans.HelloWorld;

public class SpringApp {
    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld hello = (HelloWorld) ac.getBean("hello");
        String msg = hello.getMsg();
        System.out.println("--- "+msg+" ---");
    }
}

Creating Bean Container

Bean container is an XML file that contains some XML code to configure beans.

We named it applicationContext.xml that contains beans configuration which is HelloWorld in our case. The bean tag requires id and class name. So, we passed the bean id and the name of our bean class HelloWorld with complete package name.

// applicationContext.xml

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
>

   <bean id = "hello" class = "com.linuxhint.beans.HelloWorld">
      <property name = "msg" value = "Hello World"/>
   </bean>

</beans>

After adding all the files, the final Project layout looks like this:

Run Spring Application

Now, we can run the application to test and see the desired Hello World output.

To run the application, right-click on the project and select run->java application.

You can see the output in the Console tab.

Output:

See all working fine.

Till here, we have learned successfully to create a Hello World spring project. In the next articles, we will learn to create this application using STS (Spring Tool Suite).

About the author

Irfan Khan

I’m a software programmer having more than 5 years of development experience in Java and related technology. I love to write technical content as well and love to share the technical knowledge to make it available for all.