Springboot starter 启动器项目原理

视频公开课:https://www.bilibili.com/video/av71299758
关联:mybatis-springboot-start 解读https://blog.csdn.net/shine_guo_star/article/details/103609173

  1. 约定优于配置:
    Springboot有一些常用约定,即使用一些默认值,旨在减少开发人员写配置文件的负担。但优于配置不是没有配置,必要的配置还是不能少的。在这种模式下,开发人员只要重写一些不符约定的配置和没有约定的配置就行了。
    Springboot starter 启动器项目原理

  2. Environment & @Value
    @Autowired
    Environment environment; //spring提供的一个工具,配置信息保存的地方(环境)
    @Value("${jdbc.url}")
    String url;

  3. springboot 新做法:@ConfigurationProperties
    @Value有个坏处,就是每个使用的地方都要写一次,会有硬编码
    springboot推荐使用一个xxxxProperties配置类,再使用@ConfigurationProperties自动装载属性。

Springboot starter 启动器项目原理
使用前缀映射属性
Springboot starter 启动器项目原理
最后在bean里使用xxxProperties
Springboot starter 启动器项目原理
4. xxxx-spring-boot-starter 一般这样开发(遵循如下规范)
1) 命名:thirdpartproject-springboot-starter
2) starter启动器没有代码,负责解决包依赖,引入各类jar包。
Springboot starter 启动器项目原理
3)core项目是真正实现功能的地方
Springboot starter 启动器项目原理
4)如何让项目能扫描到starter包下的bean?
普通方法:@SpringBootApplication(scanBasePackages = {“com.study”, “com.netease”})
注:com.study是新建工程的包名,com.netease是bean所在的core项目的包名

高级方法:遵循约定,通过spring.factories的SPI自动扫描

Springboot starter 启动器项目原理
Springboot starter 启动器项目原理