In this article, I will be demonstrating how Spring Java-Based Configuration works. 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.
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>
Create the following bean classes:
MessageDAO:
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.
MessageService:
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 MessageDAO
to obtain the message and prints it. It has a private field corresponding to MessageDAO
and getter/setter methods for it.
Create a class called MyConfiguration as follows:
@Configuration
public class MyConfiguration {
@Bean
public MessageDAO messageDAO() {
MessageDAO bean = new MessageDAO();
return bean;
}
@Bean
public MessageService messageService() {
MessageService bean = new MessageService();
bean.setMessageDAO(messageDAO());
return bean;
}
}
The MyConfiguration
class has the @Configuration
annotation. This indicates that this class contains the configuration metadata. This has two methods marked with the @Bean
annotation. The names of the method are the same as the bean names. Spring uses these methods to create the beans at startup.
Create a class Main.java with the following code:
public static void main(String args[]) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
MyConfiguration.class);
MessageService messageService = applicationContext.getBean("messageService", MessageService.class);
messageService.printMessage();
}
The org.springframework.context..ApplicationContext
interface represents the Spring Container. This code first creates a AnnotationConfigApplicationContext
instance which is an implementation of the ApplicationContext
interface. There are several other implementations too. The AnnotationConfigApplicationContext
is used in case of Java/annotation configuration.
ApplicationContext
has a method getBean
. The code invokes this method in order to obtain theMessageService
. The code then invokes the printMessage
method.
So on execution, this code prints the following output:
Hello World
So in this article, we saw a Spring Java-Based configuration example. We saw how to configure a standalone Spring application via a Java class configuration. We used Eclipse and Maven in this example.