Ticker

6/recent/ticker-posts

Spring Boot Tutorial - Part 1: Introduction

Spring Boot is an open-source Java Framework make it easy for develop stand-alone application and production ready that you can "just run". Created project with a few click and started project with minimum configurations.

Why Spring Boot?

As you know Spring Boot is still base on Spring Framework, but many junior developers always ask, why we choose Spring Boot?

I can give you one example real life, Are you planning to cook Tom Yum for your family in this evening? Imagine you mix your own ingredients, you might end up with awful flavor. Luckily, nowadays commercial  made Tom Yum Soup Powder. You can easily cook it.

From above example, mixing your own ingredients the same as using Spring Framework. You need to know each core components well and the way to combine it. Otherwise you will end up with a mess. Spring Boot is like Tom Yum Soup Powder you only need to know basic usage, all core components already mixed for you.

Key Features & Benefit 

  • Create stand-alone applications.
  • Increase development productivity.
  • Embed webserver(Tomcat, Jetty, Undertow) inside application, say no to WAR file.
  • Using Java annotation-based for configuration, rarely use XML configuration.
  • Easy ship your applications into container platform.
  • Provide opinionated 'starter' dependencies to simplify your build configuration.
  • Everything auto configuration, in some case you still use manual configuration to override.
  • Suitable for microservices applications, since it stand-alone and independence.

How magic happen?

The starting point of Spring Boot is a class contain annotation @SpringBootApplication with a Java main method.
@SpringBootApplication
public class Application {
	public static void main (String[] args) {
		SpringApplication.run(Application.class);
	}
}

Auto configuration annotations

@SpringBootApplication alone won't make magic happen, bellow are additional annotations:
  • @EnableAutoConfiguration, It will auto configure base your dependencies your add to into your project. For example, you add dependency spring-boot-starter-security on application startup, Spring Boot will generate in memory user for you.
  • @ComponentScan, this annotation use for scan all bean base on define package. By default Spring Boot will scan beans in project package only. For example, we create Spring Boot project with package cyou.bannalycode, so @ComponentScan will scan all beans under package cyou.bannalycode. Incase of we have third party library from our team and its declare under difference package, so we need to define @ComponentScan as below: 
@SpringBootApplication(scanBasePackages ={"cyou.bannalycode", "io.example"})
@ComponentScan(basePackages = {"cyou.bannalycode", "io.example"})
public class Application {
	public static void main (String[] args) {
		SpringApplication.run(Application.class);
	}
}

Spring Boot Starters

Spring Boot provide a convenience way on dependencies management, which mean we'll get a bundle of dependencies base our use case. For example, I need to config my project as web application, so without spring boot starter we need to add dozen of dependencies into our project. With spring boot starter we just add:
<dependency>  
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter-web</artifactId>  
</dependency>
Behind the scene these dependencies will auto include into your project:
<dependency>  
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter</artifactId>  
 <version>2.3.12.RELEASE</version><!--depend on your spring boot version-->
 <scope>compile</scope>  
</dependency>  
<dependency>  
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter-json</artifactId>  
 <version>2.3.12.RELEASE</version>  
 <scope>compile</scope>  
</dependency>  
<dependency>  
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter-tomcat</artifactId>  
 <version>2.3.12.RELEASE</version>  
 <scope>compile</scope>  
</dependency>  
<dependency>  
 <groupId>org.springframework</groupId>  
 <artifactId>spring-web</artifactId>  
 <version>5.2.15.RELEASE</version>  
 <scope>compile</scope>  
</dependency>  
<dependency>  
 <groupId>org.springframework</groupId>  
 <artifactId>spring-webmvc</artifactId>  
 <version>5.2.15.RELEASE</version>  
 <scope>compile</scope>  
</dependency>
Here are most common use spring-boot-starters deps in our daily project:
  • spring-boot-starter-web
  • spring-boot-starter-security
  • spring-boot-starter-data-jpa
  • spring-boot-starter-logging
  • spring-boot-starter-amqp
Share we us, what is your most use starter deps?

Post a Comment

0 Comments