00.springboot 操作数据库的封装

参考https://gitee.com/owenwangwen/open-capacity-platform项目

 db-spring-boot-starter

现在我们将采用springboot 标准starter的做法,重构项目基础组件。

  • 工具类00.springboot 操作数据库的封装
  • AOP切换数据源类00.springboot 操作数据库的封装
  • 多数据源定义core log00.springboot 操作数据库的封装

user-center代码

  • user-center pom文件使用00.springboot 操作数据库的封装
  • user-center application.yml00.springboot 操作数据库的封装

咱们想想,在不同项目中,咱们的项目是如何装配这些对象的吗?下面咱们需要揭密。

  • db-spring-boot-starter 中定义了spring.factories文件00.springboot 操作数据库的封装
  • DataSourceAutoConfig 中@Import(DataSourceAOP.class)00.springboot 操作数据库的封装

那么这些文件是如何完成加载到spring容器的呢?
此时,咱们必须回到user-center,阅读源码

  • @SpringBootApplication00.springboot 操作数据库的封装
  • @EnableAutoConfiguration00.springboot 操作数据库的封装
  • AutoConfigurationImportSelector00.springboot 操作数据库的封装

阅读到这里,我们了解到,user-center在启动时,由于@SpringBootApplication是复合主机,包含@EnableAutoConfiguration,这个类中@import了核心处理类AutoConfigurationImportSelector,这个类的核心就是将classpath中搜索所有META-INF/spring.factories配置文件
,并且将其中org.springframework.boot.autoconfigure.EnableAutoConfiguration key对应的配置项加载到spring容器

总结回顾

内部实现的关键点有

  • 1:ImportSelector 该接口的方法的返回值都会被纳入到spring容器管理中
  • 2:SpringFactoriesLoader 该类可以从classpath中搜索所有META-INF/spring.factories配置文件,并读取配置