DEV Community

Suvasish Das
Suvasish Das

Posted on • Updated on

Create simple HelloWorld spring application

In this tutorial I'm going to cover step by step, how to create simple annotation based "HelloWorld" spring application. To create spring application, first of all you need to configure or create a spring project in your IDE (for this tutorial I'm going to use Eclipse IDE). If you having some trable with project creation, I strongly recommend to click here and get start with your spring journey.

Project Structure

Image description

Step1

Let's create a simple POJO class named HelloWorld, inside your project classpath (in my case com.example.SpringAnnotation is my project classpath). HelloWorld.java POJO class containing one string variable named greeting and one setGreeting(String greeting) and getGreeting() function. Those function are called getter and setter methods.

setGreeting(String greeting) function simply set the local variable named greeting and getGreeting() function simply fetch the data of the local variable greeting.

Another public function sayHello() simply print the message in the console using println() method.

package com.example.SpringAnnotation;

public class HelloWorld {
    private String greeting;

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }
    public void sayHello() {
        System.out.println("Message is: "+greeting);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step2

After creating your POJO class, now it's time to define a configuration class. Let's create a HelloWorldConfig class.
Annotate the class with @Configuration annotaion. By defining the class with @Configuration we telling spring that this class is going to contain bean definations.

Now inside the class we are going to create bean defination. Every bean definations must annotated with @Bean annotation. One configaretion class can have multiple bean definations. In this case I'm going to create one bean named helloWorld() (started with small 'h').

package com.example.SpringAnnotation;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloWorldConfig {
    // Bean name = helloWorld
    @Bean
    public HelloWorld helloWorld() {
        HelloWorld hello = new HelloWorld();
        hello.setGreeting("Hello Spring");
        return hello;
    }
}
Enter fullscreen mode Exit fullscreen mode

Step3

Now, bean defination is complete. Now create a xml file in your application classpath. I'm going to create Config.xml in my root class path so that no need to specify the xml files with fully specified path name. Now copy the following code and paste in your xml file. This step is important beacause it define the beans prototype.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd"></beans>
Enter fullscreen mode Exit fullscreen mode

Now add the following tag inside <beans></beans> tag.

<context:component-scan base-package="com.example.SpringAnnotation"/>
Enter fullscreen mode Exit fullscreen mode

context:component-scan tag allow your code to scan for a specific bean. The base-package="<your_component_classpath>" locate for your defined components class path, so that it can scan for the secified beans in those location.

Step4

Now, we are ready to write our final pice of code or the driving code. Now create a java class containing the main method(if you create your project using maven or groovy project template, you are provided a java class containing main mathod, just use those file).

Inside those class declare a ApplicationContext variable and
define with ClassPathXmlApplicationContext() with parameter the string representation of your configuration xml file name with fully specified path (in my case, my xml file is placed at the root location, so I no need to enter the fully specified path name). Make sure that this variable is declared as static, so that we can access it from main method.

The ApplicationContext interface is responsible to create the spring IOC container(if you are not familiar with IOC, click here and go through the documentation). ClassPathXmlApplicationContext class is a subclass which implemented ApplicationContext interface. There are many other subclass available to create IOC container.

Now, inside the main method of our code, create a variable of type HelloWorld and instantiated it with a defined bean. In this case context.getBean("helloWorld", HelloWorld.class) return a variable of type HelloWorld. The first argument of getBean refers to the bean name as string and the second argument refers to the actual class. By default getBean() method's return type is java.lang.Object. The second argument in the getBean() method actually used to typecast the value. So we no need to typecast it manually. In the below code, I'm typecasting the value to HelloWorld.class type.

Now, we are all set. Simply call the sayHello() method and your code works successfully.

package com.example.SpringAnnotation;

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

public class App {
    private static ApplicationContext context =  new ClassPathXmlApplicationContext("Config.xml");
    public static void main( String[] args ){
        HelloWorld helloWorld = context.getBean("helloWorld", HelloWorld.class);
        helloWorld.sayHello();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Plox!!! our code runs successfully. Here is the output-

Image description

Top comments (1)

Collapse
 
anthonyjdella profile image
offline

Super easy to set up! Good job!