SpringCloud之SpringBoot学习
SpringBoot是什么?
1、基于spring开发,简化Spring配置,约定大于配置
微服务架构
单体应用,一个应用能做所有的事==>将应用拆分成一个个模块
项目搭建
1、官网生成
2、IDEA构建
参考例子:https://blog.csdn.net/baidu_39298625/article/details/98102453
自动装配原理
1、pom.xml
- spring-boot-dependencies: 核心依赖
- 无需指定版本,springboot的maven管理了版本仓库
2、spring-boot-XXX-starter
3、@SpringBootAppLication - @SpringBootConfiguration SpringBoot配置
@Configuration spring配置类
@Component - @EnableAutoConfiguration 自动配置
@AutoConfigurationPackage 自动配置包
@Import(AutoConfigurationPackages.Registrar.class) 自动配置 包注册
@Import(AutoConfigurationImportSelector.class) 自动配置 导入选择
获取所有配置
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
Assert.notEmpty(configurations,
"No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
//获取启动类下所有的配置
protected Class<?> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}
META-INF/spring.factories
FACTORIES_RESOURCE_LOCATION
//所有资源加载到配置类中
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
@ConditionalOnXXX 扫描到spring.factories文件中所有资源,但没生效的关键条件注解
结论:springboot所有的自动配置在启动时扫描并加载,spring.factories所有的自动配置类都在这里,但是不一定生效,要判断条件是否成立,只要导入对应的starter启动器,该自动配置才能生效
https://blog.csdn.net/qq_33369905/article/details/106647298?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161693957016780274171605%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=161693957016780274171605&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_v2~rank_v29-4-106647298.pc_v2_rank_blog_default&utm_term=springboot