22、原理解析

2023-05-16

文章目录

  • 1、Profile功能
    • 1、application-profile功能
    • 2、@Profile条件装配功能
    • 3、profile分组
  • 2、外部化配置
    • 1、外部配置源
    • 2、配置文件查找位置
    • 3、配置文件加载顺序:
    • 4、指定环境优先,外部优先,后面的可以覆盖前面的同名配置项
  • 3、自定义starter
    • 1、starter启动原理
    • 2、自定义starter
  • 4、SpringBoot原理
    • 1、SpringBoot启动过程
    • 2、Application Events and Listeners
    • 3、ApplicationRunner 与 CommandLineRunner


【尚硅谷】SpringBoot2零基础入门教程-讲师:雷丰阳
笔记

路还在继续,梦还在期许

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条件装配功能

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

    // ...

}

3、profile分组

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

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

2、外部化配置

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

  1. Default properties (specified by setting SpringApplication.setDefaultProperties).
  2. @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.
  3. Config data (such as application.properties files)
  4. A RandomValuePropertySource that has properties only in random.*.
  5. OS environment variables.
  6. Java System properties (System.getProperties()).
  7. JNDI attributes from java:comp/env.
  8. ServletContext init parameters.
  9. ServletConfig init parameters.
  10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  11. Command line arguments.
  12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  13. @TestPropertySource annotations on your tests.
  14. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.

1、外部配置源

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

2、配置文件查找位置

(1) classpath 根路径
(2) classpath 根路径下config目录
(3) jar包当前目录
(4) jar包当前目录的config目录
(5) /config子目录的直接子目录

3、配置文件加载顺序:

  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

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

3、自定义starter

1、starter启动原理

● starter-pom引入 autoconfigurer 包

在这里插入图片描述

● autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类
● 编写自动配置类 xxxAutoConfiguration -> xxxxProperties
○ @Configuration
○ @Conditional
○ @EnableConfigurationProperties
○ @Bean
○ …
引入starter — xxxAutoConfiguration — 容器中放入组件 ---- 绑定xxxProperties ---- 配置项

2、自定义starter

atguigu-hello-spring-boot-starter(启动器)
atguigu-hello-spring-boot-starter-autoconfigure(自动配置包)

4、SpringBoot原理

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

1、SpringBoot启动过程

● 创建 SpringApplication
○ 保存一些信息。
○ 判定当前应用的类型。ClassUtils。Servlet
○ bootstrappers:初始启动引导器(List):去spring.factories文件中找 org.springframework.boot.Bootstrapper
○ 找 ApplicationContextInitializer;去spring.factories找 ApplicationContextInitializer
■ List<ApplicationContextInitializer<?>> initializers ○ 找 ApplicationListener ;应用监听器。去spring.factories找 ApplicationListener ■ List

public interface Bootstrapper {

	/**
	 * Initialize the given {@link BootstrapRegistry} with any required registrations.
	 * @param registry the registry to initialize
	 */
	void intitialize(BootstrapRegistry registry);

}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

@FunctionalInterface
public interface ApplicationRunner {

	/**
	 * Callback used to run the bean.
	 * @param args incoming application arguments
	 * @throws Exception on error
	 */
	void run(ApplicationArguments args) throws Exception;

}
@FunctionalInterface
public interface CommandLineRunner {

	/**
	 * Callback used to run the bean.
	 * @param args incoming main method arguments
	 * @throws Exception on error
	 */
	void run(String... args) throws Exception;

}

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
SpringApplicationRunListener

3、ApplicationRunner 与 CommandLineRunner

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

