spring 简化了java应用开发, 而springboot则简化了 spring应用的开发,用约定优于配置优于编码的方式快速构建spring对其他框架的整合.

官方文档

探究Hello,World

使用spring 快速构建一个web应用:

新建一个maven项目

pom依赖:

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

编写主启动类:

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

controller 类:

@RestController
@RequestMapping
public class DemoController {
@GetMapping("test")
public String getString (){
return "Hello World";
}
}

运行主启动类的main方法:

启动成功,访问http://127.0.0.1:8080/test 返回Hello World

就此一个简单web应用就搭建完毕,

why?

  1. 我没有引入 web相关的任何依赖呀?
  2. 就算导入了相关依赖 我也没有配置任何信息,各种组件如何工作?
  3. 我也没有用到容器,应用跑在哪里?
  4. 我编写的controller为什么可以生效?

抱着这些疑问 探究一下

依赖:

spring-boot-starter-web是我们导入的唯一的一个依赖, 点进去一看就明白了,

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.14.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

他已经帮我们导好了web开发所常用的依赖, 这就是场景启动器,Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter 相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器,

在看看 父工程:

 <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>

他的作用就是版本仲裁,当我们导入场景启动器 或者当自己导入依赖时 只要是在 springboot父工程中声明过的 都无须自己定义版本,大大减少版本冲突的可能

<!--常用依赖的版本-->
<properties> <!--*******--> <commons-pool.version>1.6</commons-pool.version>
<commons-pool2.version>2.6.0</commons-pool2.version>
<couchbase-cache-client.version>2.1.0</couchbase-cache-client.version>
<couchbase-client.version>2.7.2</couchbase-client.version>
<derby.version>10.14.2.0</derby.version>
<dom4j.version>1.6.1</dom4j.version>
<dropwizard-metrics.version>4.0.5</dropwizard-metrics.version>
<ehcache.version>2.10.6</ehcache.version>
<ehcache3.version>3.6.3</ehcache3.version>
<elasticsearch.version>6.4.3</elasticsearch.version>
<embedded-mongo.version>2.1.2</embedded-mongo.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<flatten-maven-plugin.version>1.0.1</flatten-maven-plugin.version>
<flyway.version>5.2.4</flyway.version>
<freemarker.version>2.3.28</freemarker.version>
<git-commit-id-plugin.version>2.2.6</git-commit-id-plugin.version>
<glassfish-el.version>3.0.0</glassfish-el.version>
<glassfish-jaxb.version>2.3.1</glassfish-jaxb.version>
<groovy.version>2.5.5</groovy.version>
<gson.version>2.8.5</gson.version>
<h2.version>1.4.197</h2.version>
<hamcrest.version>1.3</hamcrest.version>
<hazelcast.version>3.11.1</hazelcast.version>
<hazelcast-hibernate5.version>1.2.3</hazelcast-hibernate5.version>
<hibernate.version>5.3.7.Final</hibernate.version>
<hibernate-validator.version>6.0.14.Final</hibernate-validator.version>
<hikaricp.version>3.2.0</hikaricp.version>
<hsqldb.version>2.4.1</hsqldb.version>
<htmlunit.version>2.33</htmlunit.version>
<httpasyncclient.version>4.1.4</httpasyncclient.version>
<httpclient.version>4.5.6</httpclient.version>
<httpcore.version>4.4.10</httpcore.version>
<infinispan.version>9.4.5.Final</infinispan.version>
<influxdb-java.version>2.14</influxdb-java.version>
<jackson.version>2.9.8</jackson.version>
<janino.version>3.0.11</janino.version>
<javax-activation.version>1.2.0</javax-activation.version>
<javax-annotation.version>1.3.2</javax-annotation.version>
<javax-cache.version>1.1.0</javax-cache.version>
<javax-jaxb.version>2.3.1</javax-jaxb.version>
<javax-jaxws.version>2.3.1</javax-jaxws.version>
<javax-jms.version>2.0.1</javax-jms.version>
<javax-json.version>1.1.4</javax-json.version>
<javax-jsonb.version>1.0</javax-jsonb.version>
<javax-mail.version>1.6.2</javax-mail.version>
<javax-money.version>1.0.3</javax-money.version>
<javax-persistence.version>2.2</javax-persistence.version>
<javax-transaction.version>1.3</javax-transaction.version>
<javax-validation.version>2.0.1.Final</javax-validation.version>
<javax-websocket.version>1.1</javax-websocket.version>
<jaxen.version>1.1.6</jaxen.version>
<jaybird.version>3.0.5</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.1</jedis.version>
<jersey.version>2.27</jersey.version>
<jest.version>6.3.1</jest.version>
<jetty.version>9.4.14.v20181114</jetty.version>
<jetty-el.version>8.5.35.1</jetty-el.version>
<jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version>
<jetty-reactive-httpclient.version>1.0.2</jetty-reactive-httpclient.version>
<jmustache.version>1.14</jmustache.version>
<jna.version>4.5.2</jna.version>
<joda-time.version>2.10.1</joda-time.version>
<johnzon.version>${johnzon-jsonb.version}</johnzon.version>
<johnzon-jsonb.version>1.1.11</johnzon-jsonb.version>
<jolokia.version>1.6.0</jolokia.version>
<jooq.version>3.11.9</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.3.2</junit-jupiter.version>
<kafka.version>2.0.1</kafka.version>
<kotlin.version>1.2.71</kotlin.version>
<lettuce.version>5.1.3.RELEASE</lettuce.version>
<liquibase.version>3.6.2</liquibase.version>
<log4j2.version>2.11.1</log4j2.version>
<logback.version>1.2.3</logback.version>
<lombok.version>1.18.4</lombok.version>
<mariadb.version>2.3.0</mariadb.version>
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
<maven-assembly-plugin.version>3.1.1</maven-assembly-plugin.version>
<maven-clean-plugin.version>3.1.0</maven-clean-plugin.version>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-enforcer-plugin.version>3.0.0-M2</maven-enforcer-plugin.version>
<maven-failsafe-plugin.version>2.22.1</maven-failsafe-plugin.version>
<maven-help-plugin.version>3.1.1</maven-help-plugin.version>
<maven-install-plugin.version>2.5.2</maven-install-plugin.version>
<maven-invoker-plugin.version>3.1.0</maven-invoker-plugin.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<maven-javadoc-plugin.version>3.0.1</maven-javadoc-plugin.version>
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
<maven-site-plugin.version>3.7.1</maven-site-plugin.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
<maven-war-plugin.version>3.2.2</maven-war-plugin.version>
<micrometer.version>1.1.2</micrometer.version>
<mimepull.version>1.9.10</mimepull.version>
<mockito.version>2.23.4</mockito.version>
<mongodb.version>3.8.2</mongodb.version>
<mongo-driver-reactivestreams.version>1.9.2</mongo-driver-reactivestreams.version>
<mssql-jdbc.version>6.4.0.jre8</mssql-jdbc.version>
<mysql.version>8.0.13</mysql.version>
<nekohtml.version>1.9.22</nekohtml.version>
<neo4j-ogm.version>3.1.6</neo4j-ogm.version>
<netty.version>4.1.31.Final</netty.version>
<netty-tcnative.version>2.0.20.Final</netty-tcnative.version>
<nio-multipart-parser.version>1.1.0</nio-multipart-parser.version>
<pooled-jms-version>1.0.3</pooled-jms-version>
<postgresql.version>42.2.5</postgresql.version>
<prometheus-pushgateway.version>0.5.0</prometheus-pushgateway.version>
<quartz.version>2.3.0</quartz.version>
<querydsl.version>4.2.1</querydsl.version>
<rabbit-amqp-client.version>5.4.3</rabbit-amqp-client.version>
<reactive-streams.version>1.0.2</reactive-streams.version> <!--*******--> </properties> <dependencyManagement> <!--*******--> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-couchbase-reactive</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-groovy-templates</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-bitronix</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-reactor-netty</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>${antlr2.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jms</artifactId>
<version>${atomikos.version}</version>
</dependency> <!--*******--> </dependencies>
</dependencyManagement>

这也就是我们不用导入web相关依赖 和 版本的原因

主启动类:

main方法中调用SpringApplication 的 run方法,传入了主启动类对象, 主要是创建 ApplicationContext 上下文 并刷新容器,但身为容器中的一员,主启动类上的注解也就生效了,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 {

这是该注解的源码, 上面四个都是一些元注解,定义这个注解的一些属性,例如作用域,是否生成文档之类的 这里就不看了,

主要看 SpringBootConfiguration 和 EnableAutoConfiguration 的功能

@SpringBootConfiguration

这个注解比较简单,下面是他的源码,标注主启动类也是一个config配置类,可以使用@Bean 向容器中注入组件

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration

例如如下代码,容器中就自动注入了 自定义的RedisUtil类 ,可以在需要的地方注入使用:

@SpringBootApplication
public class DemoApplication { @Bean
public static RedisUtil getRedisUtil() {
return new RedisUtil();
} public static void main(String[] args) throws Exception {
SpringApplication.run(DemoApplication.class, args);
}
}

@EnableAutoConfiguration

这个注解听名字就很厉害,开启自动配置, 这就是springboot 自动配置的核心,源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

AutoConfigurationPackage

通过 @Import(AutoConfigurationPackages.Registrar.class) Import注解 ,手动向容器中注入Bean

(@Import的具体用法 请参考 spring注解驱动),

调用Registrar 对象的 registerBeanDefinitions 方法 手动注入到容器中,而注入的对象 则会自动扫描启动类所在的包 及其子包 中 标注 Service Controller Component 等 类:

这就是为什么 启动类所在包及其子包下的类会自动注入的原因了

Import(AutoConfigurationImportSelector.class)

这个注解和上面的AutoConfigurationPackage 一样 都是通过Import 注解进行注入Bean, 看看AutoConfigurationImportSelector 有什么玄机

主要就是 getAutoConfigurationEntry 方法中

protected AutoConfigurationEntry getAutoConfigurationEntry(
AutoConfigurationMetadata autoConfigurationMetadata,
AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
/* 去 META-INF/spring.factories 中 寻找 org.springframework.boot.autoconfigure.EnableAutoConfiguration 键所对应的值,并切割 逗号 获得一个一个类名 ,这些类就是自动配置类*/
List<String> configurations = getCandidateConfigurations(annotationMetadata,
attributes);
configurations = removeDuplicates(configurations);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
//根据用户自定义的规则筛选
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
//过滤场景启动器中没有用到的
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}

获取 文件中的配置,

得到的所有自动配置类,spring-boot-autoconfigure 包中的/META-INF/spring.factories中,这个是springboot官方集成的自动配置类

经过一系列的筛选 最后只返回了22 个自动配置类 ,而这些 则是spring-boot-starter-web 场景启动器所需要的,

而这22个主启动类, 则进行了所有默认的配置工作,满足web 开发的基本需求

看看这个filter方法是怎么过滤掉不需要的自动配置类的:

private List<String> filter(List<String> configurations,
AutoConfigurationMetadata autoConfigurationMetadata) {
//记录时间
long startTime = System.nanoTime();
//待过滤的 118个类
String[] candidates = StringUtils.toStringArray(configurations);
//是否过滤掉的标致 默认全部为false 代表全部不过滤
boolean[] skip = new boolean[candidates.length];
//是否有过滤掉自动配置类
boolean skipped = false;
/*一共有三个过滤器 : OnClassConditional , OnWebApplicationConditional,OnBeanConditional, 分别过滤自动配置类上的三种类型的注解,后面详讲*/
for (AutoConfigurationImportFilter filter : getAutoConfigurationImportFilters()) {
//加强过滤器
invokeAwareMethods(filter);
/*对 待过滤的118个类进行过滤,返回的boolean数组就是过滤结果对应下标为false代表不符合条件*/
boolean[] match = filter.match(candidates, autoConfigurationMetadata);
//根据过滤条件,对skip进行修改为true(过滤掉了),并删除待过滤类列表的对应下标(置为null)
for (int i = 0; i < match.length; i++) {
if (!match[i]) {
skip[i] = true;
candidates[i] = null;
skipped = true;
}
}
}
//如果 一个没有被过滤掉 就直接返回
if (!skipped) {
return configurations;
}
//新的数组 返回实际的类
List<String> result = new ArrayList<>(candidates.length);
//将 已经过滤过的类数组 符合条件的 set入list中返回
for (int i = 0; i < candidates.length; i++) {
if (!skip[i]) {
result.add(candidates[i]);
}
}
if (logger.isTraceEnabled()) {
int numberFiltered = configurations.size() - result.size();
logger.trace("Filtered " + numberFiltered + " auto configuration class in "
+ TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)
+ " ms");
}
return new ArrayList<>(result);
}

经过这个方法的过滤,剩下的自动配置类就是本次项目中实际需要的依赖.

下面用两个配置类看一下自动配置类是怎么工作的

自动配置类

HttpEncodingAutoConfiguration 根据上面的截图可以看到 这个自动配置类是被注入进来的,那这个类为什么会被注入进来呢:

//该类为一个自动配置类, 其中标注@Bean的方法会向容器中注入组件
@Configuration /* 将配置文件中对应的值和HttpEncodingProperties绑定起来;并把 HttpEncodingProperties加入到ioc容器中 这个类就是读取我们配置application.yml 的配置类,*/
@EnableConfigurationProperties(HttpProperties.class) /*Spring底层 @Conditional注解(看我的另一个博客),根据不同的条件,如果 满足指定的条件,整个配置类里面的配置就会生效;    判断当前应用是否是servlet-based web应用,如果是,当前配置类生效 */
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) /*判断当前项目有没有这个类 CharacterEncodingFilter;SpringMVC中进行乱码解决的过滤器*/
@ConditionalOnClass(CharacterEncodingFilter.class) /* 判断配置文件中是否存在某个配置  spring.http.encoding.enabled 是否为true 默认为true;如果手动关闭了这个选项则这个类的组件不会注入 */
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true) public class HttpEncodingAutoConfiguration

而决定这个类是否被过滤的最重要的两个注解及是 @ConditionalOnWebApplication 和 @ConditionalOnClass

是否为web项目 是否为包含某个类 我们这个HelloWorld项目都是满足了 则没有被过滤掉

再看看这个类HttpProperties 也很关键 在自动配置类上 指定了这个类为配置类 点进去看一下:

下面是删减后的HttpProperties:

//这个注解即会读取配置文件中 spring.http.* 的信息,并组装这个类的属性
@ConfigurationProperties(prefix = "spring.http")
public class HttpProperties { private Map<Locale, Charset> mapping; private Charset charset = DEFAULT_CHARSET; private final Encoding encoding = new Encoding(); public static class Encoding { private Boolean force; private Boolean forceRequest; private Boolean forceResponse; } }

相对的则是配置文件中这些信息 和上面的属性一一对应:

再来看一个自动配置类为什么没有被注入进来:

比如 RedisAutoConfiguration,看到源码, 如果没有导入相关依赖 则连idea都检测出来了,所以@ConditionalOnClass(RedisOperations.class) 这个注解肯定是不满足条件的

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {

@Conditional 派生的注解

可以用这些注解去更加灵活的配置自动配置类

@Conditional扩展注解 : 作用(判断是否满足当前指定条件)
@ConditionalOnJava 系统的java版本是否符合要求
@ConditionalOnBean 容器中存在指定Bean;
@ConditionalOnMissingBean 容器中不存在指定Bean;
@ConditionalOnExpression 满足SpEL表达式指定
@ConditionalOnClass 系统中有指定的类
@ConditionalOnMissingClass 系统中没有指定的类
@ConditionalOnSingleCandidate 容器中只有一个指定的Bean,或者这个Bean是首选Bean
@ConditionalOnProperty 系统中指定的属性是否有指定的值
@ConditionalOnResource 类路径下是否存在指定资源文件
@ConditionalOnWebApplication 当前是web环境
@ConditionalOnNotWebApplication 当前不是web环境
@ConditionalOnJnd i JNDI存在指定项

嵌入式容器

Spring Boot包括对嵌入式Tomcat,Jetty , Undertow 和 Reactor-Netty(2.x中加入,为了兼容webflux) 服务器的支持。默认情况下,Springboot使用的是Tomcat作为嵌入式服务器,在监听默认的端口8080。这是我们为什么不用自己配置Servlet容器的原因

查看pom文件的依赖 可以看到 spring-boot-starter-web 默认引入的就是 spring-boot-starter-tomcat,同样我们也可以将容器切换为其他的 比如jetty

更改pom: 将默认的tomcat依赖排除 引入 jetty依赖 ,

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>spring-boot-starter-jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
</dependencies>

然后启动即可, springboot已经根据环境自动切换 ,启动成功

Jetty started on port(s) 8080 (http/1.1) with context path '/'

那么 springboot 是怎么进行对容器的自动配置的呢:

主要是这个类EmbeddedWebServerFactoryCustomizerAutoConfiguration ,这个类在/META/spring.factories 文件中被注入容器中,而下面的四个方法则 分别判断当前环境中导入的容器 并注入对应的WebServerFactoryCustomizer 工厂初始化类

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration { //Tomcat环境
@Configuration
@ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })
public static class TomcatWebServerFactoryCustomizerConfiguration { @Bean
public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
} } //Jetty环境
@Configuration
@ConditionalOnClass({ Server.class, Loader.class, WebAppContext.class })
public static class JettyWebServerFactoryCustomizerConfiguration { @Bean
public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new JettyWebServerFactoryCustomizer(environment, serverProperties);
} } //Undertow 环境
@Configuration
@ConditionalOnClass({ Undertow.class, SslClientAuthMode.class })
public static class UndertowWebServerFactoryCustomizerConfiguration { @Bean
public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
} } // Netty 环境 (需要webflux的支持 这里不做讲解)
@Configuration
@ConditionalOnClass(HttpServer.class)
public static class NettyWebServerFactoryCustomizerConfiguration { @Bean
public NettyWebServerFactoryCustomizer nettyWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new NettyWebServerFactoryCustomizer(environment, serverProperties);
}
} }

我们以TomcatWebServerFactoryCustomizer举例 ,这个类 在 customize 方法中 读取 我们配置的各种配置,(例如端口), 并set入 ConfigurableTomcatWebServerFactory Tomcat 真正的工厂类中.

public class TomcatWebServerFactoryCustomizer implements
WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory>, Ordered { @Override
public void customize(ConfigurableTomcatWebServerFactory factory) {
ServerProperties properties = this.serverProperties;
ServerProperties.Tomcat tomcatProperties = properties.getTomcat();
PropertyMapper propertyMapper = PropertyMapper.get();
propertyMapper.from(tomcatProperties::getBasedir).whenNonNull()
.to(factory::setBaseDirectory);
propertyMapper.from(tomcatProperties::getBackgroundProcessorDelay).whenNonNull()
.as(Duration::getSeconds).as(Long::intValue)
.to(factory::setBackgroundProcessorDelay);
customizeRemoteIpValve(factory);
propertyMapper.from(tomcatProperties::getMaxThreads).when(this::isPositive)
.to((maxThreads) -> customizeMaxThreads(factory,
tomcatProperties.getMaxThreads()));
propertyMapper.from(tomcatProperties::getMinSpareThreads).when(this::isPositive)
.to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads));
propertyMapper.from(this::determineMaxHttpHeaderSize).whenNonNull()
.asInt(DataSize::toBytes).when(this::isPositive)
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
maxHttpHeaderSize));
propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull()
.asInt(DataSize::toBytes)
.to((maxSwallowSize) -> customizeMaxSwallowSize(factory, maxSwallowSize));
propertyMapper.from(tomcatProperties::getMaxHttpPostSize).asInt(DataSize::toBytes)
.when((maxHttpPostSize) -> maxHttpPostSize != 0)
.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory,
maxHttpPostSize));
propertyMapper.from(tomcatProperties::getAccesslog)
.when(ServerProperties.Tomcat.Accesslog::isEnabled)
.to((enabled) -> customizeAccessLog(factory));
propertyMapper.from(tomcatProperties::getUriEncoding).whenNonNull()
.to(factory::setUriEncoding);
propertyMapper.from(properties::getConnectionTimeout).whenNonNull()
.to((connectionTimeout) -> customizeConnectionTimeout(factory,
connectionTimeout));
propertyMapper.from(tomcatProperties::getMaxConnections).when(this::isPositive)
.to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
customizeStaticResources(factory);
customizeErrorReportValve(properties.getError(), factory);
}

