Spring

Spring Annotation-Based Configuration Example

Posted On
Posted By admin

In this article, I will be demonstrating how to configure a standalone Spring application using annotation-based configuration. I will be using Eclipse and Maven. In order to get a basic introduction to Spring, you can check out this article. In order to understand how to configure a standalone Spring application using XML configuration, you can refer to this article. In order to understand how to configure a standalone Spring application using Java configuration, you can refer to this article.

Project Set-up

Step 1 – Create a new Maven Project in Eclipse. You can refer to this article on how to create a Maven project in Eclipse.

Step 2 – Add Spring dependencies to the maven pom file. You can add the following:

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.2.RELEASE</version>
    </dependency>
  </dependencies>

Creating Beans

Create the following bean classes:

MessageDAO:

@Repository
public class MessageDAO {
  
  public String getMessage() {
    return "Hello World";
  }

}

MessageDAO is a simple class that has only one method getMessage()This returns a String value.  It has the @Repositoryannotation. This extends from the org.springframework.stereotype.Componentannotation. So it indicates that MessageDAO is a Spring component.

MessageService:

@Service
public class MessageService {
  
  private MessageDAO messageDAO;

  public void printMessage() {
    String message = messageDAO.getMessage();
    System.out.println(message);
  }

  public MessageDAO getMessageDAO() {
    return messageDAO;
  }

  public void setMessageDAO(MessageDAO messageDAO) {
    this.messageDAO = messageDAO;
  }

}

MessageService has a method printMessage. It uses the MessageDAOto obtain the message and prints it. It has a private field corresponding to MessageDAOand getter/setter methods for it.It has the @Serviceannotation.  Like @Repositorythis also extends from the org.springframework.stereotype.Componentannotation and indicates that MessageService is a Spring component.

ThemessageDAOfield has the @Autowiredannotation.  This tell Spring that the container should configure this dependency. So Spring tried to find a bean of MessageDAOtype. If it finds a matching bean, it injects it by invoking the setter method.

Configuration File

Create a file ApplicationContext.xml in the src/main/resources folder as follows:

<?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/context
    http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
	<context:component-scan base-package="learnjava.demo"/>
</beans>

 

This ApplicationContext.xml is the spring configuration file.  Since we are using annotation based configuration, the configuration file does not contain any bean definitions as explained here.  Instead, it has the <context:component-scan>tag. So Spring automatically scans all the packages under the base-package, which in this case is the “learnjava.demo” package.  It identifies all classes that have the @Componentannotation and its derivatives. These are treated as beans and created by the Spring container.  So since the MessageDAO

has the @Repositoryannotation, Spring treats it as a bean and injects it into the MessageServiceclass.  Similarly, the MessageService

has the @Serviceannotation and so that is created as a bean as well.

Writing Main Code

Create a class Main.java with the following code:

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
MessageService messageService = applicationContext.getBean("messageService", MessageService.class);
messageService.printMessage();

The org.springframework.context..ApplicationContextinterface represents the Spring Container. This code first creates a ClassPathXmlApplicationContextinstance which is an implementation of the ApplicationContextinterface.  There are several other implementations too. The ClassPathXmlApplicationContextis used in case of an XML configuration.

ApplicationContexthas a method getBean. The code invokes this method in order to obtain theMessageService. The code then invokes the printMessagemethod.

So on execution, this code prints the following output:

Hello World

Further Learning

Spring MasterClass
Spring Tutorial For Beginners
Step by Step Spring MVC Tutorial
Spring Framework in Easy Steps

Conclusion

So in this article, we saw a Spring Java annotation configuration example. We saw how to configure a standalone Spring application via annotation-based class configuration.

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment