Springboot基础知识(01)- Spring Boot 简介、配置 Spring Boot 和 Spring Initializr

 

注:开始阅读 Spring Boot 系列文章之前,读者应该已经掌握了 Java 基础知识、Web 基础知识、Spring 框架和 Spring MVC。另外,本系列文章中的所有实例都是使用 IntelliJ IDEA + Apache Maven 配置和编译的,所以还需要对 IntelliJ IDEA 和 Apache Maven 有基本的了解。

 

1. Spring Boot 简介

    Spring 应用需要进行大量的配置,各种 XML 配置和注解配置让人眼花缭乱,且极容易出错。

    为了简化 Spring 应用的搭建和开发过程,Pivotal 团队在 Spring 基础上提供了一套全新的开源的框架,它就是 Spring Boot。

    Spring Boot 具有 Spring 一切优秀特性,Spring 能做的事,Spring Boot 都可以做,而且使用更加简单,功能更加丰富,性能更加稳定而健壮。随着近些年来微服务技术的流行,Spring Boot 也成为了时下炙手可热的技术。

    Spring Boot 提供了大量开箱即用(out-of-the-box)的依赖模块,例如 spring-boot-starter-redis、spring-boot-starter-data-mongodb 和 spring-boot-starter-data-elasticsearch 等。这些依赖模块为 Spring Boot 应用提供了大量的自动配置,使得 Spring Boot 应用只需要非常少量的配置甚至零配置,便可以运行起来,让开发人员从 Spring 的“配置地狱”中解放出来,有更多的精力专注于业务逻辑的开发。

    Spring Boot 具有以下特点:

        (1) 独立运行的 Spring 项目

            Spring Boot 可以以 jar 包的形式独立运行,Spring Boot 项目只需通过命令 “java –jar xxx.jar” 即可运行。

        (2) 内嵌 Servlet 容器

            Spring Boot 使用嵌入式的 Servlet 容器(例如 Tomcat、Jetty 或者 Undertow 等),应用无需打成 WAR 包 。

        (3) 提供 starter 简化 Maven 配置

            Spring Boot 提供了一系列的“starter”项目对象模型(POMS)来简化 Maven 配置。

        (4) 提供了大量的自动配置

            Spring Boot 提供了大量的默认自动配置,来简化项目的开发,开发人员也通过配置文件修改默认配置。

        (5) 自带应用监控

            Spring Boot 可以对正在运行的项目提供监控。

        (6) 无代码生成和 xml 配置

            Spring Boot 不需要任何 xml 配置即可实现 Spring 的所有配置。


2. 创建 Maven Quickstart 项目

    1) 系统环境

        Spring Boot 版本及其环境配置要求如下表。

            Spring Boot      2.x
            JDK                 8.0 及以上版本
            Maven              3.x
            IntelliJ IDEA      14.0 以上

        本文将在 Windows 下使用 IntelliJ IDEA 和 Apache Maven 创建一个简单的 Maven Quickstart 程序。在开始之前,确保已经正确搭建了 Spring 开发环境,参考 “ Spring基础知识(1)- Spring简介、Spring体系结构和开发环境配置 ”。

        Windows版本 : Windows 10 Home (20H2)   
        IntelliJ IDEA:Community Edition for Windows 2020.1.4
        Apache Maven:3.8.1

    2) 运行 IDEA 创建项目

        点击菜单 New 创建 Project:
        
          New Project -> Project Type: Maven -> Project SDK: 1.8 -> Check "Create from archtype" -> select "org.apache.maven.archtypes:maven-archtype-quickstart" -> Next

              Name: SpringbootBasic
              GroupId: com.example
              ArtifactId: SpringbootBasic

          -> Finish

    3) 生成的项目目录结构和文件

  (1) 目录结构

        |-- src
        |   |-- main
        |   |     |-- java
        |   |       |-- com
        |   |            |-- example
        |   |                   |-- App.java
        |   |-- test
        |        |-- java
        |               |-- com
        |                    |-- example
        |                           |-- AppTest.java
        |-- pom.xml

 

        (2) App.java 代码

1         package com.example;
2 
3         public class App {
4             public static void main( String[] args ) {
5                 System.out.println( "Hello World!" );
6             }
7         }


            (3) AppTest.java 代码

 1         package com.example;
 2 
 3         import static org.junit.Assert.assertTrue;
 4 
 5         import org.junit.Test;
 6 
 7         public class AppTest {
 8 
 9             @Test
10             public void shouldAnswerWithTrue() {
11                 assertTrue( true );
12             }
13         }


            (4) pom.xml 代码

 1         <?xml version="1.0" encoding="UTF-8"?>
 2 
 3         <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5         <modelVersion>4.0.0</modelVersion>
 6 
 7         <groupId>com.example</groupId>
 8         <artifactId>SpringbootBasic</artifactId>
 9         <version>1.0-SNAPSHOT</version>
