Spring Boot Java Based Configuration Example

In this article, we will quickly discuss how to develop a simple Spring boot application using Java-based configuration.
 
We use @Configuration and @Bean annotations to develop a spring boot standalone in-memory application.

Overview

Spring provides @Configuration and @Bean annotations for Java-based configuration in the Spring boot application.

@SpringBootApplication annotation indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. 
This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Read more about @SpringBootApplication annotation on Spring Boot @SpringBootApplication Annotation with Example
You may also interested in Spring (not Spring boot)Java-based configuration example on Spring Java Based Configuration Example
Let me create a brand new Spring boot project that demonstrates how to configure Spring boot applications using Java-based configuration.

1. Setup

Let's quickly create a Spring Boot application using Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator.

Add this Maven dependency to the pom.xml file:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Here is the project structure or packaging structure for your reference:
 
In the next step, we will create a few service classes so that we can create Spring beans using @Bean Java-based configuration annotation.

2. Create Interfaces and Classes

In this example, we are sending messages using different services like SMSServiceTwitterService, and EmailService

MessageService.java

Let's create a MessageService interface with the following content:
public interface MessageService {
    public void sendMsg(String message);
}

EmailService.java

Let's create an EmailService class that implements the MessageService interface and its method:
public class EmailService implements MessageService {

    public void sendMsg(String message) {
        System.out.println(message);
    }
}

SMSService.java

Let's create the SMSService class that implements the MessageService interface and its method:
public class SMSService implements MessageService {

    public void sendMsg(String message) {
        System.out.println(message);
    }
}

TwitterService.java

Let's create the TwitterService class that implements the MessageService interface and its method:
public class TwitterService implements MessageService {

    public void sendMsg(String message) {
        System.out.println(message);
    }
}

UserService.java

Let's create a UserService interface with the following content:
public interface UserService {
    public void processMsg(String message);
}

UserServiceImpl.java

Let's create the UserServiceImpl class that implements the UserService interface and its method:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

public class UserServiceImpl implements UserService {

    @Autowired
    @Qualifier("TwitterService")
    private MessageService messageService;

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void processMsg(String message) {
        messageService.sendMsg(message);
    }
}
In the next step, we will create Spring beans for the above service classes using Bean Java-based configuration annotation.

3. Java-based Configuration

The main entry point class is annotated with @SpringBootApplication annotation so we can configure Spring beans in this class using @Bean annotation:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import net.guides.springboot2.springboot2javaconfig.service.EmailService;
import net.guides.springboot2.springboot2javaconfig.service.MessageProcessor;
import net.guides.springboot2.springboot2javaconfig.service.MessageProcessorImpl;
import net.guides.springboot2.springboot2javaconfig.service.MessageService;
import net.guides.springboot2.springboot2javaconfig.service.SMSService;
import net.guides.springboot2.springboot2javaconfig.service.TwitterService;

@SpringBootApplication
public class Springboot2JavaConfigApplication {

    @Bean(name = "emailService")
    public MessageService emailService() {
        return new EmailService();
    }

    @Bean(name = "smsService")
    public MessageService smsService() {
        return new SMSService();
    }

    @Bean(name = "twitterService")
    public MessageService twitterService() {
        return new TwitterService();
    }

    @Bean
    public MessageProcessor messageProcessor() {
        return new MessageProcessorImpl(twitterService());
    }

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(Springboot2JavaConfigApplication.class, args);
        MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
        userService.processMsg("twitter message sending ");
    }
}
Next, let's run this Spring boot application and you will see the output in the console:

Conclusion

We have used @Configuration and @Bean annotations to demonstrate Spring Java-based configuration and we haven't used @Service or @Component annotation in this example. (annotation-based configuration).
Check out the same example can be configured using XML based configuration on Spring Boot XML Configuration Example
Learn complete Spring Boot 2 on Spring Boot 2 Tutorial

Comments