Spring Boot 2 Annotation Configuration Example - RameshMF/spring-boot-developers-guide GitHub Wiki
In this article, we will quickly discuss how to develop a simple Spring boot 2 application using annotation based configuration.
We use @Controller, @Service, @Repository, and @Component annotations to develop spring boot 2 standalone in-memory application.
- 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)
Let's quickly create a Spring Boot application using Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator.
package structure diagram
<?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>
public interface MessageService {
public void sendMsg(String message);
}
import org.springframework.stereotype.Service;
@Service("EmailService")
public class EmailService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
import org.springframework.stereotype.Service;
@Service("SMSService")
public class SMSService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
import org.springframework.stereotype.Service;
@Service("TwitterService")
public class TwitterService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
public interface UserService {
public void processMsg(String message);
}
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;
@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);
}
}
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.
package net.guides.springboot2.springboot2annotationconfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import net.guides.springboot2.springboot2annotationconfig.service.UserService;
@SpringBootApplication
public class Springboot2AnnotationConfigApplication {
private static Logger LOG = LoggerFactory.getLogger(Springboot2AnnotationConfigApplication.class);
public static void main(String[] args) {
LOG.info("STARTING THE APPLICATION");
ApplicationContext applicationContext = SpringApplication.run(Springboot2AnnotationConfigApplication.class,
args);
UserService userService = applicationContext.getBean(UserService.class);
userService.processMsg("twitter message sending ");
LOG.info("APPLICATION FINISHED");
}
}