spring boot 2 - 数据访问、单元测试、指标监控、原理解析

参考自尚硅谷语雀spring boot2教程文档

一、数据访问

一、连接MySql

1、数据源的自动配置-HikariDataSource

1.1导入JDBC场景

在这里插入图片描述
在这里插入图片描述
指定MySql版本,才会自动导入驱动
在这里插入图片描述

2、自动配置原理

2.1.自动配置的类
  • DataSourceAutoConfiguration : 数据源的自动配置
  • 修改数据源相关的配置:spring.datasource
  • 数据库连接池的配置,是自己容器中没有DataSource才自动配置的
  • 底层配置好的连接池是:HikariDataSource
@Configuration(proxyBeanMethods = false)
@Conditional(PooledDataSourceCondition.class)
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
@Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
		DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.OracleUcp.class,
		DataSourceConfiguration.Generic.class, DataSourceJmxConfiguration.class })
protected static class PooledDataSourceConfiguration
2.2自定义配置

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

2.3.编译测试类

在这里插入图片描述

2.4.测试结果

在这里插入图片描述
在这里插入图片描述
三条数据,查询正确

二、整合Driud数据源

Druid官方github地址

1、创建数据源

1.1引入starter启动器配置

在这里插入图片描述

1.2yaml配置文件配置

在这里插入图片描述

1.3测试

在这里插入图片描述

三、整合MyBatis

在这里插入图片描述

二、单元测试

三、指标监控

四、原理解析

1、Profile功能

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

1.1.application-profile功能

  • 默认配置文件 application.yaml;任何时候都会加载

  • 指定环境配置文件 application-{env}.yaml

  • 激活指定环境
    1.配置文件激活
    2.命令行激活:java -jar xxx.jar --spring.profiles.active=prod --person.name=haha
    修改配置文件的任意值,命令行优先

  • 默认配置与环境配置同时生效

  • 同名配置项,profile配置优先

1.2、@Profile条件装配功能

hhhh

1.3.profile分组

在这里插入图片描述

2、外部化配置

spring官方文档

  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
  11. in an environment variable or system property).
  12. Command line arguments.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.

2.1.外部配置源

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

2.2.配置文件查找位置

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

2.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

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

3、自定义starter

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

4、SpringBoot原理

Spring原理【Spring注解】、SpringMVC原理、自动配置原理、SpringBoot原理
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

之墨_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值