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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

SpringBoot 源码解析 (一)----- SpringBoot核心原理入门

發(fā)布時(shí)間:2025/3/21 javascript 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot 源码解析 (一)----- SpringBoot核心原理入门 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

Spring Boot 概述
什么是 Spring Boot
使用 Spring Boot 有什么好處
Spring Boot HelloWorld
導(dǎo)入依賴spring boot相關(guān)的依賴
編寫(xiě)主程序
編寫(xiě)Controller、Service
運(yùn)行主程序測(cè)試
Hello World探究
POM文件
啟動(dòng)器
主程序類(主入口類)

Spring Boot 概述
Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.

上面是引自官網(wǎng)的一段話,大概是說(shuō): Spring Boot 是所有基于 Spring 開(kāi)發(fā)的項(xiàng)目的起點(diǎn)。Spring Boot 的設(shè)計(jì)是為了讓你盡可能快的跑起來(lái) Spring 應(yīng)用程序并且盡可能減少你的配置文件。

什么是 Spring Boot
它使用 “習(xí)慣優(yōu)于配置” (項(xiàng)目中存在大量的配置,此外還內(nèi)置一個(gè)習(xí)慣性的配置,讓你無(wú)須手動(dòng)配置)的理念讓你的項(xiàng)目快速運(yùn)行起來(lái)。
它并不是什么新的框架,而是默認(rèn)配置了很多框架的使用方式,就像 Maven 整合了所有的 jar 包一樣,Spring Boot 整合了所有框架
使用 Spring Boot 有什么好處
回顧我們之前的 SSM 項(xiàng)目,搭建過(guò)程還是比較繁瑣的,需要:

1)配置 web.xml,加載 spring 和 spring mvc
2)配置數(shù)據(jù)庫(kù)連接、配置日志文件
3)配置家在配置文件的讀取,開(kāi)啟注解
4)配置mapper文件

而使用 Spring Boot 來(lái)開(kāi)發(fā)項(xiàng)目則只需要非常少的幾個(gè)配置就可以搭建起來(lái)一個(gè) Web 項(xiàng)目,并且利用 IDEA 可以自動(dòng)生成生成

劃重點(diǎn):簡(jiǎn)單、快速、方便地搭建項(xiàng)目;對(duì)主流開(kāi)發(fā)框架的無(wú)配置集成;極大提高了開(kāi)發(fā)、部署效率。
回到頂部
Spring Boot HelloWorld
導(dǎo)入依賴spring boot相關(guān)的依賴

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

<groupId>cn.chenhao</groupId> <artifactId>springboot</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging><name>springboot</name> <description>Demo project for Spring Boot</description> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version><relativePath/> </parent> <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> </dependencies> <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build> 復(fù)制代碼 編寫(xiě)主程序

/**

  • @SpringBootApplication來(lái)標(biāo)注一個(gè)主程序類,說(shuō)明這是一個(gè)SpringBoot應(yīng)用
    */
    @SpringBootApplication
    public class HelloWorldMainApplication {

    public static void main(String[] args) {
    //Spring應(yīng)用啟動(dòng)
    SpringApplication.run(HelloWorldMainApplication.class, args);
    }
    }

編寫(xiě)Controller、Service

@RestController
public class HelloController {

@RequestMapping("/hello") public String hello(){return "Hello world"; }

}

運(yùn)行主程序測(cè)試
使用maven打包命令將其打包成jar包后,直接使用命令:

java -jar xxx.jar
回到頂部
Hello World探究
POM文件
父項(xiàng)目

org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE 其父項(xiàng)目是 org.springframework.boot spring-boot-dependencies 2.0.1.RELEASE ../../spring-boot-dependencies

該父項(xiàng)目是真正管理Spring Boot應(yīng)用里面的所有依賴的版本:Spring Boot的版本仲裁中心,所以以后導(dǎo)入的依賴默認(rèn)是不需要版本號(hào)。如下

還有很多版本號(hào)沒(méi)有截圖出來(lái)

啟動(dòng)器

org.springframework.boot
spring-boot-starter-web

? spring-boot-starter : spring boot場(chǎng)景啟動(dòng)器;幫助導(dǎo)入web模塊正常運(yùn)行所依賴的組件;

