SpringBoot原理解析

目录

一、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
    • ApplicationContextInitializer;去spring.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
    • 调用所有runners;callRunners()
      • 获取容器中的 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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zoeil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值