Spring Boot 简明教程

Spring Boot - Introduction

Spring Boot 是一个用于创建微服务的开源 Java 框架。它由 Pivotal Team 开发,用于构建独立且可用于生产的 Spring 应用程序。本章将为您介绍 Spring Boot,并帮助您熟悉它的基本概念。

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 applications. This chapter will give you an introduction to Spring Boot and familiarizes 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 lightweight model to support business applications.

Advantages

微服务为其开发人员提供了以下优势−

Micro services offers the following advantages to its developers −

  1. Easy deployment

  2. Simple scalability

  3. Compatible with Containers

  4. Minimum configuration

  5. Lesser production time

What is Spring Boot?

Spring Boot 为 Java 开发人员提供了一个非常好的平台,用于开发您可以在 just run 的独立而且可用于生产的 Spring 应用程序。您可以在无需进行整个 Spring 配置设置的情况下开始使用最低配置。

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

Advantages

Spring Boot为其开发人员提供了以下优势−

Spring Boot offers the following advantages to its developers −

  1. Easy to understand and develop spring applications

  2. Increases productivity

  3. Reduces the development time

Goals

Spring Boot 基于以下目标设计−

Spring Boot is designed with the following goals −

  1. To avoid complex XML configuration in Spring

  2. To develop a production ready Spring applications in an easier way

  3. To reduce the development time and run the application independently

  4. Offer an easier way of getting started with the application

Why Spring Boot?

您可以根据这里提供的特性和优势选择 Spring Boot−

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

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

  2. It provides a powerful batch processing and manages REST endpoints.

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

  4. It offers annotation-based spring application

  5. Eases dependency management

  6. It includes Embedded Servlet Container

How does it work?

Spring Boot 根据您使用 @EnableAutoConfiguration 注释添加到项目的依赖项自动配置您的应用程序。例如,如果 MySQL 数据库在您的类路径中,但是您还未配置任何数据库连接,那么 Spring Boot 会自动配置一个内存数据库。

Spring Boot automatically configures your application 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.

Spring 引导应用程序的入口点是包含 @SpringBootApplication 注解和主方法的类。

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

Spring 引导使用 @ComponentScan 注解自动扫描项目中包含的所有组件。

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

Spring Boot Starters

处理依赖管理对于大型项目而言是一项艰巨的任务。Spring Boot 通过为开发者提供一组依赖项来解决这个问题。

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

例如,如果您想使用 Spring 和 JPA 访问数据库,那么只需要在项目中包含 spring-boot-starter-data-jpa 依赖项就足够了。

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.

请注意,所有 Spring 引导启动器都遵循相同的命名模式 spring-boot-starter- *,其中 * 表示它是应用程序的类型。

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

Examples

查看下面解释的以下 Spring 引导启动器以更好地理解 −

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

Spring Boot Starter Actuator dependency 用于监控和管理您的应用程序。其代码如下所示 −

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

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

Spring Boot Starter Security dependency 用于 Spring Security。其代码如下所示 −

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 用于编写 REST 端点。其代码如下所示 −

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

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

Spring Boot Starter Thyme Leaf dependency 用于创建 Web 应用程序。其代码如下所示 −

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

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

Spring Boot Starter Test dependency 用于编写测试用例。其代码如下所示 −

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

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

Auto Configuration

Spring Boot 自动配置会根据您在项目中添加的 JAR 依赖项自动配置您的 Spring 应用程序。例如,如果 MySQL 数据库在您的类路径中,但您尚未配置任何数据库连接,那么 Spring Boot 会自动配置内存数据库。

Spring Boot Auto Configuration automatically configures your Spring application 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.

为此,您需要向您的主类文件添加 @EnableAutoConfiguration 注解或 @SpringBootApplication 注解。然后,您的 Spring 引导应用程序将被自动配置。

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

观察以下代码以更好地理解 −

Observe the following code for a better understanding −

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

@EnableAutoConfiguration
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

Spring Boot Application

Spring 引导应用程序的入口点是包含 @SpringBootApplication 注解的类。此类应具有运行 Spring 引导应用程序的主方法。 @SpringBootApplication 注解包括自动配置、组件扫描和 Spring 引导配置。

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

如果您向类中添加 @SpringBootApplication 注解,则无需添加 @EnableAutoConfiguration, @ComponentScan@SpringBootConfiguration 注解。 @SpringBootApplication 注解包含所有其他注解。

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

观察以下代码以更好地理解 −

Observe the following code for a better understanding −

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

Component Scan

当应用程序初始化时,Spring 引导应用程序扫描所有 Bean 和包声明。您需要为此类文件添加 @ComponentScan 注解以扫描项目中添加的组件。

Spring Boot application scans all the beans and package declarations when the application initializes. 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.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}