Spring Boot - 简介

Spring Boot是一个基于Java的开源框架,用于创建微服务.它由Pivotal Team开发,用于构建独立的生产就绪弹簧应用.本章将向您介绍Spring Boot,并使您熟悉其基本概念.

什么是Micro Service?

Micro Service是一种架构这允许开发人员独立开发和部署服务.每个运行的服务都有自己的流程,这实现了轻量级模型以支持业务应用程序.

优势

微服务为开发人员提供以下优势 :

  • 轻松部署

  • 简单的可扩展性

  • 与容器兼容

  • 最低配置

  • 生产时间较短

什么是Spring Boot?

Spring Boot为Java开发人员提供了一个很好的平台,可以开发一个独立的,生产级的spring应用程序,你可以运行.您可以开始使用最少的配置,而无需进行整个Spring配置设置.

优点

Spring Boot为开发人员提供以下优势 : 去;

  • 易于理解和开发弹簧应用程序

  • 提高生产力

  • 缩短开发时间

目标

Spring Boot的设计具有以下目标和减号;

  • 避免在Spring中进行复杂的XML配置

  • 在一个开发生产就绪的Spring应用程序中更简单的方法

  • 减少开发时间并独立运行应用程序

  • 提供更简便的应用程序入门方式

为什么选择Spring Boot?

你可以选择Spring Boot,因为它提供的功能和好处在这里给出 :

  • 它提供这是一种配置Java Bean,XML配置和数据库事务的灵活方法.

  • 它提供了强大的批处理和管理REST端点.

  • 在Spring Boot中,一切都是自动配置的;不需要手动配置.

  • 它提供基于注释的弹簧应用程序

  • 缓解依赖管理

  • 它包括嵌入式Servlet容器

如何它有效吗?

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

spring boot的入口点application是包含 @SpringBootApplication 注释的类和main方法.

Spring Boot使用 @ComponentScan

Spring启动器

处理依赖关系管理对于大型项目来说是一项艰巨的任务. Spring Boot通过为开发人员提供一组依赖项来解决这个问题.

例如,如果要使用Spring和JPA进行数据库访问,只要包含就足够了项目中的spring-boot-starter-data-jpa 依赖项.

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

示例

请查看下面介绍的以下Spring Boot启动器更好地理解 :

Spring Boot Starter Actuator依赖用于监控和管理您的应用程序.它的代码显示在下面 :

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

Spring Boot Starter安全性依赖性用于Spring Security.它的代码显示在下面 :

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

Spring Boot Starter Web依赖用于编写Rest端点.它的代码显示在下面 :

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

Spring Boot Starter Thyme Leaf依赖用于创建Web应用程序.它的代码显示在下面 :

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

Spring Boot Starter测试依赖项用于编写测试用例.它的代码显示在下面 :

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

自动配置

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

为此,您需要将 @EnableAutoConfiguration 注释或 @SpringBootApplication 注释添加到主类文件中.然后,您的Spring Boot应用程序将自动配置.

观察以下代码以便更好地理解 :

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应用程序

Spring Boot应用程序的入口点该类包含 @SpringBootApplication 注释.该类应该具有运行Spring Boot应用程序的主要方法. @SpringBootApplication 注释包括自动配置,组件扫描和Spring Boot配置.

如果您在课程中添加了 @SpringBootApplication 注释,您不需要添加 @EnableAutoConfiguration,@ ComponentScan @SpringBootConfiguration 注释. @SpringBootApplication 注释包括所有其他注释.

观察以下代码以便更好地理解 :

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);
   }
}

组件扫描

Spring Boot应用程序扫描所有bean和包应用程序初始化时的声明.您需要为类文件添加 @ComponentScan 注释,以扫描项目中添加的组件.

观察以下代码以便更好地理解 :

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);
   }
}