SpringCloud Alibaba 2021微服务实战三十 springBoot 核心知识

经过前面一段时间的学习,我们队cloud有了一个系统的了解,在应用中我们要不断地理解源码的实现逻辑及原理,他们的基础就是spring boot,今天我们了解一下springboot 加深对cloud源码的理解及自己新建enable或者starter组件提供模板

特点

约定优于配置,集成功能强大,集成WEB容器等,在应用中也体会到了功能的强大。Spring Boot 以约定大于配置的核心思想,默认帮我们进行了很多设置,多数 Spring Boot 应用只需要很少的 Spring 配置。同时它集成了大量常用的第三方库配置(例如 Redis、MongoDB、Jpa、RabbitMQ、Quartz 等等),Spring Boot 应用中这些第三方库几乎可以零配置的开箱即用。优势:

  • 为所有Spring开发者更快的入门

  • 开箱即用,提供各种默认配置来简化项目配置

  • 内嵌式容器简化Web项目

  • 没有冗余代码生成和XML配置的要求

一,启动

@SpringBootApplication注解(重要)
@SpringBootApplication标注在某个类上说明这个类是SpringBoot的主配置类 , SpringBoot就应该运行这个类的main方法来启动SpringBoot应用,在启动过程中:

SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值;

将这些值作为自动配置类导入容器 , 自动配置类就生效 , 帮我们进行自动配置工作;

整个J2EE的整体解决方案和自动配置都在springboot-autoconfigure的jar包中(spring.factories);

它会给容器中导入非常多的自动配置类 (xxxAutoConfiguration), 就是给容器中导入这个场景需要的所有组件 , 并配置好这些组件 ;

有了自动配置类 , 免去了我们手动编写配置注入功能组件等的工作;

2)SpringApplication.run()
     这个类主要做了以下四件事情:

     1. 推断应用的类型是普通的项目还是Web项目

     2. 查找并加载所有可用初始化器 , 设置到initializers属性中

     3. 找出所有的应用程序监听器,设置到listeners属性中

     4. 推断并设置main方法的定义类,找到运行的主类

二、SpringBoot自动配置原理
以HttpEncodingAutoConfiguration(Http编码自动配置)为例解释自动配置原理,源码如下:

//表示这是一个配置类,和以前编写的配置文件一样,也可以给容器中添加组件;
@Configuration 
 
//启动指定类的ConfigurationProperties功能;
  //进入这个HttpProperties查看,将配置文件中对应的值和HttpProperties绑定起来;
  //并把HttpProperties加入到ioc容器中
@EnableConfigurationProperties({HttpProperties.class}) 
 
//Spring底层@Conditional注解
  //根据不同的条件判断,如果满足指定的条件,整个配置类里面的配置就会生效;
  //这里的意思就是判断当前应用是否是web应用,如果是,当前配置类生效
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
 
//判断当前项目有没有这个类CharacterEncodingFilter;SpringMVC中进行乱码解决的过滤器;
@ConditionalOnClass({CharacterEncodingFilter.class})
 
//判断配置文件中是否存在某个配置:spring.http.encoding.enabled;
  //如果不存在,判断也是成立的
  //即使我们配置文件中不配置pring.http.encoding.enabled=true,也是默认生效的;
@ConditionalOnProperty(
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)
 
public class HttpEncodingAutoConfiguration {
    //他已经和SpringBoot的配置文件映射了
    private final Encoding properties;
    //只有一个有参构造器的情况下,参数的值就会从容器中拿
    public HttpEncodingAutoConfiguration(HttpProperties properties) {
        this.properties = properties.getEncoding();
    }
    
    //给容器中添加一个组件,这个组件的某些值需要从properties中获取
    @Bean
    @ConditionalOnMissingBean //判断容器没有这个组件?
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding(this.properties.getCharset().name());
        filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.REQUEST));
        filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.RESPONSE));
        return filter;
    }
    //。。。。。。。
}
结论:

1、SpringBoot启动会加载大量的自动配置类

2、我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;

3、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件存在在其中,我们就不需要再手动配置了)

4、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可;

xxxxAutoConfigurartion:自动配置类;给容器中添加组件