22、原理解析 的相关文章

  • 【UNIX环境高级编程】UNIX基础知识

    UNIX环境高级编程 UNIX基础知识 1 UNIX体系结构 从严格意义上 xff0c 可将操作系统定义为一种软件 xff0c 它控制计算机硬件资源 xff0c 提供程序运行环境 xff0c 我们称这种软件为内核 内核的接口称为系统调用 公
  • postman能正常打开但不显示窗口

    1 最近使用postman偶尔出现以下问题 postman在任务栏能正常打开 xff0c 如下图 xff0c 使用Alt 43 Tab也能看到 xff0c 但是窗口就是显示不了 2 解决方案 将鼠标放在任务栏上 xff0c 使用快捷键Alt
  • Linux文件相关常用命令(超全超详细)

    目录 command终端命令格式 xff1a command options parameter 1 ls命令 2 cd命令 3 touch 命令 4 mkdir命令 5 pwd命令 6 clear 命令 7 rm命令 8 cp命令 9 m
  • Linux远程管理常用命令(超全超详细)【持续更新】

    目录 1 shutdown命令 2 ifconfig命令 3 uname命令 4 uptime命令 5 free命令 6 who命令 7 last命令 8 history命令 9 ping命令 10 chmod 命令 11 chown 命令
  • 基于springcloud 的Eureka的服务注册与发现

    1 注册中心用来管理每个服务与服务之间的依赖关系 xff08 服务治理 xff09 xff0c 存放服务地址相关信息 xff08 接口地址 xff09 2 服务提供者 xff1a 提供服务接口 3 服务消费者 xff1a 调用服务接口 4
  • 计算机系统层次存储结构

    问 xff1a 当前计算机系统一般会采用层次结构存储数据 xff0c 请介绍下典型计算机存储系统一般分为哪几个层次 xff0c 为什么采用分层存储数据能有效提高程序的执行效率 xff1f 所谓存储系统的层次结构 xff0c 就是把各种不同存
  • Springboot的cache缓存机制

    我们知道一个程序的瓶颈在于数据库 xff0c 我们也知道内存的速度是大大快于硬盘的速度的 当我们需要重复地获取相同的数据的时候 xff0c 我们一次又一次的请求数据库或者远程服务 xff0c 导致大量的时间耗费在数据库查询或者远程方法调用上
  • Python-异常处理+文件

    目录 1 异常处理 1 简单的异常捕获 2 捕获错误类型 3 捕获未知错误 4 完整的异常语法 5 异常的传递 6 抛出异常 2 文件 1 读取文件 2 读取文件后文件指针会改变 3 写入文件 4 分行读取文件 5 复制文件 6 复制大文件
  • Eureka的设计理念

    目录 1 概述 1 1 服务实例如何注册到服务中心 1 2服务实例如何从服务中心剔除 1 3 服务实例信息的一致性问题 2 AP优于CP 3 Peer to Peer架构 3 1 主从复制 3 2 对等复制 4 Zone及Region设计
  • fastjson解析出现引用问题

    1 问题描述 后端返回前端接口数据包含引用数据 xff0c 如下图所示 2 原因 转json时使用这种方式 xff0c fastjson自动使用循环引用 xff1a String content 61 JSONObject toJSONSt
  • idea项目中添加mapper.xml文件样例

    1 点击File gt Settings gt 步骤如下图 然后新建mapper xml文件 点击mapper即可 2 添加其他模板方法同上
  • win10系统jdk1.8和tomcat8环境变量配置

    1 jdk环境变量配置 1 xff09 JAVA HOME 变量值就是jdk安装地址 JRE HOME 变量值就是jre安装地址 2 CLASSPATH 变量为 JAVA HOME lib JAVA HOME lib dt jar JAVA
  • Redis-事物&事物的四大特性(ACID)

    Redis事物 事物是指一系列操作步骤 xff0c 这一系列操作步骤 xff0c 要么完全执行 xff0c 要么完全不执行 Redis中的事物 transaction 是一组命令的集合 xff0c 至少是两个或两个以上的命令 xff0c r
  • 【chrome不能扩展程序怎么办】4步搞定安装导入扩展

    在 Edge 浏览器输入 xff1a edge version xff0c 按回车键后 xff0c 可以看到 用户配置路径 xff1a xxx xff0c 路径下的 Extensions 文件夹就是 Edge 安装扩展的目录 IE扩展位置
  • MariaDB Logs

    查询日志 xff1a query log 慢查询日志 xff1a slow query log 事务日志 xff1a transaction log 二进制日志 xff1a binary log 中继日志 xff1a relay log 错
  • android ViewBinding

    一 kotlin android extensions 在使用ViewBinding之前 xff0c 我们一直使用的是kotlin android extensions xff0c 使用kotlin android extensions可以
  • 如何添加win10命令提示符字体,美化显示效果

    如图 win10命令提示符的字体真让人难受的要死 xff0c 难道就不能改成好看一点的吗 xff1f 笔者开始了停不下来的百度和Google 在查找了大量零碎或者过时的资料后 xff0c 终于找到了一篇看起来还是很靠谱的文章 xff1a Q
  • 手动修改KDM、KSPLASH主题

    system xff1a ubuntu 13 04 x64 Qt 4 8 4 KDE Development Platform 4 10 5 kde4 config 1 0 自从安装了KDE桌面管理系统至今 xff0c 一直在折腾系统主题
  • Linux 安装Python3

    python3 下载地址 python3 下载地址 https www python org downloads 选择自己需要的版本 此文中选择3 10 9 下载源码压缩包 可下载到本地后上传至Linux服务器也可以复制下载地址 span
  • VirtualBox 重要的配置文件和配置程序

    之前我安装了VirtualBox 来跑 Linux 虚拟机 开始设置的时候设了 4G 内存 结果后来不能改了 造成的结果就是一开机 内存飙到40 一开VirtualBox 直接飙到95 然后开个数据库开个EBS 服务 基本就不用干其他事情了

