spring boot学习与实践练习1

一、入门

本文以sping boot 2.4.*为例。

微服务

微服务是一种加购风格(服务微化),一个应用应该是一组小型服务,可以通过http方式进行互通;每一个功能元素都是一个可以独立替换和独立升级的软件单元。

Spring boot

spring-boot-starter-web、spring-boot-starter-webflux、spring-boot-starter-actuator等等
https://docs.spring.io/spring-boot/docs/2.4.0-SNAPSHOT/reference/html/using-spring-boot.html#using-boot-starter
spring-boot-starter-*:将所有功能场景都抽取出来,做成一个个starter(启动器),只需要在项目里面引入这些starter相关场景的依赖都会导入进来。

image.png

Spring boot的主入口类

image.png

这个类中有个重要的注解,叫做

@SpringBootApplication

当我点击该注解进入他的实现时,发现它是个组合注解类。


image.png

@SpringBootConfiguration:spring boot的配置类

这是spring boot的配置类注解,被它标注的类,就是spring boot的配置类
当我们再点击进入它的实现时会发现spring 底层的注解@Configuration


image.png

@Configuration:配置类

spring boot中只要是被@Configuration标注的类,都是配置类,也就是我们以前用的配置文件是一致的。
配置类 等同于 配置文件,由于之前配置文件太多,不方面管理,所以现在用配置类来管理参数,让我们的配置文件中的参数更具有逻辑性。配置类也是容器中的一个组件,我们点击@Configuration的时候就可以看到他是被spring的@Component标注的。


image.png

@EnableAutoConfiguration:开启自动配置功能

以前我们需要配置的东西,现在都不需要配置了,现在spring boot帮我自动配置,那如果想让他自动配置,就要加上这个注解。
那这个注解是怎么生效的呢?我们点击进入它的实现类发现,它被一下注解修饰,如下:

@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

@AutoConfigurationPackage:自动配置包

当我们点击进入它的实现发现如下:

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

其中@Import是spring底层的基础注解。这个注解的作用就是给容器中导入一个组件,导入的组件由AutoConfigurationPackages.Registrar.class决定。
@AutoConfigurationPackages作用是:《重要》《重要》《重要》将程序启动类(@SpringBootApplication标注的类)的所在包及下面所有子包里面所有组件扫描进入容器。《重要》《重要》《重要》
我们看到Registrar类里有个registerBeanDefinitions方法,里面有扫描到的包名,我们可以加断点,看下到底是注册的哪些包?


image.png

image.png

这样就可以解释,@AutoConfigurationPackages是如何自动装在spring组件进入容器的了。

@Import(AutoConfigurationImportSelector.class),我们在EnableAutoConfiguration中还存在一个@Import注解。

它是用来干吗的?
AutoConfigurationImportSelector:自动导入组件的选择器;我们点击进入它的源码,看下,他有一个返回值为String[]的selectImports方法


image.png

我们看下它给容器导入了什么东西,还是要打断点


image.png

我们发现它导入了很多xxxAutoConfiguration的类,因此我们可以断定AutoConfigurationImportSelector注解是用来给容器导入很多自动配置类。有了自动配置类就让我们免去了手动编写配置注入功能组件的工作。
SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass()
,getBeanClassLoader());

从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值。


image.png

总结得出

Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,讲这些值作为自动配置类导入到容器中,至此自动配置类就生效了,因此就可以帮我们做自动配置了。
Spring Boot就是这么强大,JavaEE的整体解决方案和自动配置都在spring-boot-autoconfiguration-版本号.jar里了。

如何使用idea的向导快速创建boot项目

image.png

image.png

Spring Boot的目录结构

主程序我们已经生成好了,只需要我们自己编写逻辑即可。
resource文件夹中目录结构
static:保存所有静态资源,js、css、images
templates:保存所有模板页,(spring 默认jar包使用嵌入式的tomcat,默认不支持jsp页面);可以使用模板引擎(freemarker、thymeleaf);
application.properties:Spring Boot 应用的配置文件。这个配置文件很重要,可以修改boot的一些默认设置。

