SpringBoot原理解析

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

目录

一、Profile功能

一、application-profile功能

二、@Profile条件装配功能

三、profile分组

二、外部化配置

一、外部配置源

二、配置文件查找位置

三、配置文件加载顺序

四、指定环境优先外部优先后面的可以覆盖前面的同名配置项

三、自定义starter

一、starter启动原理

二、自定义starter

四、SpringBoot原理

一、SpringBoot启动过程

二、Application Events and Listeners

三、ApplicationRunner 与 CommandLineRunner


一、Profile功能

为了方便多环境适配比如生成、测试不同环境使用不同配置文件、启用不同的类等springboot简化了profile功能。

一、application-profile功能

  • 默认配置文件 application.yaml任何时候都会加载
  • 指定环境配置文件 application-{env}.yaml
  • 激活指定环境
    • 配置文件激活

这里的ppd模拟生产环境application.properties为默认配置文件都会生效

    • 命令行激活java -jar xxx.jar --spring.profiles.active=prod --person.name=haha
      • 修改配置文件的任意值命令行优先

使用Maven打成jar通过cmd命令行窗口执行命令

java -jar boot05-features-profile-0.0.1-SNAPSHOT.jar --spring.profiles.active=test

java -jar boot05-features-profile-0.0.1-SNAPSHOT.jar --spring.profiles.active=test --person.name=haha

  • 默认配置与环境配置同时生效
  • 同名配置项profile配置优先

总结

默认配置文件 < 精确配置文件 < 命令行

二、@Profile条件装配功能

可以配置在类上或者方法上

@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {

    // ...

}

测试

//person接口
public interface Person {

}

=========================================
//Boss类 prod环境启用
@Profile("prod")
@Data
@Component
@ConfigurationProperties("person")
public class Boss implements Person {

    public String name;
    public String age;
}

=========================================
//Worker类 test环境启用
@Profile("test")
@Data
@Component
@ConfigurationProperties("person")
public class Worker implements Person {

    public String name;
    public String age;
}

=========================================
//测试的Controller
@RestController
public class HelloController {

    @Autowired
    public Person person;

    @GetMapping("/")
    public String hello() {
        return person.getClass().toString();
    }

}

配置文件 

 运行结果

三、profile分组


#ppd、prod两份配置文件在指定myprod环境下都会激活
spring.profiles.group.myprod[0]=ppd
spring.profiles.group.myprod[1]=prod

spring.profiles.group.mytest[0]=test

使用--spring.profiles.active=myprod  激活

二、外部化配置

官网Core Features

一、外部配置源

常用Java属性文件YAML文件环境变量命令行参数

二、配置文件查找位置

(1) classpath 根路径

(2) classpath 根路径下config目录

(3) jar包当前目录

(4) jar包当前目录的config目录

(5) /config子目录的直接子目录

三、配置文件加载顺序

  1.  当前jar包内部的application.properties和application.yml
  2.  当前jar包内部的application-{profile}.properties 和 application-{profile}.yml
  3.  引用的外部jar包的application.properties和application.yml
  4.  引用的外部jar包的application-{profile}.properties 和 application-{profile}.yml

四、指定环境优先外部优先后面的可以覆盖前面的同名配置项

三、自定义starter

一、starter启动原理

  • starter-pom引入 autoconfigurer 包

  • autoconfigure包中配置使用 META-INF/spring.factoriesEnableAutoConfiguration 的值使得项目启动加载指定的自动配置类
  • 编写自动配置类 xxxAutoConfiguration -> xxxxProperties
    • @Configuration
    • @Conditional
    • @EnableConfigurationProperties
    • @Bean
    • ......

引入starter --- xxxAutoConfiguration --- 容器中放入组件 ---- 绑定xxxProperties ---- 配置项

二、自定义starter

boot-hello-starter启动器, 导入autoconfigure的依赖

        <dependency>
            <groupId>com.atguigu</groupId>
            <artifactId>boot-hello-starter-autoconfiguration</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

boot-hello-starter-autoconfigure自动配置包

目录结构

1.pom.xml

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

2.HelloProperties

@ConfigurationProperties("hello")
public class HelloProperties {
    private String prefix;
    private String suffix;

    public HelloProperties() {
    }

    public HelloProperties(String prefix, String suffix) {
        this.prefix = prefix;
        this.suffix = suffix;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

3.HelloServiceAutoConfiguration

@Configuration
@EnableConfigurationProperties(HelloProperties.class) //默认HelloProperties放在容器中
public class HelloServiceAutoConfiguration {

    public HelloServiceAutoConfiguration() {}

    @ConditionalOnMissingBean(HelloService.class)
    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        return helloService;
    }

}

4.HelloService

/*
 * 默认不放在容器中
 */
public class HelloService {

    @Autowired
    HelloProperties helloProperties;

    public void setProperties(String prefix, String suffix) {
        this.helloProperties = new HelloProperties(prefix, suffix);
    }