随机推荐

  • 错误 This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck~~~

    This inspection detects names that should resolve but don 39 t Due to dynamic dispatch and duck typing this is possible
  • LAMP源码安装图文详解(超详细)

    文章目录 一 LAMP架构概述1 各组件的主要作用2 各组件安装顺序 二 编译安装Apache httpd服务1 关闭防火墙 xff0c 将安装Apache所需软件包传到 opt目录下2 安装环境依赖包3 配置软件模块4 编译及安装5 优化
  • MySQL数据备份恢复全攻略,让我们通过简单几步找回丢失的数据

    编写初衷 没有人生下来天生就是会计算机的 xff0c 就拿笔者来说的话 xff0c 也是从Windows gt Centos gt Ubuntu一步一步慢慢学习 xff0c 积累下来的 为了让大家能够更快更高效率的学习 xff0c 从今天开
  • 解决/usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.29‘ not found问题

    1 问题分析 网上有分析说调用的高版本的gcc xff0c 生成的动态库没有替换老版本gcc的动态库导致 因此需要把高版本的so文件复制到低版本的so文件目录下 如下分析 xff1a https www jianshu com p 6424
  • 开源linux远程终端的源码

    C 有很多开源的Linux远程终端源代码可供使用 以下是一些常见的开源项目 xff1a MobaXterm xff1a 这是一个用C 编写的远程终端和X11服务器 xff0c 它支持SSH Telnet RDP VNC和SFTP等协议 Mo
  • 机器人操作系统(ROS)

    机器人操作系统 xff08 Robot Operating System xff0c ROS xff09 是一款广泛应用于机器人领域的开源操作系统 它最初由斯坦福大学人工智能实验室 xff08 Stanford AI Lab xff09 开
  • Ubuntu下运行文件

    在ubuntu下运行 run文件 a 打开一个终端 ctrl 43 alt 43 t b cd 到 run文件所在目录 c 输入 34 chmod 43 x foo run 34 d 输入 34 foo run 34 run文件就会开始运行
  • 必须掌握的8个dos命令

    一 xff0c ping 它是用来检查网络是否通畅或者网络连接速度的命令 作为一个生活在网络上的管理员或者黑客来说 xff0c ping命令 是第一个必须掌握的DOS命令 xff0c 它所利用的原理是这样的 xff1a 网络上的机器都有唯一
  • 搭建redis集群的时候碰到的错误

    1 出现from usr lib ruby site ruby 1 8 rubygems custom require rb 31 in 96 require from redis trib rb 2 错误 在使用ruby进行搭建redis
  • 疫情分析项目

    疫情期间各类政府媒体及社交网站 xff0c 均发布了相关疫情每日统计数据 xff0c 下面基于数据仓库工具Hive请你统计分析相关疫情数据 数据字段为 xff1a 日期 省份 城市 新增确诊 新增出院 新增死亡 消息来源 来源1 xff0c
  • selenium之css元素定位方法

    一 单一属性定位 xff08 id用 xff0c class用 xff09 1 xff1a type selector driver find element by css selector 39 input 39 2 xff1a id 定
  • 应用宝YSDK的快速接入(单机版只包含了游客模式没有加入其他的功能)

    可复制的代码 xff1a mainfest lt activity android name 61 34 com tencent tauth AuthActivity 34 android noHistory 61 34 true 34 a
  • Linux中error while loading shared libraries错误解决办法

    转载自http www cnblogs com codingmengmeng p 7456539 html 默认情况下 xff0c 编译器只会使用 lib和 usr lib这两个目录下的库文件 xff0c 通常通过源码包进行安装时 xff0
  • Java:常用类

    文章目录 一 Object类1 概述1 hashcode xff08 xff09 2 toString xff08 xff09 3 clone xff08 xff09 4 getClass xff08 xff09 5 notify xff0
  • JavaWeb介绍

    文章目录 1 基本概念1 1介绍1 2 Web应用程序1 静态web2 动态web 2 Web服务器2 1 技术介绍1 ASP2 PHP3 JSP Servlet 2 2 服务器介绍1 IIS2 Tomcat 3 Tomcat3 1 安装3
  • 邮件发送原理及实现

    文章目录 一 邮件发送原理1 1 接收发送过程1 2 邮件服务器1 3 邮件传输协议 二 Java邮件发送2 1 准备环境2 2 介绍2 2 1 授权码 2 3 简单邮件2 3 1 引入2 3 2 步骤一 xff1a 准备参数2 3 3 步
  • 11、MyBatis的逆向工程

    文章目录 11 MyBatis的逆向工程11 1 创建逆向工程的步骤1 添加依赖和插件2 创建mybatis config xml的核心配置文件3 创建逆向工程的配置文件4 执行MBG插件的generate目标5 效果6 窜库问题 11 2
  • 20、单元测试

    文章目录 1 JUnit5 的变化2 JUnit5常用注解3 断言 xff08 assertions xff09 1 简单断言2 数组断言3 组合断言4 异常断言5 超时断言6 快速失败 4 前置条件 xff08 assumptions x
  • 使用模拟器发送短信出现错误的解决方法

    在安卓应用开发揭秘第四章讲解使用模拟器发送短信并使用Toast显示短信息的时候 xff0c 本人遇到过如下错误 xff0c 现有一点个人的浅显理解 xff1a 错误 xff1a Couldn 39 t open fd for content
  • 22、原理解析

    文章目录 1 Profile功能1 application profile功能2 64 Profile条件装配功能3 profile分组 2 外部化配置1 外部配置源2 配置文件查找位置3 配置文件加载顺序 xff1a 4 指定环境优先 x