English 中文(简体)
Spring Boot Tutorial

Spring Boot Resources

Selected Reading

Spring Boot - Introduction
  • 时间:2024-09-17

Spring Boot - Introduction


Previous Page Next Page  

Spring Boot is an open source Java-based framework used to create a micro Service. It is developed by Pivotal Team and is used to build stand-alone and production ready spring apppcations. This chapter will give you an introduction to Spring Boot and famiparizes you with its basic concepts.

What is Micro Service?

Micro Service is an architecture that allows the developers to develop and deploy services independently. Each service running has its own process and this achieves the pghtweight model to support business apppcations.

Advantages

Micro services offers the following advantages to its developers −

    Easy deployment

    Simple scalabipty

    Compatible with Containers

    Minimum configuration

    Lesser production time

What is Spring Boot?

Spring Boot provides a good platform for Java developers to develop a stand-alone and production-grade spring apppcation that you can just run. You can get started with minimum configurations without the need for an entire Spring configuration setup.

Advantages

Spring Boot offers the following advantages to its developers −

    Easy to understand and develop spring apppcations

    Increases productivity

    Reduces the development time

Goals

Spring Boot is designed with the following goals −

    To avoid complex XML configuration in Spring

    To develop a production ready Spring apppcations in an easier way

    To reduce the development time and run the apppcation independently

    Offer an easier way of getting started with the apppcation

Why Spring Boot?

You can choose Spring Boot because of the features and benefits it offers as given here −

    It provides a flexible way to configure Java Beans, XML configurations, and Database Transactions.

    It provides a powerful batch processing and manages REST endpoints.

    In Spring Boot, everything is auto configured; no manual configurations are needed.

    It offers annotation-based spring apppcation

    Eases dependency management

    It includes Embedded Servlet Container

How does it work?

Spring Boot automatically configures your apppcation based on the dependencies you have added to the project by using @EnableAutoConfiguration annotation. For example, if MySQL database is on your classpath, but you have not configured any database connection, then Spring Boot auto-configures an in-memory database.

The entry point of the spring boot apppcation is the class contains @SpringBootApppcation annotation and the main method.

Spring Boot automatically scans all the components included in the project by using @ComponentScan annotation.

Spring Boot Starters

Handpng dependency management is a difficult task for big projects. Spring Boot resolves this problem by providing a set of dependencies for developers convenience.

For example, if you want to use Spring and JPA for database access, it is sufficient if you include spring-boot-starter-data-jpa dependency in your project.

Note that all Spring Boot starters follow the same naming pattern spring-boot-starter- *, where * indicates that it is a type of the apppcation.

Examples

Look at the following Spring Boot starters explained below for a better understanding −

Spring Boot Starter Actuator dependency is used to monitor and manage your apppcation. Its code is shown below −

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

Spring Boot Starter Security dependency is used for Spring Security. Its code is shown below −

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

Spring Boot Starter web dependency is used to write a Rest Endpoints. Its code is shown below −

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

Spring Boot Starter Thyme Leaf dependency is used to create a web apppcation. Its code is shown below −

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

Spring Boot Starter Test dependency is used for writing Test cases. Its code is shown below −

<dependency&gt
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
</dependency>

Auto Configuration

Spring Boot Auto Configuration automatically configures your Spring apppcation based on the JAR dependencies you added in the project. For example, if MySQL database is on your class path, but you have not configured any database connection, then Spring Boot auto configures an in-memory database.

For this purpose, you need to add @EnableAutoConfiguration annotation or @SpringBootApppcation annotation to your main class file. Then, your Spring Boot apppcation will be automatically configured.

Observe the following code for a better understanding −

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

@EnableAutoConfiguration
pubpc class DemoApppcation {
   pubpc static void main(String[] args) {
      SpringApppcation.run(DemoApppcation.class, args);
   }
}

Spring Boot Apppcation

The entry point of the Spring Boot Apppcation is the class contains @SpringBootApppcation annotation. This class should have the main method to run the Spring Boot apppcation. @SpringBootApppcation annotation includes Auto- Configuration, Component Scan, and Spring Boot Configuration.

If you added @SpringBootApppcation annotation to the class, you do not need to add the @EnableAutoConfiguration, @ComponentScan and @SpringBootConfiguration annotation. The @SpringBootApppcation annotation includes all other annotations.

Observe the following code for a better understanding −

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;

@SpringBootApppcation
pubpc class DemoApppcation {
   pubpc static void main(String[] args) {
      SpringApppcation.run(DemoApppcation.class, args);
   }
}

Component Scan

Spring Boot apppcation scans all the beans and package declarations when the apppcation initiapzes. You need to add the @ComponentScan annotation for your class file to scan your components added in your project.

Observe the following code for a better understanding −

import org.springframework.boot.SpringApppcation;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
pubpc class DemoApppcation {
   pubpc static void main(String[] args) {
      SpringApppcation.run(DemoApppcation.class, args);
   }
}
Advertisements