Spring Boot简介及快速搭建

1. 简介

SpringBoot框架基于Spring4.0设计,由Piovotal公司设计。
VMware+EMC+通用电气->Pivotal公司
2014年 Spring Boot1.0,基于Spring4.0
2015年 Spring Cloud
2018年 Spring Boot2.0,基于Spring5.0

Spring基于Spring开发,不仅继承了Spring框架原有的优秀特性,它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合进一步简化了Spring应用的整个搭建和开发过程。其设计目的是用来简化Spring应用的初始化搭建和开发过程。怎么简化的呢,通过提供默认配置等方式,让我们更容易使用。

关于SpringBoot有一句话就是:约定大于配置,采用Spring Boot可以大大简化开发模式,它集成了大量常用的第三方库配置,如Redis, MongoDB, Dubbo, kafka, ES等等。

优点

  • 快速构建一个独立的Spring应用程序
  • 嵌入的Tomcat,Jetty或者Undertow,无需部署WAR文件
  • 提供starter POMS来简化Maven配置和减少版本冲突所带来的问题
  • 对Spring和第三方提供默认配置,也可以修改默认值,简化框架配置
  • 提供生产就绪功能,如指标、健康检查和外部配置
  • 无需配置XML,无代码生成,开箱即用

2. 为什么使用Spring Boot?

Spring Cloud带动了Spring Boot, Spring Boot成就了Spring Cloud。

3. 快速开始Spring Boot的Hello World程序

  • 基本结构
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>
  • Example.java
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return String.format("hello world!");
    }
}

  • Main.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

原文地址:https://www.cnblogs.com/pangqianjin/p/14661954.html