二、Spring Boot的配置

1、配置文件
Spring Boot默认使用一个全局的配置文件,配置文件的名字是固定的。
application.properties的配置格式:

server.port=8081

application.yaml的配置格式:

server:
  port: 8081

配置文件的作用就是用来修改spring boot的默认配置。

2、YAML的语法

2.1基本语法
K:(空格)V:表示一对键值对(空格必须有);
以空格缩进来控制层级关系,只要是左对齐的一列数据,都是同一层级的。如下:

server: 
          port: 8081
          path: /hello
///属性和值是大小写敏感的

2.2值的写法
字面量:普通值(字符串、数字、布尔)
k:v:字面量直接写;字符串默认不用加上单引号和双引号
"":双引号,不会转义字符串里面的特殊字符;特殊字符会作为本身的含义格式输出。
例如:name:"aimi \n nihao" 输出 aimi 换行 nihao
'':单引号,会转义字符,特殊字符会作为一个普通的字符串输出。
例如:name:"aimi \n nihao" 输出 aimi \n nihao
对象、Map(属性和值)、(键值对):
k:v:在下一行来写对象的属性和值的关系,注意缩进。

friend: 
       lastname: aimi
       age: 30

也可以使用行内写法

friend: {lastname: aimi,age: 30}

数组:(List、Set)
用- 值表示数组中的一个元素

animals: 
        - cow
        - cat
        - dog

行内写法

animals: [cow,cat,dog]

2.3配置与bean映射
我们创建一个类,来写个综合性的配置映射bean的样例


image.png
image.png
///yaml的配置
  person:
    lastName: zhangsan
    age: 30
    boss: false
    birth: 2020/10/11
    map: {k1: v1,name: aimi}
    list: [zhang,abc,nihao]
    dog: {lastName: 泰迪, age: 3}

如何把yaml与我们的java bean关联起来呢?
@ConfigurationProperties将配置文件中的配置的属性与java对象进行映射绑定
prefix是指的前缀,是用来指定以什么开头的属性
@ConfigurationProperties默认是从全局配置文件中获取值

再加上注解@Component将这个组件加入到容器中

/**
 * @ConfigurationProperties将配置文件中的配置的属性与java对象进行映射绑定
 *          prefix是指的前缀,是用来指定以什么开头的属性
 *
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private boolean boss;
    private Date birth;
    private Map<String,Object> map;
    private List<Object> list;
    private Dog dog;
/**
 * Spring Boot的单元测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTest {

    @Autowired
    Person person;
    @Test
    public void contextLoads(){

        System.out.println(person);
    }

}

我们运行下测试用例,直接就可以看到已经被注入到容器中了。


image.png

当然@Value注解也可以进行配置文件数据的获取,在这里我就不赘述了。但是它和@ConfigurationProperties有什么区别?

2.4@Value和@ConfigurationProperties比较

(1)功能上:@Value是逐个制定key,而@ConfigurationProperties可以批量指定
(2)松散绑定:@Value不支持 ,而@ConfigurationProperties支持
(3)Spring EL表达式:@Value支持 ,而@ConfigurationProperties不支持
(4)数据校验:@Value不支持 ,而@ConfigurationProperties支持
什么时候选择合适的注解,这里建议:
如果程序中只是对单一属性进行赋值,那么就用@Value
如果程序中对javaBean的多个属性进行赋值,那么就用@ConfigurationProperties

2.5@PropertySource和@ImportResource

1、@PropertySource:加载指定的配置文件

@PropertySource(value = {"classpath:person.properties"})
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private boolean boss;
    private Date birth;
    private Map<String,Object> map;
    private List<Object> list;
    private Dog dog;
image.png

其中@PropertySource的value属性中可以指定多个配置文件,用英文逗号隔开即可。
2、@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效。

@ImportResource(locations = {"classpath:bean.xml"})
//导入spring 配置文件让其内部的bean组件生效

我们自己编写的配置文件,放在resource文件夹下


image.png
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cow" class="com.ls.demo.domain.Cow"></bean>
</beans>

Spring Boot不推荐这种方式加载配置文件声明的bean,这样太过繁琐。
Srping Boot推荐的方式是使用全部注解的方式。
1、配置类====Spring的xml配置文件
2、使用@Bean给容器添加组件

/**
 * @Configuration:指定当前类是一个配置类,就是来替代之前的spring配置文件
 * 在配置文件当中用<bean></bean>添加组件
 */