ConfigurableTomcatWebServerFactory 工厂类中 手动构建一个 Tomcat对象 并进行相应的配置,至此 一个嵌入式容器就创建完成

@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null) ? this.baseDirectory
: createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);
return getTomcatWebServer(tomcat);
}

SpringBoot2.x 启动过程详解的更多相关文章

  1. Linux启动过程详解(inittab、rc.sysinit、rcX.d、rc.local)

    启动第一步--加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬 ...

  2. Linux启动过程详解

    Linux启动过程详解 附上两张图,加深记忆 图1: 图2: 第一张图比较简洁明了,下面对第一张图的步骤进行详解: 加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的 ...

  3. Android 核心分析 之八Android 启动过程详解

    Android 启动过程详解 Android从Linux系统启动有4个步骤: (1) init进程启动 (2) Native服务启动 (3) System Server,Android服务启动 (4) ...

  4. 【STM32H7教程】第13章 STM32H7启动过程详解

    完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第13章       STM32H7启动过程详解 本章教 ...

  5. fabric网络环境启动过程详解

    这篇文章对fabric的网络环境启动过程进行讲解,也就是我们上节讲到的启动测试fabric网络环境时运行network_setup.sh这个文件的执行流程 fabric网络环境启动过程详解 上一节我们 ...

  6. (转)Linux 开机引导和启动过程详解

    Linux 开机引导和启动过程详解 编译自:https://opensource.com/article/17/2/linux-boot-and-startup作者: David Both 原创:LC ...

  7. linux 开机启动过程详解

    Linux开机执行内核后会启动init进程,该进程根据runlevel(如x)执行/etc/rcx.d/下的程序,其下的程序是符号链接,真正的程序放在/etc/init.d/下.开机启动的程序(服务等 ...

  8. 转-Linux启动过程详解(inittab、rc.sysinit、rcX.d、rc.local)

    http://blog.chinaunix.net/space.php?uid=10167808&do=blog&id=26042   1)BIOS自检2)启动Grub/Lilo3)加 ...

  9. [转载] Linux启动过程详解-《别怕Linux编程》之八

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket.为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. = ...

  10. Linux启动过程详解 (转)

    启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...

随机推荐

  1. 【2】Anaconda下:ipython文件的打开方式,Jupyter Notebook中运行.py文件,快速打开ipython文件的方法!

    相关文章: [1]Anaconda安装超简洁教程,瞬间学会! [2]Anaconda下:ipython文件的打开方式,Jupyter Notebook中运行.py文件,快速打开ipython文件的方法 ...

  2. 4.4 EAT Hook 挂钩技术

    EAT(Export Address Table)用于修改动态链接库(DLL)中导出函数的调用.与IAT Hook不同,EAT Hook是在DLL自身中进行钩子操作,而不是修改应用程序的导入表.它的原 ...

  3. Python 实现SynFlood洪水攻击

    Syn-Flood攻击属于TCP攻击,Flood类攻击中最常见,危害最大的是Syn-Flood攻击,也是历史最悠久的攻击之一,该攻击属于半开放攻击,攻击实现原理就是通过发送大量半连接状态的数据包,从而 ...

  4. 【C语言深度解剖】一篇解决程序的环境【编译+链接详解】让面试官给我们竖起大拇指

    文章目录 程序的翻译环境 翻译环境详解 编译 预编译 编译 汇编 关于形成符号表 链接 运行环境 尾声 [C语言深度解剖][Linux操作系统]程序的环境[编译+链接详解] 那么这里博主先安利一下一些 ...

  5. P6824 「EZEC-4」可乐 题解

    题目链接:可乐 一开始想着 0-1 Trie,枚举 \(x\) 去写,然后判断就行了.然后想起南京区域赛的 C 题,其实和这个也有点大同小异的感觉,可以用更朴素的办法,找到对于一个 \(a_i\) 而 ...

  6. 探索C语言中的联合体与枚举:数据多面手的完美组合!

    ​ 欢迎大家来到贝蒂大讲堂 养成好习惯,先赞后看哦~ 所属专栏:C语言学习 贝蒂的主页:Betty's blog 1. 联合体的定义 联合体又叫共用体,它是一种特殊的数据类型,允许您在相同的内存位置存 ...

  7. NC50439 tokitsukaze and Soldier

    题目链接 题目 题目描述 在一个游戏中,tokitsukaze需要在n个士兵中选出一些士兵组成一个团去打副本. 第i个士兵的战力为v[i],团的战力是团内所有士兵的战力之和. 但是这些士兵有特殊的要求 ...

  8. Spring Boot整合Postgres实现轻量级全文搜索

    有这样一个带有搜索功能的用户界面需求: 搜索流程如下所示: 这个需求涉及两个实体: "评分(Rating).用户名(Username)"数据与User实体相关 "创建日期 ...

  9. 【OpenGL ES】Blinn改进的冯氏光照模型

    1 前言 ​ 光照元素主要有环境光(ambient).漫反射光(diffuse).镜面反射光(specular),光照模型主要有冯氏模型和 Blinn 改进的冯氏模型,两者区别在与镜面反射光的计算,冯 ...

  10. Vuex和普通全局对象

    Vuex中的核心方法 Vuex是一个专为Vue.js应用程序开发的状态管理模式,其采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.每一个Vuex应用的核心就是 ...