目录

Spring Boot - 介绍

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

什么是微服务?

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

优势 (Advantages)

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

  • Easy deployment
  • 简单的可扩展性
  • Compatible with Containers
  • 最低配置
  • Lesser production time

什么是Spring Boot?

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

优势 (Advantages)

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

  • 易于理解和开发弹簧应用
  • Increases productivity
  • 缩短开发时间

Goals

Spring Boot的设计目标如下 -

  • 避免在Spring中进行复杂的XML配置
  • 以更简单的方式开发生产就绪的Spring应用程序
  • 减少开发时间并独立运行应用程序
  • 提供一种更简单的应用程序入门方式

为什么选择Spring Boot?

您可以选择Spring Boot,因为它提供的功能和优点如下所示 -

  • 它提供了一种灵活的方法来配置Java Bean,XML配置和数据库事务。

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

  • 在Spring Boot中,一切都是自动配置的; 无需手动配置。

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

  • 简化依赖管理

  • 它包括嵌入式Servlet容器

它是如何工作的?

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

spring boot应用程序的入口点是包含@SpringBootApplication注释和main方法的类。

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

Spring Boot Starters

处理依赖管理对于大项目来说是一项艰巨的任务。 Spring Boot通过提供一组依赖项来解决此问题,以方便开发人员。

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

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

例子 (Examples)

请看下面的Spring Boot启动器,以便更好地理解 -

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

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

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

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

Spring Boot Starter web dependency用于编写Rest端点。 其代码如下所示 -

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

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

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

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

<dependency>
   <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 Application的入口点是包含@SpringBootApplication注释的类。 该类应具有运行Spring Boot应用程序的主要方法。 @SpringBootApplication注释包括自动配置,组件扫描和Spring Boot配置。

如果将@SpringBootApplication批注添加到类中,则无需添加@EnableAutoConfiguration, @ComponentScan @SpringBootConfiguration@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);
   }
}
↑回到顶部↑
WIKI教程 @2018