? Spring Boot將所有的功能場(chǎng)景抽取出來(lái),做成一個(gè)個(gè)的starter(啟動(dòng)器),只需要在項(xiàng)目中引入這些starter,那么相關(guān)的場(chǎng)景的所有依賴都會(huì)導(dǎo)入進(jìn)項(xiàng)目中。要用什么功能就導(dǎo)入什么場(chǎng)景的啟動(dòng)器。

org.springframework.boot spring-boot-starter-tomcat org.springframework spring-web org.springframework spring-webmvc

添加了 spring-boot-starter-web 依賴,會(huì)自動(dòng)添加 Tomcat 和 Spring MVC 的依賴

spring-boot-starter-web中又引入了spring-boot-starter-tomcat

@SpringBootApplication
public class HelloWorldMainApplication {

public static void main(String[] args) {//Spring應(yīng)用啟動(dòng)SpringApplication.run(HelloWorldMainApplication.class, args); }

}

@SpringBootApplication

Spring Boot應(yīng)用標(biāo)注在某個(gè)類上,說(shuō)明這個(gè)類是SpringBoot的主配置類,SpringBoot就應(yīng)該運(yùn)行這個(gè)類的main方法來(lái)啟動(dòng)SpringBoot應(yīng)用。
注解定義如下:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {}

@SpringBootConfiguration

Spring Boot的配置類
標(biāo)注在某個(gè)類上,表示這是一個(gè)Spring Boot的配置類
注解定義如下:

@Configuration
public @interface SpringBootConfiguration {}
其實(shí)就是一個(gè)Configuration配置類,意思是HelloWorldMainApplication最終會(huì)被注冊(cè)到Spring容器中

@EnableAutoConfiguration
開(kāi)啟自動(dòng)配置功能
以前使用Spring需要配置的信息,Spring Boot幫助自動(dòng)配置;
@EnableAutoConfiguration通知SpringBoot開(kāi)啟自動(dòng)配置功能,這樣自動(dòng)配置才能生效。
注解定義如下:

@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
@AutoConfigurationPackage

自動(dòng)配置包注解
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {}
@Import(AutoConfigurationPackages.Registrar.class):默認(rèn)將主配置類(@SpringBootApplication)所在的包及其子包里面的所有組件掃描到Spring容器中。如下

@Order(Ordered.HIGHEST_PRECEDENCE)
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {

@Override public void registerBeanDefinitions(AnnotationMetadata metadata,BeanDefinitionRegistry registry) {//默認(rèn)將會(huì)掃描@SpringBootApplication標(biāo)注的主配置類所在的包及其子包下所有組件register(registry, new PackageImport(metadata).getPackageName()); }@Override public Set<Object> determineImports(AnnotationMetadata metadata) {return Collections.<Object>singleton(new PackageImport(metadata)); }

}

@Import(EnableAutoConfigurationImportSelector.class)

EnableAutoConfigurationImportSelector: 導(dǎo)入哪些組件的選擇器,將所有需要導(dǎo)入的組件以全類名的方式返回,這些組件就會(huì)被添加到容器中。
//EnableAutoConfigurationImportSelector的父類:AutoConfigurationImportSelector
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
try {
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List configurations = getCandidateConfigurations(annotationMetadata, attributes);
configurations = removeDuplicates(configurations);
configurations = sort(configurations, autoConfigurationMetadata);
Set exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
return configurations.toArray(new String[configurations.size()]);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}

我們主要看第11行List configurations = getCandidateConfigurations(annotationMetadata, attributes);會(huì)給容器中注入眾多的自動(dòng)配置類(xxxAutoConfiguration),就是給容器中導(dǎo)入這個(gè)場(chǎng)景需要的所有組件,并配置好這些組件。我們跟進(jìn)去看看

protected List getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
List configurations = SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
//…
return configurations;
}

protected Class<?> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}

public static final String FACTORIES_RESOURCE_LOCATION = “META-INF/spring.factories”;

public static List loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
try {
//從類路徑的META-INF/spring.factories中加載所有默認(rèn)的自動(dòng)配置類
Enumeration urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
List result = new ArrayList();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
//獲取EnableAutoConfiguration指定的所有值,也就是EnableAutoConfiguration.class的值
String factoryClassNames = properties.getProperty(factoryClassName);
result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
}
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException(“Unable to load [” + factoryClass.getName() + “] factories from location [” + FACTORIES_RESOURCE_LOCATION + “]”, ex);
}
}