xxxxProperties:封装配置文件中相关属性;

 Tips:SpringBoot推荐使用.yaml来配置,与properties不同在于yml除了存储键值对外还可以存储对象。
这里面最重要的理解以下几点:

1,各个注解的作用

2,@Configuration 配置类配置的需要容器管理的类有哪些

3,这些类属性配置如何装配,有何条件

4.配置类配置的类打成jar包如何才能让另一个spring容器管理,这里最重要的一个文件spring.factories

二,配置文件

配置文件名称是固定的;

application.properties,bootstrap.properties

语法结构 :key=value
application.yml  bootstrap.yml

语法结构 :key:空格 value
配置文件的作用 :修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了;

yml 
说明:语法要求严格!

1、空格不能省略

2、以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。

3、属性和值的大小写都是十分敏感的。


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

pets:
 - cat
 - dog
 - pig

三、配置文件值注入
跳转到目录
yaml文件更强大的地方在于,他可以给我们的实体类直接注入匹配值!

person:
  name: qinjiang
  age: 3
  happy: false
  birth: 2000/01/01
  maps: {k1: v1,k2: v2}
  lists:
   - code
   - girl
   - music
  dog:
    name: 旺财
    age: 1
/*
	@ConfigurationProperties作用:
	将配置文件中配置的每一个属性的值,映射到这个组件中;
	告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
	参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应

	// SpringBoot自动配置原理, 在application.yml中的配置, 都是给SpringBoot提供好的类设置字段值
*/
@Component //注册bean
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
}

还有@PropertySource(value = "classpath:person.properties")这个注解; 并配合@Value注解来使用;

伪代码

@Configuration
@PropertySource("classpath:db.properties")
@EnableTransactionManagement // 事务处理的注解
public class AppConfig {

    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.initialSize}")
    private int initialSize;

    // 连接池对象
    @Bean // bean的id就是dataSource,也可以起一个
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driverClassName);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setInitialSize(initialSize);
        return ds;
    }

    // 事务管理器对象;这里传的ds对象,就是上面的连接池对象,它会自动找ds对象
    @Bean
    public DataSourceTransactionManager txManager(DataSource ds){
        return new DataSourceTransactionManager(ds);
    }
}

@Value获取值和@ConfigurationProperties获取值比较

1、@ConfigurationProperties只需要写一次即可 , @Value则需要每个字段都添加

2、松散绑定:这个什么意思呢? 比如我的yml中写的last-name,这个和lastName是一样的, - 后面跟着的字母默认是大写的。这就是松散绑定。可以测试一下

3、JSR303数据校验 , 这个就是我们可以在字段是增加一层过滤器验证 , 可以保证数据的合法性

4、复杂类型封装,yml中可以封装对象 , 使用value就不支持

结论:

配置yml和配置properties都可以获取到值 , 强烈推荐 yml;

如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @Value;
 

@PropertySource&@ImportResource

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

/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 *      prefix = "person":配置文件中哪个下面的所有属性进行一一映射
 *
 * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
 *  @ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;
 *
 */
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {

    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
     * <bean/>
     */

   //lastName必须是邮箱格式
   // @Email
    //@Value("${person.last-name}")
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;

想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

//导入Spring的配置文件让其生效
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

SpringBoot 自动装配都是用的配置类!

3、配置文件占位符

Person:
  name: gzy${random.uuid}
  age: ${random.int}
  happy: false
  birth: 1998/2/11
  maps: {k1: v1, k2: v2}
  lists:
    - code
    - girl
    - music
  dog:
  	# 使用Person.hello这个配置的值,如果没有这个配置,则使用指定的默认值hello
    name: ${Person.hello:hello1}
    age: 1

 

多环境切换 Profile

  • 我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml用来指定多个环境版本;
  • 但是Springboot并不会直接启动这些配置文件,它默认使用application.properties主配置文件;

  • 我们需要通过一个配置来选择需要激活的环境:

  • #比如在配置文件中指定使用dev环境,我们可以通过设置不同的端口号进行测试;
    #我们启动SpringBoot,就可以看到已经切换到dev下的配置了;
    spring.profiles.active=dev
    yaml的多文档块

server:
  port: 8081
spring:
  profiles:
    active: dev
---
server:
  port: 8083
spring:
  profiles: dev # 配置环境的名称

---
server:
  port: 8084
spring:
  profiles: prod

还可以这样 pom标签

<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<!-- 环境标识,需要与配置文件的名称相对应 -->
				<profiles.active>dev</profiles.active>
			</properties>
			<activation>
				<!-- 默认环境 -->
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
	</profiles>

 

spring:  
  profiles:
    active: @profiles.active@

springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件:

优先级1:项目路径下的config文件夹配置文件
优先级2:项目路径下配置文件
优先级3:资源路径下的config文件夹配置文件
优先级4:资源路径下配置文件

优先级由高到底,高优先级的配置会覆盖低优先级的配置;
SpringBoot会从这四个位置全部加载主配置文件;互补配置;

我们再来看springboot项目启动

入口

@SpringBootApplication

public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

}
 

@Target(ElementType.TYPE) // 注解的适用范围,其中TYPE用于描述类、接口(包括包注解类型)或enum声明

@Retention(RetentionPolicy.RUNTIME) // 注解的生命周期,保留到class文件中(三个生命周期)

@Documented // 表明这个注解应该被javadoc记录

@Inherited // 子类可以继承该注解

@SpringBootConfiguration // 继承了Configuration,表示当前是注解类

@EnableAutoConfiguration // 开启springboot的注解功能,springboot的四大神器之一,其借助@import的帮助

@ComponentScan(excludeFilters = { // 扫描路径设置(具体使用待确认)

@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),

@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

public @interface SpringBootApplication {

...

} 

 

在其中比较重要的有三个注解,分别是:

  1)@SpringBootConfiguration // 继承了Configuration,表示当前是注解类

  2)@EnableAutoConfiguration // 开启springboot的注解功能,springboot的四大神器之一,其借助@import的帮助

  3)@ComponentScan(excludeFilters = { // 扫描路径设置(具体使用待确认)

   接下来对三个注解一一详解,增加对springbootApplication的理解:

 

1)@Configuration注解

  按照原来xml配置文件的形式,在springboot中我们大多用配置类来解决配置问题

   配置bean方式的不同: 

    a)xml配置文件的形式配置bean

您是否这样做过

<bean id="mockService" class="..MockServiceImpl">

...

</bean>

等同于

@Configuration

public class MockConfiguration{

    @Bean

    public MockService mockService(){

        return new MockServiceImpl();

    }

}

 

 

<bean id="mockService" class="..MockServiceImpl">

  <propery name ="dependencyService" ref="dependencyService" />

</bean>

<bean id="dependencyService" class="DependencyServiceImpl"></bean>
等同于

@Configuration

public class MockConfiguration{

  @Bean

  public MockService mockService(){

      return new MockServiceImpl(dependencyService());

  }

  @Bean

  public DependencyService dependencyService(){

      return new DependencyServiceImpl();

  }

}
这里面有个问题,如果出现循环依赖呢

a依赖b b 有依赖a

这个问题下一讲讲述

 

 

@ComponentScan注解
  作用:a)对应xml配置中的元素;

     b) (重点)ComponentScan的功能其实就是自动扫描并加载符合条件的组件(比如@Component和@Repository等)或者bean定义;

     c) 将这些bean定义加载到IoC容器中.

   我们可以通过basePackages等属性来细粒度的定制@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现会从声明@ComponentScan所在类的package及子包进行扫描。

  注:所以SpringBoot的启动类最好是放在root package下,因为默认不指定basePackages
 

 

@EnableAutoConfiguration
    此注解顾名思义是可以自动配置,所以应该是springboot中最为重要的注解。

    在spring框架中就提供了各种以@Enable开头的注解,例如: @EnableScheduling、@EnableCaching、@EnableMBeanExport等; @EnableAutoConfiguration的理念和做事方式其实一脉相承简单概括一下就是,借助@Import的支持,收集和注册特定场景相关的bean定义。  

  @EnableScheduling是通过@Import将Spring调度框架相关的bean定义都加载到IoC容器【定时任务、时间调度任务】
  @EnableMBeanExport是通过@Import将JMX相关的bean定义加载到IoC容器【监控JVM运行时状态】
     @EnableAutoConfiguration也是借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IoC容器。

     @EnableAutoConfiguration作为一个复合Annotation,其自身定义关键信息如下:

