zoukankan      html  css  js  c++  java
  • 初识 Spring Boot-Spring Boot教程深入浅出系列

    其他教程
    Spring Boot 配置-Spring Boot教程深入浅出系列
    自定义 Jackson ObjectMapper-Spring Boot教程深入浅出系列
    Spring Boot Actuator 介绍-Spring Boot教程深入浅出系列

    spring cloud 入门教程

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

    什么是微服务?

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

    优点

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

    • 轻松部署
    • 简单的可扩展性
    • 与容器兼容
    • 最低配置
    • 更少的生产时间

    什么是 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应用的入口点是包含@SpringBootApplication注解和main方法的类。

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

    Spring Boot 启动器

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

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

    请注意,所有 Spring Boot starter 都遵循相同的命名模式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 Security 依赖项用于 Spring Security。它的代码如下所示 -

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

    Spring Boot Starter web 依赖用于编写一个 Rest Endpoints。它的代码如下所示 -

    <dependency>
       <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 Test 依赖用于编写测试用例。它的代码如下所示 -

    <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 应用程序的 main 方法。@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);
       }
    }
  • 相关阅读:
    try,except,finally的用法
    python实现蓝牙通信
    分布式全局ID的几种生成方案
    为什么要两次调用encodeURI来解决乱码问题
    jenkins配置到gitlab拉代码
    查看IOS-app证书到期时间
    使用SSH方式实现Git远程连接GitHub/gitlab
    Git 分支
    jenkins构建后操作archive the artfacts的用法
    MAC 安装jenkins
  • 原文地址:https://www.cnblogs.com/BlogNetSpace/p/15165931.html
Copyright © 2011-2022 走看看