@Configuration
public class CowConfiguration {
    //将方法的返回值添加到容器中,容器中这个组件默认的bean的id就是方法名
    @Bean
    public Cow cow(){
        System.out.println("cow添加到容器中");
        return new Cow();
    }
}

2.6Profile

1、多Profile文件方式
我们在主配置文件编写的时候,文件名可以是applicat-{profile}.properties/yml,默认使用application.properties的配置
2、yml支持多文档块方式

server:
  port: 8081
spring:
  profiles:
    active: prod
---
server:
  port: 8082
spring:
  profiles: dev
---
server:
  port: 8083
spring:
  profiles: prod

3、激活制定的 profile
1、在配置文件中激活指定相应的配置文件spring.profiles.active=dev,该指定是在主配置文件中。
2、命令行模式激活(在程序启动时配置)
java -jar xxxx.jar --spring.profiles.active=dev
3、虚拟机参数
-Dspring.profiles.active=dev

2.7配置文件加载位置

springboot启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件
-file:./config/
-file:./
-classpath:/config/
-classpath:/
优先级由高到低,有优先级的配置会覆盖低优先级配置。
Spring Boot会从这个四个位置全部加载主配置文件;互补配置;

2.8配置文件加载顺序(常用的几种)

1、命令行参数
2、JNDI属性
3、Java系统属性(System.properties)
4、操作系统环境变量
5、jar包外部的application-{profile}.properties或者application.yml(带profile)配置文件
6、jar包内部的application-{profile}.properties或者application.yml(带profile)配置文件
7、jar包外部的application.properties或者application.yml(不带profile)配置文件
8、jar包内部的application.properties或者application.yml(不带profile)配置文件
9、@Configuration注解类上的@PropertySource
10、通过SpringApplication.setDefaultProperties指定的默认值。
所有配置文件的加载支持参看官方文档:
https://docs.spring.io/spring-boot/docs/2.3.4.RELEASE/reference/html/spring-boot-features.html#boot-features-external-config

自动配置原理

1)SpringBoot启动的时候加载主配置类,开启了自动配置功能@EnableAutoConfiguration
2)@EnableAutoConfiguration作用:利用@AutoConfigurationImportSelector往spring容器中导入了一些组件?
主要利用了他内部的实现方法selectImports来实现组件加载的。方法详情如下:

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        }
//这个方法中获取了被加载的组件
        AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
        return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }

我们看下getAutoConfigurationEntry方法里面的实现,如下:

    /**
     * Return the {@link AutoConfigurationEntry} based on the {@link AnnotationMetadata}
     * of the importing {@link Configuration @Configuration} class.
     * @param annotationMetadata the annotation metadata of the configuration class
     * @return the auto-configurations that should be imported
     */
    protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
        if (!isEnabled(annotationMetadata)) {
            return EMPTY_ENTRY;
        }
        AnnotationAttributes attributes = getAttributes(annotationMetadata);
        List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
        configurations = removeDuplicates(configurations);
        Set<String> exclusions = getExclusions(annotationMetadata, attributes);
        checkExcludedClasses(configurations, exclusions);
        configurations.removeAll(exclusions);
        configurations = getConfigurationClassFilter().filter(configurations);
        fireAutoConfigurationImportEvents(configurations, exclusions);
        return new AutoConfigurationEntry(configurations, exclusions);
    }

在getCandidateConfigurations中有具体实现。

    /**
     * Return the auto-configuration class names that should be considered. By default
     * this method will load candidates using {@link SpringFactoriesLoader} with
     * {@link #getSpringFactoriesLoaderFactoryClass()}.
     * @param metadata the source metadata
     * @param attributes the {@link #getAttributes(AnnotationMetadata) annotation
     * attributes}
     * @return a list of candidate configurations
     */
    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {

        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
                getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
                + "are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

//使用类加载器获取所有jar包中META-INF/spring.factories文件中org.springframework.boot.autoconfigure.EnableAutoConfiguration类对应的配置类,并加载入spring容器中。

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
        MultiValueMap<String, String> result = cache.get(classLoader);
        if (result != null) {
            return result;
        }

        try {
            Enumeration<URL> urls = (classLoader != null ?
                    classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
                    ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
            result = new LinkedMultiValueMap<>();
            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                UrlResource resource = new UrlResource(url);
                Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                for (Map.Entry<?, ?> entry : properties.entrySet()) {
                    String factoryTypeName = ((String) entry.getKey()).trim();
                    for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
                        result.add(factoryTypeName, factoryImplementationName.trim());
                    }
                }
            }
            cache.put(classLoader, result);
            return result;
        }
        catch (IOException ex) {
            throw new IllegalArgumentException("Unable to load factories from location [" +
                    FACTORIES_RESOURCE_LOCATION + "]", ex);
        }
    }

看下图:org.springframework.boot.autoconfigure.EnableAutoConfiguration下面对应了很多很多配置类,都是要被加载的。


image.png
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration

每一个这样的xxxAutoConfiguration类都是容器中的一个组件,都加入到容器中,用他们来自动装配,这是自动装配的开始。
3)每一个自动配置类如何进行自动装配的?
我们以一个为例说明自动配置原理:HttpEncodingAutoConfiguration(http编码自动配置)

org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration

点击进入他的源码:

@Configuration(proxyBeanMethods = false)//表示这是一个配置类,跟我们的以前编写的配置文件一个作用,可以为容器添加组件
@EnableConfigurationProperties(ServerProperties.class)//启用指定类的ConfigurationProperties功能,配置文件中能配置什么属性,就跟ServerProperties.class类的属性一一对应
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)//Spring底层根据@Conditional注解来做条件判断,如果满足指定条件,整个配置类里面的配置就会生效。这里的判断是当前应用是否是web应用,如果是,当前配置类生效,如果不是,则不生效。
@ConditionalOnClass(CharacterEncodingFilter.class)//判断当前项目是否存在CharacterEncodingFilter.class这个类,这个类大家都清楚,这是springMvc当中解决乱码的过滤器。
@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)//判断配置文件中是否存在某个配置前缀为server.servlet.encoding,matchIfMissing的意思即使在配置文件中不存在server.servlet.encoding,server.servlet.encoding.enabled=true配置也依然生效。
public class HttpEncodingAutoConfiguration {
    private final Encoding properties;

    public HttpEncodingAutoConfiguration(ServerProperties properties) {//这里已经把ServerProperties类映射到了properties属性上。
        this.properties = properties.getServlet().getEncoding();
    }

    @Bean //给容器添加一个组件,这个组件的某些值需要从properties中获取
    @ConditionalOnMissingBean//这里是条件判断,意思是当容器中不存在这个CharacterEncodingFilter 这个bean,他就加载入容器
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
//这里面设置的编码,就来自于ServerProperties 类对应的配置属性。
        filter.setEncoding(this.properties.getCharset().name());
        filter.setForceRequestEncoding(this.properties.shouldForce(Encoding.Type.REQUEST));
        filter.setForceResponseEncoding(this.properties.shouldForce(Encoding.Type.RESPONSE));
        return filter;
    }

5)所有在配置文件中能配置的属性都是xxxProperties类中封装着;

