Spring Boot 简介

SpringBoot是一个快速开发框架,通过用MAVEN依赖的继承方式,帮助我们快速整合第三方常用框架,完全采用注解化(使用注解方式启动SpringMVC),简化XML配置,内置HTTP服务器(Tomcat,Jetty),最终以Java应用程序进行执行。

如何创建 Spring Boot 项目

Sping Boot 项目的本质其实还是一个 Maven 项目,主要有如下几种创建 Spring Boot 项目的方式;

在线创建

1、打开 ​​https://start.spring.io/​​ 来生成 Spring Boot 项目;

如何创建 Spring Boot 项目_xml

​/2、

2、然后选择和填写相关配置;

  • Project:表示使用什么构建工具,Maven or Gradle;
  • Language:表示使用什么编程语言, Java 、Kotlin or Groovy;
  • Spring Boot:Spring Boot 的版本;
  • Project Metadata:项目元数据,即 Maven项目基本元素,根据自己的实际情况填写;
  • Dependencies:要加入的 Spring Boot 组件;

如何创建 Spring Boot 项目_xml_02

3、然后点击生成或 Ctrl + Enter 即可;

 

如何创建 Spring Boot 项目_spring_03

4、将压缩包下载后,解压缩后用自己喜欢的 IDE 开发即可;

 

IntelliJ IDEA 创建

1、新建项目时选择 Spring Initializr ;

如何创建 Spring Boot 项目_maven_04

2、点击下一步,填写相关配置;

 

  • Group:组织 ID,一般分为多个段,一般第一段为域,而第二段则是 公司名称;
  • Artifact:唯一标识符,一般是项目名;

如何创建 Spring Boot 项目_xml_05

3、选择包,添加相关依赖;

 

如何创建 Spring Boot 项目_xml_06

4、配置项目名,点击完成即可;

 

如何创建 Spring Boot 项目_xml_07

Maven 创建

 

1、新建 Maven 项目;

如何创建 Spring Boot 项目_maven_08

2、填写项目名和相关配置;

 

如何创建 Spring Boot 项目_maven_09

3、点击完成即可;

 

如何创建 Spring Boot 项目_spring_10

4、配置 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.cunyu1943</groupId>
<artifactId>springDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>

5、在 main/java 目录下创建一个包,然后新建一个类,比如我的如下;

 

package controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}

@GetMapping("/index")
public String index() {
return "Hello World!";
}
}

6、运行上一步中的 main 方法即可;

常见项目结构

代码层

根目录:com.springboot:

  • build :工程启动类;
  • entity :实体类;
  • dao :数据访问层;
  • service :数据服务层,业务类代码;
  • controller :前端访问控制器;
  • config :配置信息类;
  • util :工具类;
  • constant :常用接口类;
  • vo :数据传输类;
  • Application.java:项目的启动类;

资源文件结构

根目录 src/main/resources:

  • config :.properties、.json 等配置文件;
  • i18n :国际化相关;
  • META-INF/spring :spring.xml ;
  • static :页面以及 js、css、image 等分别放在各自文件夹下;

@SpringBootApplication 注解分析

 

package org.springframework.boot.autoconfigure;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
......
}

说明:

@SpringBootApplication 标注该类是一个启动类,可以看做是 @Configuration、@EnableAutoConfiguration、@ComponentScan 的集合;

  • @Configuration :允许在上下文中注册额外的 Bean 或导入其他配置;
  • @EnableAutoConfiguration:启动 Spring Boot 的自动配置机制;
  • @ComponentScan:扫描被 @ComponentScan(@Service、@Controller、@Repository) 注解的 Bean,默认扫描该类所在包下所有类,将这些 Bean 定义加载到 IOC 容器中;

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cunyu</groupId>
<artifactId>springboot-03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-03</name>
<description>spring boot - 03</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

创建好项目后,如果没有选其他组件,会生成如上的 Spring Boot 项目依赖,主要有四个部分:

  • 项目元数据

创建时输入的 Project Metadata 部分,即 Maven 项目的基本元素,包括 groupId、artifactId、version、name、description 等;

  • parent

继承 spring-boot-starter-parent 的依赖管理,控制版本与打包等等内容;

  • dependencies

项目具体依赖,默认包含 spring-boot-starter-web,用于实现HTTP接口(该依赖中包含了Spring MVC);spring-boot-starter-test用于编写单元测试的依赖包。后续开发中,主要就是在这里添加各种依赖。

  • build

构建配置部分,默认使用 spring-boot-maven-plugin,配合 spring-boot-starter-parent 可以把 Spring Boot 应用打包成 jar 来直接运行。

Spring Boot中parent标签的作用

在Spring Boot的官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent作为parent标签。

 

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

这个parent标签有一个最常用的功能,就是定义子标签的依赖版本号,然而它的功能却不仅仅如此。

parent 源码

 

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
<artifactId>spring-boot-starter-parent</artifactId>
<packaging>pom</packaging>
<name>Spring Boot Starter Parent</name>
<description>Parent pom providing dependency and plugin management for applications
built with Maven</description>
<url>https://projects.spring.io/spring-boot/#/spring-boot-starter-parent</url>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>${java.version}</jvmTarget>
<javaParameters>true</javaParameters>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<