SpringBoot啟動(dòng)的時(shí)候從類路徑下的 META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,并將這些值作為自動(dòng)配置類導(dǎo)入到容器中,自動(dòng)配置類就會(huì)生效,最后完成自動(dòng)配置工作。EnableAutoConfiguration默認(rèn)在spring-boot-autoconfigure這個(gè)包中,如下圖

最終有96個(gè)自動(dòng)配置類被加載并注冊(cè)進(jìn)Spring容器中

J2EE的整體整合解決方案和自動(dòng)配置都在spring-boot-autoconfigure-xxx.jar中。在這些自動(dòng)配置類中會(huì)通過(guò)@ConditionalOnClass等條件注解判斷是否導(dǎo)入了某些依賴包,從而通過(guò)@Bean注冊(cè)相應(yīng)的對(duì)象進(jìn)行自動(dòng)配置。后面我們會(huì)有單獨(dú)文章講自動(dòng)配置的內(nèi)容

總結(jié)

以上是生活随笔為你收集整理的SpringBoot 源码解析 (一)----- SpringBoot核心原理入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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

主站蜘蛛池模板: 日本三级不卡 | 美少妇av| 男女操操视频 | 色污网站 | 91本色| 中文字幕人成乱码熟女香港 | 亚洲色域网 | 国产欧美久久久久久 | 日韩大片免费观看 | 一区二区三区日韩视频 | 日韩激情成人 | 人妻91麻豆一区二区三区 | 在线观看香蕉视频 | 欧美乱码精品一区二区 | 香蕉人人精品 | 国产精品免费91 | 国产色综合网 | 欧美在线导航 | 毛片在线播放视频 | 免费观看亚洲视频 | 精品国产精品网麻豆系列 | 91成人高清 | 欧亚av| 夜夜夜操 | 岛国精品一区二区三区 | 九九热视频精品在线观看 | 亚洲av区无码字幕中文色 | 新天堂网 | 欧美激情3p | 亚洲自拍中文字幕 | 精品一区二区三区在线观看 | 日本天堂网在线观看 | 欧美在线v | 8x8ⅹ8成人免费视频观看 | 日韩高清专区 | 黄色片网战 | 成人黄色一级片 | 中文字幕少妇在线三级hd | h片在线观看免费 | 精品少妇人妻一区二区黑料社区 | 五月天欧美 | 亚洲视频在线一区 | 伊人影视在线 | 性久久久久| 亚洲综合精品国产 | 午夜小福利 | 国产高清免费 | 欧美日韩在线观看免费 | 国产精品成人3p一区二区三区 | 国产成人久久精品流白浆 | 久久影视中文字幕 | 中国女人内精69xxxxxx | 在线观看免费黄色 | 麻豆出品| 秋霞国产午夜精品免费视频 | 国产成a人亚洲精品 | 亚洲色域网 | 夜夜高潮夜夜爽国产伦精品 | 中文字幕免费播放 | 国产成人精品999在线观看 | 亚洲人成在线播放 | www.com久久 | 日韩中文字幕在线免费观看 | 黄色一级免费观看 | 播放毛片 | 亚洲毛片在线免费观看 | 99久久影院 | 91国在线观看 | 中文字幕一区二区三区免费看 | 欧美亚洲色综久久精品国产 | 精品乱子一区二区三区 | 久久伊 | 亚洲性生活视频 | 中文字幕永久在线 | 国产精选一区二区 | 亚洲一区二区黄片 | 狠狠a| 在线中出 | 久久激情综合网 | 亚洲区小说区图片区 | 国模在线 | 日韩欧美片| 国产婷婷久久 | 久久精品一二区 | 午夜丰满寂寞少妇精品 | 国产综合影院 | 在线视频网站 | 无码任你躁久久久久久久 | 日本aⅴ片 | 国内精品视频一区二区三区 | jizzjizz亚洲 | 日韩在线专区 | 短篇山村男同肉耽h | 天堂久久一区 | 我要看18毛片 | 91精品国产综合久久精品图片 | 曰批又黄又爽免费视频 | 免费看日韩av | 亚洲看看 |