Starter机制介绍
我们知道SpringBoot自己在“后台”帮我们配置了很多原本需要我们手动去的东西,至于这个“后台”是啥,就是Starter机制。我们先来看一看常用的Starter启动器,他们都是以spring-boot-starter开头明名的,至于spring-boot这个前缀则被Spring Boot官方保留。
至于Starter机制是如何实现上文说的“自动变速箱”功能,简单说就是通过条件注解来实现的,SpringBoot通过条件注解确定需要加载哪些组件,读取哪些配置,还有一些控制加载顺序的注解。这里我们先来看一些常用的条件注解和控制加载顺序的注解:
依赖配置文件
这里以一个简单的SpringBoot中的依赖配置文件做说明,只加入最基础的配置。
<?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>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<groupId>com.david</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--web功能的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--@ConfiguaritionProperties的执行器的配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
可以看到有如下几个依赖需要加入:
1)spring-boot-starter-parent,这个是所有SpringBoot工程都需要加入的依赖项,可以说这个是一个“根依赖”,必须要加入,注意这里指定的版本号也会约束其他依赖项,相当于一个最上层的版本配置,这里指定的就是我们常说的SpringBoot的版本。
2)spring-boot-starter-web,这个是Web工程的依赖项,所有的Web程序都要加。
3)spring-boot-configuration-processor,这个是ConfiguaritionProperties的配置,有了它我们的Spring项目中的Jar就会包含上很多meta-data,这些元数据会在开发的时候通过idea给我们很多输入的提示。
spring-boot-starter-parent的作用
可以说这是SpringBoot项目中最重要的配置文件,这里看下关于他的几个核心部分,首先关于它的描述是这样的:
<description>
Parent pom providing dependency and plugin management for applications built with Maven
</description>
大意是说这个文件是一个提供了通过maven编译的应用的依赖和插件管理一个“父配置文件”。嗯,就理解是管理基础依赖和插件的就可以了。
看他的resource标签,是这样的:
<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>
这里指定了资源的导入和导出路径,什么意思?就是你的配置文件配在对应的目录下,框架都会去扫描到读取的,按上面的配置会读src/main/resources目录下的配置文件的内容。比如我们在这里放了如下的配置文件:
这样程序启动的会去读application.properties和application.yml文件里面的配置的。这里要说一下,上面的配置加载是有顺序的,先加yml,yaml ,后加properties里面的配置,这也决定了properties文文件配置的优先级是要高于yaml文件的,或者说相同参数的配置,properties里面的参数会覆盖yaml里面的。
最后看到最上面还有一个parent标签,是这样描述的,这里描述了spring-boot-starter-parent的父依赖配置spring-boot-dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
同样的我们看一看spring-boot-dependencies的描述文件,看看里面的properties标签,是这样:
<properties>
<activemq.version>5.15.3</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.63</appengine-sdk.version>
<artemis.version>2.4.0</artemis.version>
<aspectj.version>1.8.13</aspectj.version>
<assertj.version>3.9.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
<byte-buddy.version>1.7.11</byte-buddy.version>
<caffeine.version>2.6.2</caffeine.version>
<cassandra-driver.version>3.4.0</cassandra-driver.version>
<classmate.version>1.3.4</classmate.version>
<commons-codec.version>1.11</commons-codec.version>
<commons-dbcp2.version>2.2.0</commons-dbcp2.version>
<commons-lang3.version>3.7</commons-lang3.version>
<commons-pool.version>1.6</commons-pool.version>
<commons-pool2.version>2.5.0</commons-pool2.version>
<couchbase-cache-client.version>2.1.0</couchbase-cache-client.version>
<couchbase-client.version>2.5.7</couchbase-client.version>
<derby.version>10.14.1.0</derby.version>
<dom4j.version>1.6.1</dom4j.version>
<dropwizard-metrics.version>3.2.6</dropwizard-metrics.version>
<ehcache.version>2.10.4</ehcache.version>
<ehcache3.version>3.5.2</ehcache3.version>
<elasticsearch.version>5.6.8</elasticsearch.version>
<embedded-mongo.version>2.0.3</embedded-mongo.version>
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
<flatten-maven-plugin.version>1.0.0</flatten-maven-plugin.version>
<flyway.version>5.0.7</flyway.version>
<freemarker.version>2.3.28</freemarker.version>
<git-commit-id-plugin.version>2.2.3</git-commit-id-plugin.version>
<glassfish-el.version>3.0.0</glassfish-el.version>
<groovy.version>2.4.15</groovy.version>
<gson.version>2.8.2</gson.version>
<h2.version>1.4.197</h2.version>
<hamcrest.version>1.3</hamcrest.version>
<hazelcast.version>3.9.3</hazelcast.version>
<hazelcast-hibernate5.version>1.2.3</hazelcast-hibernate5.version>
<hibernate.version>5.2.16.Final</hibernate.version>
<hibernate-validator.version>6.0.9.Final</hibernate-validator.version>
<hikaricp.version>2.7.8</hikaricp.version>
<hsqldb.version>2.4.0</hsqldb.version>
<htmlunit.version>2.29</htmlunit.version>
<httpasyncclient.version>4.1.3</httpasyncclient.version>
<httpclient.version>4.5.5</httpclient.version>
<httpcore.version>4.4.9</httpcore.version>
<infinispan.version>9.1.7.Final</infinispan.version>
<influxdb-java.version>2.9</influxdb-java.version>
<jackson.version>2.9.5</jackson.version>
<janino.version>3.0.8</janino.version>
<javax-annotation.version>1.3.2</javax-annotation.version>
<javax-cache.version>1.1.0</javax-cache.version>
<javax-jaxb.version>2.3.0</javax-jaxb.version>
<javax-jms.version>2.0.1</javax-jms.version>
<javax-json.version>1.1.2</javax-json.version>
<javax-jsonb.version>1.0</javax-jsonb.version>
<javax-mail.version>1.6.1</javax-mail.version>
<javax-money.version>1.0.1</javax-money.version>
<javax-transaction.version>1.2</javax-transaction.version>
<javax-validation.version>2.0.1.Final</javax-validation.version>
<jaxen.version>1.1.6</jaxen.version>
<jaybird.version>3.0.3</jaybird.version>
<jboss-logging.version>3.3.2.Final</jboss-logging.version>
<jboss-transaction-spi.version>7.6.0.Final</jboss-transaction-spi.version>
<jdom2.version>2.0.6</jdom2.version>
<jedis.version>2.9.0</jedis.version>
<jersey.version>2.26</jersey.version>
<jest.version>5.3.3</jest.version>
<jetty.version>9.4.9.v20180320</jetty.version>
<jetty-el.version>8.5.24.2</jetty-el.version>
<jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version>
<jmustache.version>1.14</jmustache.version>
<jna.version>4.5.1</jna.version>
<joda-time.version>2.9.9</joda-time.version>
<johnzon-jsonb.version>1.1.7</johnzon-jsonb.version>
<jolokia.version>1.5.0</jolokia.version>
<jooq.version>3.10.6</jooq.version>
<jsonassert.version>1.5.0</jsonassert.version>
<json-path.version>2.4.0</json-path.version>
<jstl.version>1.2</jstl.version>
<jtds.version>1.3.1</jtds.version>
<junit.version>4.12</junit.version>
<junit-jupiter.version>5.1.0</junit-jupiter.version>
<junit-platform.version>1.1.0</junit-platform.version>
<kotlin.version>1.2.31</kotlin.version>
<lettuce.version>5.0.3.RELEASE</lettuce.version>
<liquibase.version>3.5.5</liquibase.version>
<log4j2.version>2.10.0</log4j2.version>
<logback.version>1.2.3</logback.version>
<lombok.version>1.16.20</lombok.version>
<mariadb.version>2.2.3</mariadb.version>
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
<maven-assembly-plugin.version>3.1.0</maven-assembly-plugin.version>
<maven-clean-plugin.version>3.0.0</maven-clean-plugin.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven-dependency-plugin.version>3.0.2</maven-dependency-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-eclipse-plugin.version>2.10</maven-eclipse-plugin.version>
<maven-enforcer-plugin.version>3.0.0-M1</maven-enforcer-plugin.version>
<maven-failsafe-plugin.version>2.21.0</maven-failsafe-plugin.version>
<maven-help-plugin.version>2.2</maven-help-plugin.version>
<maven-install-plugin.version>2.5.2</maven-install-plugin.version>
<maven-invoker-plugin.version>3.0.0</maven-invoker-plugin.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<maven-javadoc-plugin.version>3.0.0</maven-javadoc-plugin.version>
<maven-resources-plugin.version>3.0.1</maven-resources-plugin.version>
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
<maven-site-plugin.version>3.6</maven-site-plugin.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<maven-war-plugin.version>3.1.0</maven-war-plugin.version>
<micrometer.version>1.0.3</micrometer.version>
<mockito.version>2.15.0</mockito.version>
<mongodb.version>3.6.3</mongodb.version>
<mongo-driver-reactivestreams.version>1.7.1</mongo-driver-reactivestreams.version>
<mssql-jdbc.version>6.2.2.jre8</mssql-jdbc.version>
<mysql.version>5.1.46</mysql.version>
<narayana.version>5.8.0.Final</narayana.version>
<nekohtml.version>1.9.22</nekohtml.version>
<neo4j-ogm.version>3.1.0</neo4j-ogm.version>
<netty.version>4.1.23.Final</netty.version>
<nio-multipart-parser.version>1.1.0</nio-multipart-parser.version>
<postgresql.version>42.2.2</postgresql.version>
<quartz.version>2.3.0</quartz.version>
<querydsl.version>4.1.4</querydsl.version>
<rabbit-amqp-client.version>5.1.2</rabbit-amqp-client.version>
<reactive-streams.version>1.0.2</reactive-streams.version>
<reactor-bom.version>Bismuth-SR8</reactor-bom.version>
<rest-assured.version>3.0.7</rest-assured.version>
<rxjava.version>1.3.8</rxjava.version>
<rxjava2.version>2.1.12</rxjava2.version>
<rxjava-adapter.version>1.2.1</rxjava-adapter.version>
<selenium.version>3.9.1</selenium.version>
<selenium-htmlunit.version>2.29.3</selenium-htmlunit.version>
<sendgrid.version>4.1.2</sendgrid.version>
<servlet-api.version>3.1.0</servlet-api.version>
<simple-json.version>1.1.1</simple-json.version>
<slf4j.version>1.7.25</slf4j.version>
<snakeyaml.version>1.19</snakeyaml.version>
<solr.version>6.6.3</solr.version>
<spring.version>5.0.5.RELEASE</spring.version>
<spring-amqp.version>2.0.3.RELEASE</spring-amqp.version>
<spring-batch.version>4.0.1.RELEASE</spring-batch.version>
<spring-cloud-connectors.version>2.0.1.RELEASE</spring-cloud-connectors.version>
<spring-data-releasetrain.version>Kay-SR6</spring-data-releasetrain.version>
<spring-hateoas.version>0.24.0.RELEASE</spring-hateoas.version>
<spring-integration.version>5.0.4.RELEASE</spring-integration.version>
<spring-kafka.version>2.1.5.RELEASE</spring-kafka.version>
<spring-ldap.version>2.3.2.RELEASE</spring-ldap.version>
<spring-plugin.version>1.2.0.RELEASE</spring-plugin.version>
<spring-restdocs.version>2.0.1.RELEASE</spring-restdocs.version>
<spring-retry.version>1.2.2.RELEASE</spring-retry.version>
<spring-security.version>5.0.4.RELEASE</spring-security.version>
<spring-session-bom.version>Apple-SR1</spring-session-bom.version>
<spring-ws.version>3.0.1.RELEASE</spring-ws.version>
<sqlite-jdbc.version>3.21.0.1</sqlite-jdbc.version>
<statsd-client.version>3.1.0</statsd-client.version>
<sun-mail.version>1.6.1</sun-mail.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>
<thymeleaf-extras-java8time.version>3.0.1.RELEASE</thymeleaf-extras-java8time.version>
<thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version>
<thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
<tomcat.version>8.5.29</tomcat.version>
<unboundid-ldapsdk.version>4.0.5</unboundid-ldapsdk.version>
<undertow.version>1.4.23.Final</undertow.version>
<versions-maven-plugin.version>2.3</versions-maven-plugin.version>
<webjars-hal-browser.version>3325375</webjars-hal-browser.version>
<webjars-locator-core.version>0.35</webjars-locator-core.version>
<wsdl4j.version>1.6.3</wsdl4j.version>
<xml-apis.version>1.4.01</xml-apis.version>
<xml-maven-plugin.version>1.0.1</xml-maven-plugin.version>
<xmlunit2.version>2.5.1</xmlunit2.version>
</properties>
喔,好多似曾相识的面孔在这里都能见到,比如aspectj,caffeine, dom4j,hikaricp,junit,log4j2, mysql,tomcat等等一些,而且你也看到了,这里帮我们把版本控制也做好了,防止了原来在Spring中我们自己手动引入依赖的时候的经常会发生的版本冲突问题,所谓的约定优于配置在这里就是一个典型的体现场景。
spring-boot-starter-web的作用
这个配置是web应用的核心配置,有了他不用像Spring再像Spring一样去导SpringMVC相关的依赖了,这里看一下这个文件的内容。
同样的也有一个parent标签指定父依赖spring-boot-starters.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
另外dependencies标签指定是一些依赖坐标的打包,包含json,tomcat等等,包含版本控制都做好了。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.0.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.9.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
其中spring-boot-starter的依赖又包含了如下的dependencies,有spring-boot,自动配置,日志,注解,core等等的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.0.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.0.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.19</version>
<scope>runtime</scope>
</dependency>
</dependencies>
以上可以看到SpringBoot基本上通过spring-boot-starter-parent和spring-boot-starter-web两个配置就把起步依赖需要导入的依赖项把我们需要的依赖给导完了,这也就是SpringBoot的starter机制在依赖配置层的最好体现,实现了所谓约定优于配置。