Spring Framework Hello World Example

Spring Framework Hello World Example

In this article, I am going to discuss Spring Framework Hello World Example. Please read our previous article, where we discussed Spring Framework Architecture. Now, we will be writing a simple Hello World program using the Spring Framework. This application prints the text “Hello World!”.

Step 1: Before starting the project, there are several jar files that need to be installed into the project directory. Download the following jar files:

Store these JAR files in a folder. Every time we create a new project, these files need to be imported into the project.

Step 2: Create a new project in VS Code. Use the shortcut Ctrl-Shift-P (or Cmd-Shift-P on Mac) to open a dropdown menu. In the menu, select “Create a new Java project”. Provide the project with a directory and name.

Step 3: Import the aforementioned JAR files into the project. To do this, expand the “Java Projects” section at the bottom of the Explorer tab in VS Code.

Spring Framework Hello World Example

Copy the previously downloaded JAR files into the “Referenced Libraries” folder.

Spring Framework Hello World Example

Step 4: Create a new file called HelloWorld.java in the src/ directory. This file is responsible for printing the hello world statement. Add the following code to the file:

public class HelloWorld
{
    private String msg;

    public void getMsg()            {System.out.println(msg);}
    public void setMsg(String msg)  {this.msg = msg;}
}

Step 5: When creating the project, a file called App.java should be automatically created in the src/ directory. This file should contain the main() function. Modify the file as follows:

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

public class App
{
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld hw = (HelloWorld) context.getBean("helloWorld");
        hw.getMsg();
    }
}

We have performed the following modifications:

  1. Imported the required packages.
  2. Created an object of type ApplicationContext and initialized it with Beans.xml.
  3. Created an object of type HelloWorld and initialized it with the HelloWorld object from Beans.xml.
  4. Printed the message.

Step 6: In step 5, we have referenced a file called Beans.xml. This file is supposed to contain the HelloWorld object. Let us create the file in the src/ directory. Add the following content to the file:

<?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 = "helloWorld" class = "HelloWorld">
      <property name = "msg" value = "Hello World!"/>
   </bean>

</beans>

This file creates an object of type HelloWorld and populates it with the msg property.

Step 7: Compile and execute the application. Ensure compilation is successful. Ensure that the output is as expected:

Spring Framework Hello World Example

Congratulations! You have completed the hello world application in Spring Framework!

In the next article, I am going to discuss Spring Framework IoC Containers. Here, in this article, I try to explain Spring Framework Hello World Example. I hope you enjoy this Spring Framework Hello World Example article.

Leave a Reply

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