Spring 4 Hello World using java based configuration

In this article, we will create a Hello World program in Spring4 with the java based configurations.

 

We will be using Eclipse as the IDE and Maven for creating the project structure and adding needed dependencies.

 

Here are the steps:

 

Step 1:

 

Run the following command to create a Maven project :

 

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.topjavatutorial.spring -DartifactId=spring-hello -DinteractiveMode=false

 

basicmavenproject

 

This will create the project with name “spring-hello”.

 

If you go inside the spring-hello folder, you should be able to find the generated pom.xml file.

 

Here is how the pom.xml looks like currently:

 

<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 http://maven.apache.org/maven-v4_0_0.xsd”>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.topjavatutorial.spring</groupId>
  <artifactId>spring-hello</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>spring-hello</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

 

This is just a basic pom.xml. In the next step, we add spring dependency to it.

 

Step 2:

 

Add following spring-context dependency in the pom xml :

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.0.5.RELEASE</version>

</dependency>

 

Here is how it looks like after this change :

 

<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 http://maven.apache.org/maven-v4_0_0.xsd”>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.topjavatutorial.spring</groupId>
  <artifactId>spring-hello</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>spring-hello</name>
  <url>http://maven.apache.org</url>
  <dependencies>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

 

Now, the project is ready with spring configuration.

 

Step 3:

 

Convert the generated project to eclipse project by running the following command from command prompt:

 

mvn eclipse:eclipse

 

This command should be run from the path where pom.xml is present. For example, we created the “spring-hello” project in C:\projects and the pom.xml is inside the spring-hello folder. So, we run the command from C:\projects\spring-hello

 

convert maven project to eclipse

 

This would convert it to an Eclipse project. Now, lets import the project in Eclipse using :

 

File ->Import -> Existing Projects into Workspace

 

Step 4:

 

Lets create a package com.topjavatutorial.spring if not already present.

 

Create a class HelloService as follows :

 

package com.topjavatutorial.spring;

public class HelloService {
  private String msg;

  public String getMessage() {
    return msg;
  }

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

}


 

Step 5:

 

Create following java-based bean definition class:

 

package com.topjavatutorial.spring;

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

@Configuration
public class HelloBeanConfiguration {
  @Bean
  public HelloService helloService() {
    HelloService bean = new HelloService();
    bean.setMessage("Hello TopJavaTutorial");
    return bean;
  }
}


 

Step 6:

 

Create an App class with main() method unless its already present in the package.

 

Inside the main method, instantiate the Spring Container by giving the Java-based configuration class you created in the previous step as the constructor argument.

 

Using the application context, you can access the HelloService methods.

 

package com.topjavatutorial.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
        AnnotationConfigApplicationContext applicationContext =
            new AnnotationConfigApplicationContext(HelloBeanConfiguration.class);
        
        HelloService hello = applicationContext.getBean("helloService",HelloService.class);
        
        System.out.println(hello.getMessage());
    }
}


 

Step 7:

 

Running the App.java file will produce the following output :

 

Hello TopJavaTutorial

 

 

spring annotation program output

 

Understanding this program

 

In step 4, we created a HelloService class with methods getMessage() and setMessage().

 

Then in Step 5, we created a spring bean definition class HelloBeanConfiguration with annotation @Configuration. This annotation informs Spring container that this class contains configuration metadata.

 

Inside this class, we added a method helloService() and annotated that with @Bean annotation.

Inside this method, we created the bean instance and provided the required dependencies using setter injection.

This method will be called during bootstrap and the value will be returned as bean. By default, the method name is considered as the bean name.

 

In Step 6, we created the ApplicationContext using the AnnotationConfigApplicationContext class.  The class AnnotationConfigApplicationContext is used to process Java‐based configuration metadata classes.

Using the ApplicationContext, we lookup the “helloService” bean and then execute the getMessage() using it.

 

 

This example used java based configuration for the Hello World program. We can also used XML configuration based approach for the same.
 
You can refer following article for this :
 
Spring 4 Hello World using XML based configuration
 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

2 comments

  1. […] Spring 4 Hello World using XML based configuration   Spring 4 Hello World using java based configuration   […]

  2. […] Spring 4 Hello World using annotation based configuration […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: