In this article, we will quickly discuss how to develop a simple Spring boot 2 application using Java-based configuration.
We use @Configuration and @Bean annotations to develop spring boot 2 standalone in-memory application. Note that we haven't used @Service or @Component annotation in this example. (annotation based configuration)
@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
We will build a simple maven based Spring Boot project. let's start with the tools and technologies used in this example.
Tools and Technologies Used
- Spring Boot - 2.0.4.RELEASE
- JDK - 1.8 or later
- Spring Framework - 5.0.8 RELEASE
- Maven - 3.2+
- IDE - Eclipse or Spring Tool Suite (STS)
Create, Import and Project Structure of Spring Boot application
Let's quickly create a Spring Boot application using Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator. Download project and import in your IDE. Below is the project structure for your reference.
The pom.xml File
Refer below maven Spring Boot starter dependencies.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.guides.springboot2</groupId>
<artifactId>springboot2-annotation-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot2-annotation-config</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
In the next step, we will create few service classes so that we can create Spring beans using @Bean Java-based configuration annotation.
Create few Services using @Service Annotation
Note that we haven't used @Service or @Component annotations.
MessageService.java
public interface MessageService {
public void sendMsg(String message);
}
EmailService.java
import org.springframework.stereotype.Service;
public class EmailService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
SMSService.java
import org.springframework.stereotype.Service;
public class SMSService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
TwitterService.java
import org.springframework.stereotype.Service;
public class TwitterService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
UserService.java
public interface UserService {
public void processMsg(String message);
}
UserServiceImpl.java
package net.guides.springboot2.springboot2annotationconfig.service;
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 next step, we will create Spring beans for above service classes using Bean Java-based configuration annotation.
Configuration Spring beans and Running Application
This spring boot application has an entry point Java class called SpringBootCrudRestApplication.java with the public static void main(String[] args) method, which you can run to start the application.
In this file, we have used @Bean annotation to create bean programmatically(using new keyword).
package net.guides.springboot2.springboot2annotationconfig;
package net.guides.springboot2.springboot2javaconfig;
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 ");
}
}
Output
Note that 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
Post a Comment