10 
11         <name>SpringbootBasic</name>
12         <!-- FIXME change it to the project's website -->
13         <url>http://www.example.com</url>
14 
15         <properties>
16             <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17             <maven.compiler.source>1.7</maven.compiler.source>
18             <maven.compiler.target>1.7</maven.compiler.target>
19         </properties>
20 
21         <dependencies>
22             <dependency>
23                 <groupId>junit</groupId>
24                 <artifactId>junit</artifactId>
25                 <version>4.11</version>
26                 <scope>test</scope>
27             </dependency>
28         </dependencies>
29 
30         <build>
31             <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
32             <plugins>
33                 <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
34                 <plugin>
35                   <artifactId>maven-clean-plugin</artifactId>
36                   <version>3.1.0</version>
37                 </plugin>
38                 <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
39                 <plugin>
40                   <artifactId>maven-resources-plugin</artifactId>
41                   <version>3.0.2</version>
42                 </plugin>
43                 <plugin>
44                   <artifactId>maven-compiler-plugin</artifactId>
45                   <version>3.8.0</version>
46                 </plugin>
47                 <plugin>
48                   <artifactId>maven-surefire-plugin</artifactId>
49                   <version>2.22.1</version>
50                 </plugin>
51                 <plugin>
52                   <artifactId>maven-jar-plugin</artifactId>
53                   <version>3.0.2</version>
54                 </plugin>
55                 <plugin>
56                   <artifactId>maven-install-plugin</artifactId>
57                   <version>2.5.2</version>
58                 </plugin>
59                 <plugin>
60                   <artifactId>maven-deploy-plugin</artifactId>
61                   <version>2.8.2</version>
62                 </plugin>
63                 <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
64                 <plugin>
65                   <artifactId>maven-site-plugin</artifactId>
66                   <version>3.7.1</version>
67                 </plugin>
68                 <plugin>
69                   <artifactId>maven-project-info-reports-plugin</artifactId>
70                   <version>3.0.0</version>
71                 </plugin>
72             </plugins>
73             </pluginManagement>
74         </build>
75         </project>

 

    4) 编译运行

        (1) 修改 pom.xml

1             <properties>
2                 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3                 <maven.compiler.source>1.8</maven.compiler.source>
4                 <maven.compiler.target>1.8</maven.compiler.target>
5             </properties>

            把 Maven 推荐 JDK 版本从 1.7 改成 1.8。

        (2) 可以选择 Java SDK 版本

            在IDE项目列表 -> External Libraries -> <1.8>, 在 "<1.8>" 上点击鼠标右键 跳出菜单,选择 Open Library Settings 菜单项,跳出对话框。
        
            在对话框上 Platform Settings -> SDKS 选择或添加 <1.8>

                Name: 1.8
                JDK home path: select 1.8 SDK or download a SDK

        (3) Run App.main()

            Open App.java, click mouse right button, select Run "App.main()"

        (4) Edit Configurations

            Click "+" add new configuration -> Select "Application"
                Name: SpringbootBasic
                Main class: com.example.App
            -> Apply / OK

        Click Run "SpringbootBasic"

              Hello World!

    5)使用 spring-boot-maven-plugin 插件运行打包

        (1) 修改 pom.xml

 1             <build>
 2                 ...
 3                 <plugins>
 4                     <plugin>
 5                         <groupId>org.springframework.boot</groupId>
 6                         <artifactId>spring-boot-maven-plugin</artifactId>
 7                         <configuration>
 8                             <mainClass>com.example.App</mainClass>
 9                             <layout>JAR</layout>
10                         </configuration>
11                         <executions>
12                         <execution>
13                             <goals>
14                             <goal>repackage</goal>
15                             </goals>
16                         </execution>
17                         </executions>
18                     </plugin>
19                 </plugins>
20                 ...
21             </build>


            layout 属性用来指定打成 jar 还是war 文件,可用的值包括:ZIP 、JAR 、WAR、 NONE

            在IDE中项目列表 -> 点击鼠标右键 -> Maven -> Reload Project

        (2) 运行

            Run -> Edit configurations -> Click "+" -> Select "Maven"

                Command line: clean spring-boot:run
                Name: SpringbootBasic [clean,spring-boot:run]


            -> OK

            Run -> Run "SpringbootBasic [clean,spring-boot:run]"

                Hello World!

        (3) 打包 jar 

            菜单 View -> Tool Windows -> Maven -> SpringbootBasic -> Lifecycle -> Clean & Package

            jar 包生成在目录 target/ 里

                SpringbootBasic-1.0-SNAPSHOT.jar
                SpringbootBasic-1.0-SNAPSHOT.jar.original  

            注:SpringbootBasic-1.0-SNAPSHOT.jar  包含依赖包,可以直接运行。 SpringbootBasic-1.0-SNAPSHOT.jar.original 里不包含依赖的包(要手动配置依赖环境),运行前要把文件名上的 “.original” 去掉。


        (4) 运行 jar

            点击 IDEA 底部 Terminal 标签页,执行如下命令。

            $ java -jar target/SpringbootBasic-1.0-SNAPSHOT.jar

                Hello World!


