Spring 4 Hello World using Annotation based approach

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

 

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: (Create a simple maven project)

 

Run the following command to create a Maven project :

 

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

 

This will create the project with name “SpringXMLApp”.

 

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

 

Here is how the basic pom.xml would like at this point:
 

<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>SpringXMLApp</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SpringXMLApp</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 will add spring dependency to it.

 

Step 2: (Add Spring dependency)

 

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>SpringXMLApp</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SpringXMLApp</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:(Import the project in Eclipse)

 

You can import the project in Eclipse in following ways:

 

  • Import the project as a Maven project

(OR)

  • Run mvn eclipse command as then import as normal project

 

If you need help importing the project to eclipse, refer the below article for the same :

Import Maven project in Eclipse

 

Step 4: (Define Spring beans)

 

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

 

Create an Interface HelloService and implementing class HelloServiceImpl. Let’s annotate HelloServiceImpl class with @Service as follows:

 

package com.topjavatutorial.spring;

public interface HelloService {

  public abstract HelloDAO getDao();

  public abstract void setDao(HelloDAO dao);

}

 

package com.topjavatutorial.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {
  private HelloDAO dao;

  /* (non-Javadoc)
   * @see com.topjavatutorial.spring.HelloService#getDao()
   */
  public HelloDAO getDao() {
    return dao;
  }

  /* (non-Javadoc)
   * @see com.topjavatutorial.spring.HelloService#setDao(com.topjavatutorial.spring.HelloDAO)
   */
  @Autowired
  public void setDao(HelloDAO dao) {
    this.dao = dao;
  }

}


 

Also, create a DAO interface HelloDAO and implementing class HelloDAOImpl. Let’s annotate the DAO class with @Repository annotation as follows :
 

package com.topjavatutorial.spring;

public interface HelloDAO {

  public abstract String getName();

}

 
package com.topjavatutorial.spring;

import org.springframework.stereotype.Repository;

@Repository
public class HelloDAOImpl implements HelloDAO {

  /* (non-Javadoc)
   * @see com.topjavatutorial.spring.HelloDAO#getName()
   */
  public String getName(){
    return "TopJavaTutorial";
  }
}


 

Step 5: (Create XML Spring config file)

 

Create a xml bean definition file beans.xml in src/main/resources folder.
 

If the folder does not exist in your maven project, manually create the folder and update the project. Refer below article for this :

Missing src/main/resources in maven project
 

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.topjavatutorial.spring"/>

</beans>

 

Step 6: (Create ApplicationContext from XML bean definitions)

 

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 XML bean definition file as the constructor argument.

 

package com.topjavatutorial.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
      HelloService helloBean = (HelloService) context.getBean("helloService");
      System.out.println("Hello " + helloBean.getDao().getName());
      context.close();
    }
}


 

Step 7: ( Run the program)

 

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

 

Hello TopJavaTutorial

 

spring xml config program output

Understanding this program

 

In step 4, we created a HelloService interface and implementing class HelloServiceImpl. We annotated it with @Service annotation. We also created a HelloDAO interface and HelloDAOImpl class implementing this interface and then annotated the class with @Repository annotation.

 


@Repository
public class HelloDAOImpl implements HelloDAO {
...
}


@Service
public class HelloServiceImpl implements HelloService {
...
}

 
We used @Service and @Repository annotation to define the beans. These two annotations extend from @Component annotation.

 
The @Autowired attribute informs Spring that the specified dependency should be satisfied from within the container.
 
In Step 5, we added the element in the bean config file.
 

<context:component-scan base-package="com.topjavatutorial.spring"/>

 
Spring scans the classes mentioned in the package and creates beans with required annotations and injects the dependencies.

 

In Step 6, we created the ApplicationContext using the XML bean definition file. The class ClassPathXmlApplicationContext is used to load the xml configuration metadata from the classpath.

 

Next, using the ApplicationContext, we lookup the “helloServiceImpl” bean and use it to invoke the getName() method of the DAO class.

 
 
This example used Annotation based approach for the Hello World program. You may also use Java or XML based approaches as explained in following articles :

 
Spring 4 Hello World using XML based configuration
 
Spring 4 Hello World using java based configuration
 
 

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

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