太卷了!現在面試上來就問SpringBoot源碼實作,CRUD工程師能招架得住嗎今晚這節SpringBoot直播課千萬不能錯過,聽完就能掌握SpringBoot源碼,征服面試官
demo代碼:
https://gitee.com/xqh_1_0/spring-boot.git
1、POM.xml檔案
1、父項目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
這個父項目spring-boot-starter-parent又依賴一個父項目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
下面有個屬性,定義了對應的版本号
<properties>
<activemq.version>5.15.3</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.63</appengine-sdk.version>
<artemis.version>2.4.0</artemis.version>
<aspectj.version>1.8.13</aspectj.version>
<assertj.version>3.9.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
。。。。。。。
Spring Boot的版本仲裁中心 會自動導入對應的版本,不需要我們自己導入依賴,沒有dependencies裡面管理的依賴自己聲明
3、啟動器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
**spring-boot-starter-web:**幫我們導入web子產品正常運作所依賴的元件
spring boot将所有的功能場景都抽取出來,做成一個個的starter(啟動器),隻需要在項目裡引入這些starter相關場景的所有依賴都會被導入進來,要用什麼功能就導入什麼場景的啟動器。
4、主程式入口
@SpringBootApplication
public class SpringBoot01HelloQuickApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01HelloQuickApplication.class, args);
}
}
@SpringBootApplication: 說明這個類是SpringBoot的主配置類,SpringBoot就應該運作這個類的main方法來啟動應用
進入SpringBootApplication注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@SpringBootConfiguration:SpringBoot的配置類: 标準在某個類上,表示這是一個SpringBoot的配置類
@Configuration:配置類上,來标注這個注解; 配置類 ---- 配置檔案,也是容器中的一個元件(@Component) @EnableAutoConfiguration:開啟自動配置功能 以前需要自動配置的東西,Spring Boot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啟自動 配置功能;這樣自動配置才能生效。
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:自動配置包 @Import({Registrar.class}):底層注解,給容器導入元件; 将主配置類(@SpringBootApplication标注的類)的所在包及下面所有的子包裡面的所有元件掃描到Spring容器;
@Import({AutoConfigurationImportSelector.class}): 給容器導入元件?
AutoConfigurationImportSelector:導入元件選擇器
将所有需要導入的元件以及全類名的方式傳回;這些元件将以字元串數組 String[] 添加到容器中;
會給容器非常多的自動配置類,(xxxAutoConfiguration);就是給容器中導入這個場景需要的所有元件,并配置 好這些元件。
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
List<String> configurations =
SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(),
this.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;
}
SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Spring Boot在啟動的時候從類路徑下的META-INF/spring.factorys中擷取的EnableAutoConfiguration指定的值;
将這些值作為自動配置類導入到容器中,自動配置就生效了。
J2EE的整體解決方案
org\springframework\boot\spring-boot-autoconfigure\2.0.1.RELEASE\spring-boot-autoconfigure-2.0.1.RELEASE.jar
5、使用Spring Initializer建立一個快速向導
1.IDE支援使用Spring Initializer
自己選擇需要的元件:例如web
預設生成的SpringBoot項目
- 主程式已經生成好了,我們隻需要完成我們的邏輯
- resources檔案夾目錄結構
- static:儲存所有的靜态檔案;js css images
- templates:儲存所有的模闆頁面;(Spring Boot預設jar包使用嵌入式的Tomcat,預設不支援JSP);可
- 以使用模闆引擎(freemarker.thymeleaf);
- application.properties:Spring Boot的預設配置,例如 server.port=9000