1、Profile功能

为了方便多环境适配,springboot简化了profile功能。

1、application-profile功能

  • 默认配置文件 application.yaml;任何时候都会加载
  • 指定环境配置文件 application-{env}.yaml
  • 激活指定环境
    • 配置文件激活
    • 命令行激活:java -jar xxx.jar —spring.profiles.active=prod —person.name=haha
      • 修改配置文件的任意值,命令行优先
  • 默认配置与环境配置同时生效
  • 同名配置项,profile配置优先

    2、@Profile条件装配功能

    1. @Configuration(proxyBeanMethods = false)
    2. @Profile("production")
    3. public class ProductionConfiguration {
    4. // ...
    5. }

    3、profile分组

    ``` spring.profiles.group.production[0]=proddb spring.profiles.group.production[1]=prodmq

使用:—spring.profiles.active=production 激活

  1. <a name="fB5qd"></a>
  2. # 2、外部化配置
  3. [https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config)
  4. 1. Default properties (specified by setting `SpringApplication.setDefaultProperties`).
  5. 1. `@PropertySource` annotations on your `@Configuration` classes. Please note that such property sources are not added to the `Environment` until the application context is being refreshed. This is too late to configure certain properties such as `logging.*` and `spring.main.*` which are read before refresh begins.
  6. 1. **Config data (such as **`**application.properties**`** files)**
  7. 1. A `RandomValuePropertySource` that has properties only in `random.*`.
  8. 1. OS environment variables.
  9. 1. Java System properties (`System.getProperties()`).
  10. 1. JNDI attributes from `java:comp/env`.
  11. 1. `ServletContext` init parameters.
  12. 1. `ServletConfig` init parameters.
  13. 1. Properties from `SPRING_APPLICATION_JSON` (inline JSON embedded in an environment variable or system property).
  14. 1. Command line arguments.
  15. 1. `properties` attribute on your tests. Available on `@SpringBootTest` and the [test annotations for testing a particular slice of your application](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-tests).
  16. 1. `@TestPropertySource` annotations on your tests.
  17. 1. [Devtools global settings properties](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools-globalsettings) in the `$HOME/.config/spring-boot` directory when devtools is active.
  18. <a name="aOFOk"></a>
  19. ## 1、外部配置源
  20. 常用:**Java属性文件**、**YAML文件**、**环境变量**、**命令行参数**;
  21. <a name="EVV1l"></a>
  22. ## 2、配置文件查找位置
  23. (1) classpath 根路径<br />(2) classpath 根路径下config目录<br />(3) jar包当前目录<br />(4) jar包当前目录的config目录<br />(5) /config子目录的直接子目录
  24. <a name="VHleJ"></a>
  25. ## 3、配置文件加载顺序:
  26. 1. 当前jar包内部的application.properties和application.yml
  27. 1. 当前jar包内部的application-{profile}.properties 和 application-{profile}.yml
  28. 1. 引用的外部jar包的application.properties和application.yml
  29. 1. 引用的外部jar包的application-{profile}.properties 和 application-{profile}.yml
  30. <a name="LGVVN"></a>
  31. ## 4、指定环境优先,外部优先,后面的可以覆盖前面的同名配置项
  32. ......
  33. <a name="efPjL"></a>
  34. # 3、自定义starter
  35. <a name="DM2nk"></a>
  36. ## 1、starter启动原理
  37. - starter-pom引入 autoconfigurer 包
  38. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/10374550/1609673784445-c015dc1b-b743-4a3b-bb49-4c222bffd955.png#crop=0&crop=0&crop=1&crop=1&height=152&id=FD7ZX&margin=%5Bobject%20Object%5D&name=image.png&originHeight=181&originWidth=416&originalType=binary&ratio=1&rotation=0&showTitle=false&size=8343&status=done&style=none&title=&width=349)
  39. - autoconfigure包中配置使用 **META-INF/spring.factories** 中 **EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类**
  40. - **编写自动配置类 xxxAutoConfiguration -> xxxxProperties**
  41. - **@Configuration**
  42. - **@Conditional**
  43. - **@EnableConfigurationProperties**
  44. - **@Bean**
  45. - ......
  46. **引入starter --- xxxAutoConfiguration --- 容器中放入组件 ---- 绑定xxxProperties ---- 配置项**
  47. <a name="B7hoA"></a>
  48. ## 2、自定义starter
  49. **atguigu-hello-spring-boot-starter(启动器)**<br />**atguigu-hello-spring-boot-starter-autoconfigure(自动配置包)**
  50. <a name="HWmRa"></a>
  51. # 4、SpringBoot原理
  52. Spring原理【[Spring注解](https://www.bilibili.com/video/BV1gW411W7wy?p=1)】、**SpringMVC**原理、**自动配置原理**、SpringBoot原理
  53. <a name="k747C"></a>
  54. ## 1、SpringBoot启动过程
  55. - 创建 **SpringApplication**
  56. - 保存一些信息。
  57. - 判定当前应用的类型。ClassUtils。Servlet
  58. - **bootstrappers:初始启动引导器(**List<Bootstrapper>**):去spring.factories文件中找 **org.springframework.boot.**Bootstrapper**
  59. - 找 **ApplicationContextInitializer**;去**spring.factories找 ApplicationContextInitializer**
  60. - List<ApplicationContextInitializer<?>> **initializers**
  61. - **找 ApplicationListener ;应用监听器。**去**spring.factories ApplicationListener**
  62. - List<ApplicationListener<?>> **listeners**
  63. - 运行 **SpringApplication**
  64. - **StopWatch**
  65. - **记录应用的启动时间**
  66. - **创建引导上下文(Context环境)createBootstrapContext()**
  67. - 获取到所有之前的 **bootstrappers 挨个执行 **intitialize() 来完成对引导启动器上下文环境设置
  68. - 让当前应用进入**headless**模式。**java.awt.headless**
  69. - **获取所有 RunListener(运行监听器)【为了方便所有Listener进行事件感知】**
  70. - getSpringFactoriesInstances 去**spring.factories找 SpringApplicationRunListener**.
  71. - 遍历 **SpringApplicationRunListener 调用 starting 方法;**
  72. - **相当于通知所有感兴趣系统正在启动过程的人,项目正在 starting。**
  73. - 保存命令行参数;ApplicationArguments
  74. - 准备环境 prepareEnvironment();
  75. - 返回或者创建基础环境信息对象。**StandardServletEnvironment**
  76. - **配置环境信息对象。**
  77. - **读取所有的配置源的配置属性值。**
  78. - 绑定环境信息
  79. - 监听器调用 listener.environmentPrepared();通知所有的监听器当前环境准备完成
  80. - 创建IOC容器(createApplicationContext())
  81. - 根据项目类型(Servlet)创建容器,
  82. - 当前会创建 **AnnotationConfigServletWebServerApplicationContext**
  83. - **准备ApplicationContext IOC容器的基本信息 ** **prepareContext()**
  84. - 保存环境信息
  85. - IOC容器的后置处理流程。
  86. - 应用初始化器;applyInitializers;
  87. - 遍历所有的 **ApplicationContextInitializer 。调用 initialize.。来对ioc容器进行初始化扩展功能**
  88. - 遍历所有的 listener 调用 **contextPrepared。EventPublishRunListenr;通知所有的监听器contextPrepared**
  89. - **所有的监听器 调用 contextLoaded。通知所有的监听器 contextLoaded;**
  90. - **刷新IOC容器。**refreshContext
  91. - 创建容器中的所有组件(Spring注解)
  92. - 容器刷新完成后工作?afterRefresh
  93. - 所有监听 器 调用 listeners.**started**(context); **通知所有的监听器 started**
  94. - **调用所有runners;**callRunners()
  95. - **获取容器中的** **ApplicationRunner **
  96. - **获取容器中的 CommandLineRunner**
  97. - **合并所有runner并且按照@Order进行排序**
  98. - **遍历所有的runner。调用 run 方法**
  99. - **如果以上有异常,**
  100. - **调用Listener 的 failed**
  101. - **调用所有监听器的 running 方法 **listeners.running(context); **通知所有的监听器 running **
  102. - **running如果有问题。继续通知 failed 。调用所有 Listener 的 failed;通知所有的监听器
  103. ```java
  104. public interface Bootstrapper {
  105. /**
  106. * Initialize the given {@link BootstrapRegistry} with any required registrations.
  107. * @param registry the registry to initialize
  108. */
  109. void intitialize(BootstrapRegistry registry);
  110. }

image.png
image.png
image.png

  1. @FunctionalInterface
  2. public interface ApplicationRunner {
  3. /**
  4. * Callback used to run the bean.
  5. * @param args incoming application arguments
  6. * @throws Exception on error
  7. */
  8. void run(ApplicationArguments args) throws Exception;
  9. }
  1. @FunctionalInterface
  2. public interface CommandLineRunner {
  3. /**
  4. * Callback used to run the bean.
  5. * @param args incoming main method arguments
  6. * @throws Exception on error
  7. */
  8. void run(String... args) throws Exception;
  9. }

2、Application Events and Listeners

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-application-events-and-listeners
ApplicationContextInitializer
ApplicationListener
**

3、ApplicationRunner 与 CommandLineRunner