@SuppressWarnings("deprecation")

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Inherited

@AutoConfigurationPackage【重点注解】

@Import(AutoConfigurationImportSelector.class)【重点注解】

public @interface EnableAutoConfiguration {

...

}

其中最重要的两个注解已经标注:1、@AutoConfigurationPackage【重点注解】2、@Import(AutoConfigurationImportSelector.class)【重点注解】

    当然还有其中比较重要的一个类就是:EnableAutoConfigurationImportSelector.class

AutoConfigurationPackage注解:
 

 

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Inherited

@Import(AutoConfigurationPackages.Registrar.class)

public @interface AutoConfigurationPackage {

 

}

 

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {

 

        @Override

        public void registerBeanDefinitions(AnnotationMetadata metadata,

                BeanDefinitionRegistry registry) {

            register(registry, new PackageImport(metadata).getPackageName());

        }

 


    }

 

它其实是注册了一个Bean的定义;

new PackageImport(metadata).getPackageName(),它其实返回了当前主程序类的同级以及子级的包组件(重点);

(重点)那这总体就是注册当前主程序类的同级以及子级的包中的符合条件的Bean的定义

Import(AutoConfigurationImportSelector.class)注解

 

 

(重点)可以从图中看出  AutoConfigurationImportSelector 实现了 DeferredImportSelector 从 ImportSelector继承的方法:selectImports

@Override

    public String[] selectImports(AnnotationMetadata annotationMetadata) {

        if (!isEnabled(annotationMetadata)) {

            return NO_IMPORTS;

        }

        AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader

                .loadMetadata(this.beanClassLoader);

        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 = filter(configurations, autoConfigurationMetadata);

        fireAutoConfigurationImportEvents(configurations, exclusions);

        return StringUtils.toStringArray(configurations);

    }

 

第9行List<String> configurations = getCandidateConfigurations(annotationMetadata,attributes);其实是去加载各个组件jar下的   public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";外部文件。

该方法在springboot启动流程——bean实例化前被执行,返回要实例化的类信息列表;

如果获取到类信息,spring可以通过类加载器将类加载到jvm中,现在我们已经通过spring-boot的starter依赖方式依赖了我们需要的组件,那么这些组件的类信息在select方法中就可以被获取到。
 

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {

 List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.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;

 }

 其返回一个自动配置类的类名列表,方法调用了loadFactoryNames方法,查看该方法

public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {

 String factoryClassName = factoryClass.getName();

 return (List)loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());

 }

自动配置器会跟根据传入的factoryClass.getName()到项目系统路径下所有的spring.factories文件中找到相应的key,从而加载里面的类。

其中,最关键的要属@Import(AutoConfigurationImportSelector.class),借助AutoConfigurationImportSelector,@EnableAutoConfiguration可以帮助SpringBoot应用将所有符合条件(spring.factories)的bean定义(如Java Config@Configuration配置)都加载到当前SpringBoot创建并使用的IoC容器。

自动配置幕后英雄:SpringFactoriesLoader详解
借助于Spring框架原有的一个工具类:SpringFactoriesLoader的支持,@EnableAutoConfiguration可以智能的自动配置功效才得以大功告成!

SpringFactoriesLoader属于Spring框架私有的一种扩展方案,其主要功能就是从指定的配置文件META-INF/spring.factories加载配置,加载工厂类。

SpringFactoriesLoader为Spring工厂加载器,该对象提供了loadFactoryNames方法,入参为factoryClass和classLoader即需要传入工厂类名称和对应的类加载器,方法会根据指定的classLoader,加载该类加器搜索路径下的指定文件,即spring.factories文件;

传入的工厂类为接口,而文件中对应的类则是接口的实现类,或最终作为实现类。

所以,@EnableAutoConfiguration自动配置的魔法其实就变成了:

从classpath中搜寻所有的META-INF/spring.factories配置文件,并将其中org.springframework.boot.autoconfigure.EnableAutoConfiguration对应的配置项通过反射(Java Refletion)实例化为对应的标注了@Configuration的JavaConfig形式的IoC容器配置类,然后汇总为一个并加载到IoC容器。

启动流程图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值