springboot学习笔记-01-springboot-helloworld的编写以及原理初步了解(自动装配)
生活随笔
收集整理的這篇文章主要介紹了
springboot学习笔记-01-springboot-helloworld的编写以及原理初步了解(自动装配)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 原理初探
- 主程序
- 關(guān)于spring boot,談?wù)勀愕睦斫?#xff1a;
微服務(wù)階段
原理初探
pom.xml
- spring-boot-dependencies:核心依賴在父工程中!
- 我們?cè)趯懟蛘咭胍恍﹕pringboot依賴的時(shí)候,不需要指定版本,就因?yàn)橛羞@些版本倉庫
啟動(dòng)器
- <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.9.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent>進(jìn)入父項(xiàng)目<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.1.9.RELEASE</version><relativePath>../../spring-boot-dependencies</relativePath> </parent>
- 啟動(dòng)器:說白了就是Springboot的啟動(dòng)場(chǎng)景
- 比如spring-boot-starter-web,他就會(huì)幫我們自動(dòng)導(dǎo)入web環(huán)境所有的依賴!
- springboot會(huì)將所有場(chǎng)景都變成一個(gè)個(gè)啟動(dòng)器
- 我們要是用什么功能,就只需要找到對(duì)應(yīng)的啟動(dòng)器就可以了 ‘starter’
主程序
// @SpringBootApplication 標(biāo)注這個(gè)類是一個(gè)springboot的應(yīng)用 @SpringBootApplication public class HelloworldApplication {public static void main(String[] args) {SpringApplication.run(HelloworldApplication.class, args);} }- 注解
- @SpringBootConfiguration : springboot的配置@Configuration: spring 配置類@Component: 說明這也是一個(gè)spring的組件@EnableAutoConfiguration : 自動(dòng)配置@AutoConfigurationPackage: 自動(dòng)配置包@Import({AutoConfiguration.Registrar.class}): 自動(dòng)配置‘包注冊(cè)’@Import({AutoConfigurationImportSelector.class}): 自動(dòng)導(dǎo)入選擇// 獲取所有配置 List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes)
-
獲取候選的配置
-
META-INF/spring.factories: 自動(dòng)配置的核心文件
結(jié)論:springboot中所有的自動(dòng)配置都是在啟動(dòng)的時(shí)候掃描并加載,但是不一定生效,但是要判斷條件是否成立,只要導(dǎo)入了對(duì)應(yīng)的start,就有了對(duì)應(yīng)的啟動(dòng)器了,有了啟動(dòng)器,我們的自動(dòng)裝配就會(huì)生效,然后啟動(dòng)成功!!
關(guān)于spring boot,談?wù)勀愕睦斫?#xff1a;
- 自動(dòng)裝配
- run()
-
SpringApplication.run分析
分析該方法主要分兩部分,一部分是SpringApplication的實(shí)例化,二是run方法的執(zhí)行;
SpringApplication
這個(gè)類主要做了以下四件事情 - 推斷應(yīng)用的類型是普通的項(xiàng)目還是Web項(xiàng)目
- 查找并加載所有可用初始化器 , 設(shè)置到initializers屬性中
- 找出所有的應(yīng)用程序監(jiān)聽器,設(shè)置到listeners屬性中
- 推斷并設(shè)置main方法的定義類,找到運(yùn)行的主類
查看構(gòu)造器public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {this.sources = new LinkedHashSet();this.bannerMode = Mode.CONSOLE;this.logStartupInfo = true;this.addCommandLineProperties = true;this.addConversionService = true;this.headless = true;this.registerShutdownHook = true;this.additionalProfiles = new HashSet();this.isCustomEnvironment = false;this.resourceLoader = resourceLoader;Assert.notNull(primarySources, "PrimarySources must not be null");this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));this.webApplicationType = WebApplicationType.deduceFromClasspath();this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));this.mainApplicationClass = this.deduceMainApplicationClass(); }
run方法
-
全面接管SpringMVC的配置!!實(shí)操!!
總結(jié)
以上是生活随笔為你收集整理的springboot学习笔记-01-springboot-helloworld的编写以及原理初步了解(自动装配)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 预感
- 下一篇: sql server存储过程中解决单引号