日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot学习笔记-01-springboot-helloworld的编写以及原理初步了解(自动装配)

發(fā)布時(shí)間:2024/7/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)成功!!

  • springboot在啟動(dòng)的時(shí)候,在類路徑下 /META-INF/spring.factories 獲取指定的值
  • 將這些自動(dòng)配置的類導(dǎo)入容器,自動(dòng)配置就會(huì)生效,幫我們進(jìn)行自動(dòng)配置
  • 以前我們需要自動(dòng)配置的東西,現(xiàn)在springboot幫我們做了!
  • 整合javaEE,解決方案和自動(dòng)配置的東西都在spring-boot-autoconfigure:2.2.4.RELEASE.jar中
  • 他會(huì)把所有需要導(dǎo)入的組件,以類名的方式返回,這些組件就會(huì)被添加到容器中
  • 容器中也會(huì)存在非常多的XXXAutoConfiguration文件(@),就是這些文件給容器中導(dǎo)入了這個(gè)場(chǎng)景需要的所有組件;并自動(dòng)配置,@Configuration,JavaConfig!
  • 有了自動(dòng)配置類,免去了我們手動(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)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。