3. 配置 Spring Boot


    1) 导入 spring-boot-starter-parent, spring-boot-starter 相关依赖包

        访问 http://www.mvnrepository.com/,查询 spring-boot-starter-parent

        修改 pom.xml:

 1             <project ... >
 2                 ...
 3 
 4                 <parent>
 5                     <groupId>org.springframework.boot</groupId>
 6                     <artifactId>spring-boot-starter-parent</artifactId>
 7                     <version>2.6.6</version>
 8                     <relativePath/> <!-- lookup parent from repository -->
 9                 </parent>
10 
11                 <dependencies>
12 
13                     ...
14 
15                     <dependency>
16                         <groupId>org.springframework.boot</groupId>
17                         <artifactId>spring-boot-starter</artifactId>
18                     </dependency>
19 
20                     <dependency>
21                         <groupId>org.springframework.boot</groupId>
22                         <artifactId>spring-boot-starter-test</artifactId>
23                         <scope>test</scope>
24                     </dependency>
25 
26                     ...
27 
28                 </dependencies>
29             
30                 ...    
31 
32             </project>


        在IDE中项目列表 -> SpringbootBasic -> 点击鼠标右键 -> Maven -> Reload Project

        本文选择了 spring-boot-starter-parent 2.6.6 相关依赖包,spring-boot-starter 和 spring-boot-starter-test 的版本由 spring-boot-starter-parent 控制。

    2) 修改 src/main/java/com/example/App.java 文件

 1         package com.example;
 2 
 3         import org.springframework.boot.SpringApplication;
 4         import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6         @SpringBootApplication
 7         public class App {
 8             public static void main(String[] args) {
 9                 SpringApplication.run(App.class, args);
10                 System.out.println("Spring boot jar empty project");
11             }
12         }


    3) 运行

        Click Run "SpringbootBasic"

            .   ____          _            __ _ _
            /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
            ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
            \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
            '  |____| .__|_| |_|_| |_\__, | / / / /
            =========|_|==============|___/=/_/_/_/
            :: Spring Boot ::                (v2.6.6)

            ...
            
            Spring boot jar empty project


4. Spring Initializr

    Spring Initializr 提供了 Web 方式创建 SpringBoot 项目,访问 https://start.spring.io,可以看到一个类似下面的表单。

        Project: Maven Project / Gradle Project
        Language: Java / Kotlin / Groovy

        Spring Boot:
        
            3.0.0 (SNAPSHOT) / 3.0.0 (M2) / 2.7.0 (SNAPSHOT) / 2.7.0 (M3) /
            2.6.7(SNAPSHOT) / 2.6.6 / 2.5.13 (SNAPSHOT) / 2.5.12

        Project Metadata

            Group: com.example
            Artifact: demo
            Name: demo
            Description: Demo project for Spring Boot
            Package Name: com.example.demo
            Packaging: jar / war
            Java: 18 / 17 / 11 / 8

        Dependencies  [ADD DEPENDENCIES]

    示例

        Project: Maven Project  
        Language: Java

        Spring Boot: 2.6.6

        Group: com.example
        Artifact: SpringbootBasic
        Name: SpringbootBasic
        Description: Maven spring boot jar empty project
        Package Name: com.example.SpringbootBasic
        Packaging: jar
        Java: 8

        Dependencies: no

        生成并下载 SpringbootBasic.zip,解开压缩包,导入 IDEA 运行。

5. Springboot 相关的网站

    Spring:https://spring.io/
    Spring Initializr: https://start.spring.io/

    Maven:https://maven.apache.org/
    Maven Repository:https://mvnrepository.com/

    Java Standard Edition 8 API:https://docs.oracle.com/javase/8/docs/api/
    Tomcat:https://tomcat.apache.org/
    JMeter:https://jmeter.apache.org/

 

 

本系列文章资料参考来源:

1. http://c.biancheng.net/spring_boot/

2. https://docs.spring.io/spring-boot/docs/2.3.12.RELEASE/reference/html/

 

posted @ 2022-04-07 08:00  垄山小站  阅读(529)  评论(0编辑  收藏  举报