Spring Java-Based Configuration Example

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.

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:

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 MessageDAOto obtain the message and prints it. It has a private field corresponding to MessageDAOand getter/setter methods for it.

Configuration Class

Create a class called MyConfiguration as follows:

@Configurationpublic 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 MyConfigurationclass has the @Configurationannotation. This indicates that this class contains the configuration metadata. This has two methods marked with the @Beanannotation.   The names of the method are the same as the bean names. Spring uses these methods to create the beans at startup.

Writing Main Code

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..ApplicationContextinterface represents the Spring Container. This code first creates a AnnotationConfigApplicationContextinstance which is an implementation of the ApplicationContextinterface.  There are several other implementations too. The AnnotationConfigApplicationContext is used in case of Java/annotation 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

Edit

Further Learning

Conclusion

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.

Comments

Popular posts from this blog

Java 8 DoubleFunction Example

How to convert a List to a Set

ArrayList Vs LinkedList