    public String sayhello(String username) {
        return helloProperties.getPrefix() + ":" +username + helloProperties.getSuffix();
    }
}

5.BootHelloStarterApplication

@SpringBootApplication
public class BootHelloStarterApplication {

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

}

6.META-INF下创建spring.factories指明自动配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.atguigu.hello.config.HelloServiceAutoConfiguration

7.使用maven先将 boot-hello-starter-autoconfigure打成jar包放在本地仓库接着再打包 boot-hello-starter 顺序不能颠倒

 8.新建项目引入我们的starter

        <dependency>
            <groupId>com.atguigu</groupId>
            <artifactId>boot-hello-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

9.测试的Controller 

@RestController
public class TestHello {

    @Autowired
    HelloService helloService;


    @GetMapping("/hello")
    public String sayhello() {
        return helloService.sayhello("张三");
    }

}

10.查看源码(bushi)

四、SpringBoot原理

Spring原理【Spring注解】、SpringMVC原理、自动配置原理、SpringBoot原理

一、SpringBoot启动过程

  • 创建 SpringApplication
    • 保存一些信息。
    • 判定当前应用的类型。ClassUtils。Servlet
    • bootstrappers初始启动引导器List<Bootstrapper>去spring.factories文件中找 org.springframework.boot.Bootstrapper
    • ApplicationContextInitializerspring.factoriesApplicationContextInitializer
      • List<ApplicationContextInitializer<?>> initializers
    • ApplicationListener 应用监听器。spring.factoriesApplicationListener
      • List<ApplicationListener<?>> listeners
  • 运行 SpringApplication
    • StopWatch
    • 记录应用的启动时间
    • 创建引导上下文Context环境createBootstrapContext()
      • 获取到所有之前的 bootstrappers 挨个执行 intitialize() 来完成对引导启动器上下文环境设置
    • 让当前应用进入headless模式。java.awt.headless
    • 获取所有 RunListener运行监听器【为了方便所有Listener进行事件感知】
      • getSpringFactoriesInstances 去spring.factoriesSpringApplicationRunListener.
    • 遍历 SpringApplicationRunListener 调用 starting 方法
      • 相当于通知所有感兴趣系统正在启动过程的人项目正在 starting。
    • 保存命令行参数ApplicationArguments
    • 准备环境 prepareEnvironment;
      • 返回或者创建基础环境信息对象。StandardServletEnvironment
      • 配置环境信息对象。
        • 读取所有的配置源的配置属性值。
      • 绑定环境信息
      • 监听器调用 listener.environmentPrepared()通知所有的监听器当前环境准备完成
    • 创建IOC容器createApplicationContext
      • 根据项目类型Servlet创建容器
      • 当前会创建 AnnotationConfigServletWebServerApplicationContext
    • 准备ApplicationContext IOC容器的基本信息   prepareContext()
      • 保存环境信息
      • IOC容器的后置处理流程。
      • 应用初始化器applyInitializers
        • 遍历所有的 ApplicationContextInitializer 。调用 initialize.。来对ioc容器进行初始化扩展功能
        • 遍历所有的 listener 调用 contextPrepared。EventPublishRunListenr通知所有的监听器contextPrepared
      • 所有的监听器 调用 contextLoaded。通知所有的监听器 contextLoaded
    • 刷新IOC容器。refreshContext
      • 创建容器中的所有组件Spring注解
    • 容器刷新完成后工作afterRefresh
    • 所有监听 器 调用 listeners.started(context); 通知所有的监听器 started
    • 调用所有runnerscallRunners()
      • 获取容器中的 ApplicationRunner
      • 获取容器中的 CommandLineRunner
      • 合并所有runner并且按照@Order进行排序
      • 遍历所有的runner。调用 run 方法
    • 如果以上有异常
      • 调用Listener 的 failed
    • 调用所有监听器的 running 方法 listeners.running(context); 通知所有的监听器 running
    • running如果有问题。继续通知 failed 。调用所有 Listener 的 failed通知所有的监听器 failed

对于springBoot启动原理还可以看这篇文章SpringBoot是如何动起来的_springboot headless mode_Java小果的博客-CSDN博客 

请你说说springboot的启动流程

springboot是通过main方法下的SpringApplication.run方法启动的,启动的时候他会调用refshContext方法,先刷新容器,然后根据解析注解或者解析配置文件的形式注册bean,而它是通过启动类的SpringBootApplication注解开始解析的,他会根据EnableAutoConfiguration开启自动化配置,里面有个核心方法ImportSelect选择性的导入,根据loadFactoryNames根据classpash路径以MATA-INF/spring.factories下面以xxxEnableAutoConfiguration开头的key去加载里面所有对应的自动化配置,并不是把这一百二十多个自动化配置全部导入,在他每个自动化配置里面都有条件判断注解(@ConditionalOnMissingBean、@ConditionalOnBean等注解),先判断是否引入相应的jar包,再判断容器是否有bean再进行注入到bean容器

二、Application Events and Listeners

Core Features

ApplicationContextInitializer

ApplicationListener

SpringApplicationRunListener

三、ApplicationRunner 与 CommandLineRunner

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring