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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@Repository
public class MessageDAO {
public String getMessage() {
return "Hello World";
}
}
@Repository public class MessageDAO { public String getMessage() { return "Hello World"; } }
@Repository
public class MessageDAO {
  
  public String getMessage() {
    return "Hello World";
  }

}

MessageDAO is a simple class that has only one method

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

MessageService:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@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;
}
}
@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; } }
@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
printMessage. It uses the
MessageDAO
MessageDAOto obtain the message and prints it. It has a private field corresponding to
MessageDAO
MessageDAOand getter/setter methods for it.It has the
@Service
@Serviceannotation.  Like
@Repository
@Repositorythis also extends from the
org.springframework.stereotype.Component
org.springframework.stereotype.Componentannotation and indicates that MessageService is a Spring component.

The

messageDAO
messageDAOfield has the
@Autowired
@Autowiredannotation.  This tell Spring that the container should configure this dependency. So Spring tried to find a bean of
MessageDAO
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?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>
<?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>
<?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>
<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
@Component
@Componentannotation and its derivatives. These are treated as beans and created by the Spring container.  So since the
MessageDAO
MessageDAO

has the

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

has the

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

Writing Main Code

Create a class Main.java with the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
MessageService messageService = applicationContext.getBean("messageService", MessageService.class);
messageService.printMessage();
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); MessageService messageService = applicationContext.getBean("messageService", MessageService.class); messageService.printMessage();
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
MessageService messageService = applicationContext.getBean("messageService", MessageService.class);
messageService.printMessage();

The

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

ApplicationContext
ApplicationContexthas a method
getBean
getBean. The code invokes this method in order to obtain the
MessageService
MessageService. The code then invokes the
printMessage
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