0.SpringBoot是什么?
SpringBoot框架是Spring家族的纯正后裔,将简化开发这一传统发扬光大。SpringBoot之前的Spring项目中有许多繁琐的xml配置,项目的依赖管理也非常耗费精力且容易出现各种不兼容情况或引起bug,SpringBoot的出现极大地简化了Spring的开发流程,让Spring框架更加好用。
1.SpringBoot特性
SpringBoot提供了4个主要特性:
1.Spring Boot Starter:它将常用依赖分组进行整合,将其合并到一个依赖中,让开发者可以一次性添加到项目的Maven中;
2.自动配置:Spring Boot的自动配置特性利用了Spring4对条件化配置的支持,合理地推断应用所需的bean并自动化配置它们;
3.命令行接口(Command-line interface,CLI):Spring Boot的CLI发挥了Groovy编程语言的优势,并结合自动配置进一步简化Spring应用的开发;
4.Actuator:它为Spring Boot应用添加了一定的管理特性。
SpringBoot为Spring开发提供了更快的入门体验,开箱即用,无需XML配置,也可以修改参数满足特定需要。它还提供了嵌入式服务器、安全、指标、健康检测、外部配置等大型项目中常见的非功能性特性。SpringBoot不是对Spring进行功能增强,而是提供了一种快速使用Spring的方式。
SpringBoot的核心功能:
1.起步依赖。
起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些对象加在一起即支持某项功能,即提供了各种starter启动器,它们包含一些依赖,能够完成某些功能,简化开发。
2.自动配置。
自动配置是SpringBoot简化开发的关键,思想就是约定大于配置,是指对于工程常见的一些功能,SpringBoot在创建工程的时候自动把这些功能所需的类实例化并加入到Spring容器了,不需要我们再做额外的配置。
2.SpringBoot开发实践
开始使用Springboot来搭建一个项目,感受一下它的简单易用。
2.1创建SpringBoot项目
1.使用idea快速创建一个SpringBoot项目
2.选择Spring Initializr,Module SDK选择JDK1.8,next。
3.输入项目名称,项目管理工具选择Maven,JDK还选1.8。
4.选择SpringBoot版本和依赖,注意SpringBoot3.0以上版本使用JDK1.8编译会报错,这里选择2.7.9。勾选web,template engines,sql,分别选择Spring Web,Thymeleaf,Mybatis。
5.填写项目名称与路径
6.项目创建成功,目录结构如图所示。
其中,static用来存放静态资源,templates存放网页的模板文件,application.properties 是组件的配置文件,各类参数可以在这里修改。
通过idea快速创建的SpringBoot项目的pom.xml中已经导入了我们选择的Web、Thymeleaf、Mybatis的起步依赖(starter)的坐标:
<?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.7.9-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
接下来就可以创建一个Controller类来写一个简单的Spring应用。
8.创建Controller类
在demo包下创建controller包,在controller包中创建Controller类。Controller类应与启动类DemoApplication处于同一目录下,否则SpringBoot加载不到。
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class BeginDemo {
@RequestMapping("/begin")
public @ResponseBody String beginDemo(){
return "SpringBoot is coming!";
}
}
@Controller表示这是一个controller类
@RequestMapping注解后面的括号里写的是访问应用的路径(前面还有localhost:8080)
@ReponseBody注解作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式,这个返回的是一个字符串。
9.启动应用
点击DemoApplication启动类的main方法,启动应用。
D:\JavaTools\JDK1.8\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\IDEA-2019\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=52636:D:\IDEA-2019\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath D:\JavaTools\JDK1.8\jre\lib\charsets.jar;D:\JavaTools\JDK1.8\jre\lib\deploy.jar;D:\JavaTools\JDK1.8\jre\lib\ext\access-bridge-64.jar;D:\JavaTools\JDK1.8\jre\lib\ext\cldrdata.jar;D:\JavaTools\JDK1.8\jre\lib\ext\dnsns.jar;D:\JavaTools\JDK1.8\jre\lib\ext\jaccess.jar;D:\JavaTools\JDK1.8\jre\lib\ext\jfxrt.jar;D:\JavaTools\JDK1.8\jre\lib\ext\localedata.jar;D:\JavaTools\JDK1.8\jre\lib\ext\nashorn.jar;D:\JavaTools\JDK1.8\jre\lib\ext\sunec.jar;D:\JavaTools\JDK1.8\jre\lib\ext\sunjce_provider.jar;D:\JavaTools\JDK1.8\jre\lib\ext\sunmscapi.jar;D:\JavaTools\JDK1.8\jre\lib\ext\sunpkcs11.jar;D:\JavaTools\JDK1.8\jre\lib\ext\zipfs.jar;D:\JavaTools\JDK1.8\jre\lib\javaws.jar;D:\JavaTools\JDK1.8\jre\lib\jce.jar;D:\JavaTools\JDK1.8\jre\lib\jfr.jar;D:\JavaTools\JDK1.8\jre\lib\jfxswt.jar;D:\JavaTools\JDK1.8\jre\lib\jsse.jar;D:\JavaTools\JDK1.8\jre\lib\management-agent.jar;D:\JavaTools\JDK1.8\jre\lib\plugin.jar;D:\JavaTools\JDK1.8\jre\lib\resources.jar;D:\JavaTools\JDK1.8\jre\lib\rt.jar;H:\ideawork\SpringBootDemo\target\classes;D:\JavaTools\repository-localhost\org\springframework\boot\spring-boot-starter-thymeleaf\2.7.9-SNAPSHOT\spring-boot-starter-thymeleaf-2.7.9-20230222.113803-31.jar;D:\JavaTools\repository-localhost\org\springframework\boot\spring-boot-starter\2.7.9-SNAPSHOT\spring-boot-starter-2.7.9-20230222.113803-31.jar;D:\JavaTools\repository-localhost\org\springframework\boot\spring-boot\2.7.9-SNAPSHOT\spring-boot-2.7.9-20230222.113803-31.jar;D:\JavaTools\repository-localhost\org\springframework\boot\spring-boot-autoconfigure\2.7.9-SNAPSHOT\spring-boot-autoconfigure-2.7.9-20230222.113803-31.jar;D:\JavaTools\repository-localhost\org\springframework\boot\spring-boot-starter-logging\2.7.9-SNAPSHOT\spring-boot-starter-logging-2.7.9-20230222.113803-31.jar;D:\JavaTools\repository-localhost\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\JavaTools\repository-localhost\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\JavaTools\repository-localhost\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\JavaTools\repository-localhost\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\JavaTools\repository-localhost\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\JavaTools\repository-localhost\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\JavaTools\repository-localhost\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\JavaTools\repository-localhost\org\thymeleaf\thymeleaf-spring5\3.0.15.RELEASE\thymeleaf-spring5-3.0.15.RELEASE.jar;D:\JavaTools\repository-localhost\org\thymeleaf\thymeleaf\3.0.15.RELEASE\thymeleaf-3.0.15.RELEASE.jar;D:\JavaTools\repository-localhost\org\attoparser\attoparser\2.0.5.RELEASE\attoparser-2.0.5.RELEASE.jar;D:\JavaTools\repository-localhost\org\unbescape\unbescape\1.1.6.RELEASE\unbescape-1.1.6.RELEASE.jar;D:\JavaTools\repository-localhost\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\JavaTools\repository-localhost\org\thymeleaf\extras\thymeleaf-extras-java8time\3.0.4.RELEASE\thymeleaf-extras-java8time-3.0.4.RELEASE.jar;D:\JavaTools\repository-localhost\org\springframework\boot\spring-boot-starter-web\2.7.9-SNAPSHOT\spring-boot-starter-web-2.7.9-20230222.113803-31.jar;D:\JavaTools\repository-localhost\org\springframework\boot\spring-boot-starter-json\2.7.9-SNAPSHOT\spring-boot-starter-json-2.7.9-20230222.113803-31.jar;D:\JavaTools\repository-localhost\com\fasterxml\jackson\core\jackson-databind\2.13.5\jackson-databind-2.13.5.jar;D:\JavaTools\repository-localhost\com\fasterxml\jackson\core\jackson-annotations\2.13.5\jackson-annotations-2.13.5.jar;D:\JavaTools\repository-localhost\com\fasterxml\jackson\core\jackson-core\2.13.5\jackson-core-2.13.5.jar;D:\JavaTools\repository-localhost\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.5\jackson-datatype-jdk8-2.13.5.jar;D:\JavaTools\repository-localhost\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.5\jackson-datatype-jsr310-2.13.5.jar;D:\JavaTools\repository-localhost\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.5\jackson-module-parameter-names-2.13.5.jar;D:\JavaTools\repository-localhost\org\springframework\boot\spring-boot-starter-tomcat\2.7.9-SNAPSHOT\spring-boot-starter-tomcat-2.7.9-20230222.113803-31.jar;D:\JavaTools\repository-localhost\org\apache\tomcat\embed\tomcat-embed-core\9.0.71\tomcat-embed-core-9.0.71.jar;D:\JavaTools\repository-localhost\org\apache\tomcat\embed\tomcat-embed-el\9.0.71\tomcat-embed-el-9.0.71.jar;D:\JavaTools\repository-localhost\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.71\tomcat-embed-websocket-9.0.71.jar;D:\JavaTools\repository-localhost\org\springframework\spring-web\5.3.25\spring-web-5.3.25.jar;D:\JavaTools\repository-localhost\org\springframework\spring-beans\5.3.25\spring-beans-5.3.25.jar;D:\JavaTools\repository-localhost\org\springframework\spring-webmvc\5.3.25\spring-webmvc-5.3.25.jar;D:\JavaTools\repository-localhost\org\springframework\spring-aop\5.3.25\spring-aop-5.3.25.jar;D:\JavaTools\repository-localhost\org\springframework\spring-context\5.3.25\spring-context-5.3.25.jar;D:\JavaTools\repository-localhost\org\springframework\spring-expression\5.3.25\spring-expression-5.3.25.jar;D:\JavaTools\repository-localhost\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.3.0\mybatis-spring-boot-starter-2.3.0.jar;D:\JavaTools\repository-localhost\org\springframework\boot\spring-boot-starter-jdbc\2.7.9-SNAPSHOT\spring-boot-starter-jdbc-2.7.9-20230222.113803-31.jar;D:\JavaTools\repository-localhost\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;D:\JavaTools\repository-localhost\org\springframework\spring-jdbc\5.3.25\spring-jdbc-5.3.25.jar;D:\JavaTools\repository-localhost\org\springframework\spring-tx\5.3.25\spring-tx-5.3.25.jar;D:\JavaTools\repository-localhost\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.3.0\mybatis-spring-boot-autoconfigure-2.3.0.jar;D:\JavaTools\repository-localhost\org\mybatis\mybatis\3.5.11\mybatis-3.5.11.jar;D:\JavaTools\repository-localhost\org\mybatis\mybatis-spring\2.1.0\mybatis-spring-2.1.0.jar;D:\JavaTools\repository-localhost\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar;D:\JavaTools\repository-localhost\org\springframework\spring-core\5.3.25\spring-core-5.3.25.jar;D:\JavaTools\repository-localhost\org\springframework\spring-jcl\5.3.25\spring-jcl-5.3.25.jar com.example.demo.DemoApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.9-SNAPSHOT)
2023-02-24 23:47:08.868 INFO 9008 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_91 on PC-202003181451 with PID 9008 (H:\ideawork\SpringBootDemo\target\classes started by Administrator in H:\ideawork\SpringBootDemo)
2023-02-24 23:47:08.872 INFO 9008 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2023-02-24 23:47:09.742 WARN 9008 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.example.demo]' package. Please check your configuration.
2023-02-24 23:47:10.245 INFO 9008 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-02-24 23:47:10.246 INFO 9008 --- [ main] o.a.catalina.core.AprLifecycleListener : An older version [1.2.16] of the Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.30]
2023-02-24 23:47:10.246 INFO 9008 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded Apache Tomcat Native library [1.2.16] using APR version [1.6.3].
2023-02-24 23:47:10.247 INFO 9008 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [false].
2023-02-24 23:47:10.247 INFO 9008 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2023-02-24 23:47:11.318 INFO 9008 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.0.2m 2 Nov 2017]
2023-02-24 23:47:11.329 INFO 9008 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-02-24 23:47:11.329 INFO 9008 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.71]
2023-02-24 23:47:11.521 INFO 9008 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-02-24 23:47:11.521 INFO 9008 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2585 ms
2023-02-24 23:47:11.797 INFO 9008 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2023-02-24 23:47:12.159 INFO 9008 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-02-24 23:47:12.173 INFO 9008 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.858 seconds (JVM running for 7.502)
2023-02-24 23:47:20.748 INFO 9008 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-02-24 23:47:20.748 INFO 9008 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-02-24 23:47:20.750 INFO 9008 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
控制台输出未见异常,应用启动成功。
10.通过浏览器访问应用
在浏览器输入localhost:8080/begin,即可访问应用,返回SpringBoot is coming!字符串,应用功能正常。
至此,SpringBoot创建Spring项目就完成了,基本没有做什么配置。
2.2SpringBoot原理分析
接下来我们分析一下为什么SpringBoot可以如此方便地创建一个项目,主要还是启动依赖和自动配置两个方面。
2.2.1起步依赖原理分析
先看一项目的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.7.9-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
1.spring-boot-starter-parent
按住Ctrl点击pom.xml中的spring-boot-starter-parent,跳转到了spring-boot-starter-parent-2.7.9-SNAPSHOT的pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.9-SNAPSHOT</version>
</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>
Parent pom providing dependency and plugin management for applications built with Maven,即父pom为Maven构建的应用提供依赖、插件管理。说明父pom中已经定义好了一些常用的依赖、插件的版本等属性。这个pom中主要定义了properties和plugin,按住Ctrl点击
spring-boot-dependencies,跳转到spring-boot-dependencies的pom.xml:
<properties>
<activemq.version>5.16.6</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.98</appengine-sdk.version>
<artemis.version>2.19.1</artemis.version>
<aspectj.version>1.9.7</aspectj.version>
<assertj.version>3.22.0</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<wsdl4j.version>1.6.3</wsdl4j.version>
<xml-maven-plugin.version>1.0.2</xml-maven-plugin.version>
<xmlunit2.version>2.9.1</xmlunit2.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-amqp</artifactId>
<version>${activemq.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-blueprint</artifactId>
<version>${activemq.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>${activemq.version}</version>
</dependency>
<dependency>
</dependencies>
这里放的是SpringBoot常用的依赖,所以SpringBoot工程继承承spring-boot-starter-parent后已经具备版本锁定等配置了,即
起步依赖的作用就是进行依赖的传递。
2.spring-boot-starter-web
按住Ctrl点击pom.xml中的spring-boot-starter-web,跳转到了spring-boot-starter-web的pom.xml,xml配置如
下:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.9-SNAPSHOT</version>
<name>spring-boot-starter-web</name>
<description>Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container</description>
<url>https://spring.io/projects/spring-boot</url>
<organization>
<name>VMware, Inc.</name>
<url>https://spring.io</url>
</organization>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<developers>
<developer>
<name>Spring</name>
<email>ask@spring.io</email>
<organization>VMware, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
<developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
<url>https://github.com/spring-projects/spring-boot</url>
</scm>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/spring-projects/spring-boot/issues</url>
</issueManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.9-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.7.9-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.7.9-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.25</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.25</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
“使用SpringMvc创建web应用(包括RESTful)的starter,使用Tomcat作为默认嵌入式容器”,pom.xml包含了json、Tomcat、Spring-webmvc、Spring-web等依赖,这样我们的工程只要引入spring-boot-starter-web起步依赖的
坐标就可以进行web开发了,同样体现了依赖传递的作用。
2.2.2自动配置原理分析
接下来分析SpringBoot的自动配置,打开应用的启动类
DemoApplication :
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
按住Ctrl点击注解@SpringBootApplication,查看它的源码:
@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 {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "nameGenerator"
)
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
@SpringBootConfiguration:等同与@Configuration,既标注该类是Spring的一个配置类
@EnableAutoConfiguration:SpringBoot自动配置功能开启
按住Ctrl点击查看注解@EnableAutoConfiguration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
其中,@Import(AutoConfigurationImportSelector.class) 导入了AutoConfigurationImportSelector类
按住Ctrl点击查看AutoConfigurationImportSelector源码
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!this.isEnabled(annotationMetadata)) {
return NO_IMPORTS;
} else {
AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
}
protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!this.isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
} else {
AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
configurations = this.removeDuplicates(configurations);
Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
this.checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = this.getConfigurationClassFilter().filter(configurations);
this.fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
}
}
其中,SpringFactoriesLoader.loadFactoryNames 方法的作用就是从META-INF/spring.factories文件中读取指定
类对应的类名称列表
spring.factories 文件中有关自动配置的配置信息如下:
# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
上面配置文件存在大量的以Configuration为结尾的类名称,这些类就是存有自动配置信息的类,而
SpringApplication在获取这些类名后再加载
我们以ServletWebServerFactoryAutoConfiguration为例来分析源码:
@AutoConfiguration
@AutoConfigureOrder(-2147483648)
@ConditionalOnClass({ServletRequest.class})
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@EnableConfigurationProperties({ServerProperties.class})
@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class})
public class ServletWebServerFactoryAutoConfiguration {
...
}
其中,
@EnableConfigurationProperties(ServerProperties.class) 代表加载ServerProperties服务器配置属性类进入ServerProperties.class源码如下:
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
/**
* Server HTTP port.
*/
private Integer port;
/**
* Network address to which the server should bind.
*/
private InetAddress address;
... ... ...
}
其中,
prefix = “server” 表示SpringBoot配置文件中的前缀,SpringBoot会将配置文件中以server开始的属性映射到该类
的字段中。映射关系如下:
其中,
prefix = “server” 表示SpringBoot配置文件中的前缀,SpringBoot会将配置文件中以server开始的属性映射到该类
的字段中。映射关系如下:
可以看出SpringBoot通过一系列的自动配置类来自动配置bean或加载application.properties配置文件中设定的参数来完成bean的配置。
本节内容到此结束,主要介绍了SpringBoot的特点作用和入门案例,下节介绍一下SpringBoot的配置文件和整合其他技术的步骤。