//从配置文件中找到指定的值和ServerProperties对象进行绑定
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {

根据当前注解判断,决定这个配置类是否生效。

因此我们可以在application.properties中配置

##我们能配置的属性来源于ServerProperties类中的内部类Servlet下的Encoding类的属性
server.servlet.encoding.charset=utf-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true

Spring boot的精华

1)Spring Boot启动会加载大量的自动配置类。
2)当我们要实现某类功能时,先查看SpringBoot中默认是否写好了相应的配置类。
3)如果有相应配置类,我们需要查看它配置了哪些组件。如果没有需要我们自己编写相应的配置类。
4)给容器中自动配置类添加组件的时候,会从properties中获取某些属性,我们可以在对应的配置文件中设置相应的值。
XXXAutoConfiguration:自动配置类
给容器添加组件
xxxProperties:封装配置文件中相应的属性

条件注解@Conditional

1、@Conditional
作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置类里面的所有内容才生效。
SpringBoot中扩展了很多条件注解,常用的如下:
@ConditionalOnJava 系统的java版本是否符合要求
@ConditionalOnBean 容器中存在指定的bean
@ConditionalOnMissBean 容器中不存在指定的bean
@ConditionalOnExpression 满足SpEL表达式指定
@ConditionalOnClass 系统中有指定的类
@ConditionalOnMissClass 系统中没有指定的类
@ConditionalOnSingleCandidate 容器中只有一个指定的bean,或者这个bean是首选bean
@ConditionalOnProperty 系统中指定的属性是否有指定的值
@ConditionalOnResource 类路径下是否存在指定资源文件
@ConditionalOnWebApplication 当前是web环境
@ConditionalOnNotWebApplication 当前不是web环境
@ConditionalOnJndi JNDI存在指定项

加载的xxxAutoConfiguration配置类不是所有的配置类都生效,他是在一定条件下才生效。

那我们怎样才能确定EnableAutoConfiguration下加载的自动配置类,是否生效?
我们需要在主配置文件中开启debug模式

##开启spring boot的debug模式
debug=true

启动boot

//自动配置报告
============================
CONDITIONS EVALUATION REPORT
============================

已经启用的自动配置类

Positive matches:
-----------------
AopAutoConfiguration matched:
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

   AopAutoConfiguration.ClassProxyingConfiguration matched:
      - @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
      - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)

   AuditEventsEndpointAutoConfiguration matched:
      - @ConditionalOnAvailableEndpoint no property management.endpoint.auditevents.enabled found so using endpoint default; @ConditionalOnAvailableEndpoint marked as exposed by a 'management.endpoints.jmx.exposure' property (OnAvailableEndpointCondition)

   BeansEndpointAutoConfiguration matched:
      - @ConditionalOnAvailableEndpoint no property management.endpoint.beans.enabled found so using endpoint default; @ConditionalOnAvailableEndpoint marked as exposed by a 'management.endpoints.jmx.exposure' property (OnAvailableEndpointCondition)

   BeansEndpointAutoConfiguration#beansEndpoint matched:
      - @ConditionalOnMissingBean (types: org.springframework.boot.actuate.beans.BeansEndpoint; SearchStrategy: all) did not find any beans (OnBeanCondition)
。。。。。。。。还有很多省略了。。。。。。。

没有启动的自动配置类

Negative matches:
-----------------
 ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   AopAutoConfiguration.AspectJAutoProxyingConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)

   AppOpticsMetricsExportAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'io.micrometer.appoptics.AppOpticsMeterRegistry' (OnClassCondition)

   ArtemisAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   AtlasMetricsExportAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'io.micrometer.atlas.AtlasMeterRegistry' (OnClassCondition)
。。。。。。。。还有很多省略了。。。。。。。
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,719评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,337评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,887评论 0 324
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,488评论 1 266
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,313评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,284评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,672评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,346评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,644评论 1 293
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,700评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,457评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,316评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,706评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,990评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,261评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,648评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,859评论 2 335