Spring Boot, and the Spring Framework that Spring Boot is based on, is a great framework for developing microservices in Java.

Larsson, Magnus. Microservices with Spring Boot and Spring Cloud: Build resilient and scalable microservices using Spring Cloud, Istio, and Kubernetes, 2nd Edition (p. 67). Packt Publishing. Kindle Edition.

In this article, we will not discuss Microservices (this post about Introduction Microservices) here, we just share knowledge about fundamental Spring Boot.

In 2014, Spring Boot v1.0 was released as part of the Spring ecosystem, and as the documentation Spring Boot targets the fast development of production-ready applications.

Spring Boot introduces a new concept of the convention-over-configuration principle to configure our application, the convention means Spring Boot configures in a most-used way, it offers default configuration that can customize when needed, and most Spring Boot applications by default need minimal Spring configuration.

The convention-based mechanism using the @SpringBootApplication annotation in the application class, then by annotating the application with that annotation the following functionality will be provided:

  • It enables component scanning.

    The @ComponentScan annotation by default enables, it will scan the Spring Stereotype (e.g. @Component, @Service, and @Repository annotations) in the base package of the application class and all sub-packages, the bean will automatically able to be injected (e.g. @Autowired) in all packages.

    Otherwise, we should complement it for the packages are declared outside of the application package as the next snippet:

    package id.vimona.blog.springcontext;
      
    import ...
      
    @SpringBootApplication
    @ComponentScan("com.example.spring")
    public class SpringContextApplication implements ApplicationRunner {
      
        ...
    }
    
  • It enables autoconfiguration.

    For example, when our application added configuration of web dependency, Spring Boot configures Tomcat for a servlet container, cause in most cases, developers use this implementation similar to the next snippet:

    <dependencies>
      ...
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
     </dependencies>
    

    In the above maven pom.xml file, the application added spring-boot-starter-web then Spring Boot will choose the all dependencies right for us and automatically configure as the below setting:

  • Embedded Tomcat, Jetty web server.

    Web apps use HTTP Protocol to exchange data requests and responses from client and server, something to translate the request and response is a servlet container (or known as a web server). In a simple way, Tomcat and Jetty are a component that implements a servlet container to translate the HTTP message to a Java application.

    For Spring Boot, Tomcat is the convention for a servlet container.

Note: Refer to the previous post, I mentioned about @SpringBootApplication annotation in the snippet code. Since the Spring Boot annotation enables component scanning, therefore we can ignore redefining it in the configuration level, as long as the bean component is still in the same base package.

Reference:

  • Spilca, Laurentiu. Spring Start Here: Learn what you need and learn it well. Manning. Kindle Edition.
  • Larsson, Magnus. Microservices with Spring Boot and Spring Cloud: Build resilient and scalable microservices using Spring Cloud, Istio, and Kubernetes, 2nd Edition. Packt Publishing. Kindle Edition.
  • https://spring.io/projects/spring-boot#overview