SpringBoot 原始碼解析 (一)----- SpringBoot核心原理入門
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.
上面是引自官網的一段話,大概是說: Spring Boot 是所有基於 Spring 開發的專案的起點。Spring Boot 的設計是為了讓你儘可能快的跑起來 Spring 應用程式並且儘可能減少你的配置檔案。
什麼是 Spring Boot
- 它使用 “習慣優於配置” (專案中存在大量的配置,此外還內建一個習慣性的配置,讓你無須手動配置)的理念讓你的專案快速執行起來。
- 它並不是什麼新的框架,而是預設配置了很多框架的使用方式,就像 Maven 整合了所有的 jar 包一樣,Spring Boot 整合了所有框架
使用 Spring Boot 有什麼好處
回顧我們之前的 SSM 專案,搭建過程還是比較繁瑣的,需要:
- 1)配置 web.xml,載入 spring 和 spring mvc
- 2)配置資料庫連線、配置日誌檔案
- 3)配置家在配置檔案的讀取,開啟註解
- 4)配置mapper檔案
- .....
而使用 Spring Boot 來開發專案則只需要非常少的幾個配置就可以搭建起來一個 Web 專案,並且利用 IDEA 可以自動生成生成
- 劃重點:簡單、快速、方便地搭建專案;對主流開發框架的無配置整合;極大提高了開發、部署效率。
Spring Boot HelloWorld
匯入依賴spring boot相關的依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <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> </project>
編寫主程式
/** * @SpringBootApplication來標註一個主程式類,說明這是一個SpringBoot應用 */ @SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { //Spring應用啟動 SpringApplication.run(HelloWorldMainApplication.class, args); } }
編寫Controller、Service
@RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "Hello world"; } }
執行主程式測試
使用maven打包命令將其打包成jar包後,直接使用命令:
java -jar xxx.jar
Hello World探究
POM檔案
父專案
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> </parent>
其父專案是
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.1.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
該父專案是真正管理Spring Boot應用裡面的所有依賴的版本:Spring Boot的版本仲裁中心,所以以後匯入的依賴預設是不需要版本號。如下
還有很多版本號沒有截圖出來
啟動器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
spring-boot-starter : spring boot場景啟動器;幫助匯入web模組正常執行所依賴的元件;
Spring Boot將所有的功能場景抽取出來,做成一個個的starter(啟動器),只需要在專案中引入這些starter,那麼相關的場景的所有依賴都會匯入進專案中。要用什麼功能就匯入什麼場景的啟動器。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency>
添加了 spring-boot-starter-web 依賴,會自動新增 Tomcat 和 Spring MVC 的依賴
spring-boot-starter-web中又引入了spring-boot-starter-tomcat
主程式類(主入口類)
@SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { //Spring應用啟動 SpringApplication.run(HelloWorldMainApplication.class, args); } }
@SpringBootApplication
- Spring Boot應用標註在某個類上,說明這個類是SpringBoot的主配置類,SpringBoot就應該執行這個類的main方法來啟動SpringBoot應用。
註解定義如下:
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {}
@SpringBootConfiguration
- Spring Boot的配置類
- 標註在某個類上,表示這是一個Spring Boot的配置類
註解定義如下:
@Configuration public @interface SpringBootConfiguration {}
其實就是一個Configuration配置類,意思是HelloWorldMainApplication最終會被註冊到Spring容器中
@EnableAutoConfiguration
- 開啟自動配置功能
- 以前使用Spring需要配置的資訊,Spring Boot幫助自動配置;
- @EnableAutoConfiguration通知SpringBoot開啟自動配置功能,這樣自動配置才能生效。
註解定義如下:
@AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {}
@AutoConfigurationPackage
- 自動配置包註解
@Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage {}@Import(AutoConfigurationPackages.Registrar.class):預設將主配置類(@SpringBootApplication)所在的包及其子包裡面的所有元件掃描到Spring容器中。如下
@Order(Ordered.HIGHEST_PRECEDENCE) static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports { @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { //預設將會掃描@SpringBootApplication標註的主配置類所在的包及其子包下所有元件 register(registry, new PackageImport(metadata).getPackageName()); } @Override public Set<Object> determineImports(AnnotationMetadata metadata) { return Collections.<Object>singleton(new PackageImport(metadata)); } }
@Import(EnableAutoConfigurationImportSelector.class)
EnableAutoConfigurationImportSelector: 匯入哪些元件的選擇器,將所有需要匯入的元件以全類名的方式返回,這些元件就會被新增到容器中。
1 //EnableAutoConfigurationImportSelector的父類:AutoConfigurationImportSelector 2 @Override 3 public String[] selectImports(AnnotationMetadata annotationMetadata) { 4 if (!isEnabled(annotationMetadata)) { 5 return NO_IMPORTS; 6 } 7 try { 8 AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader 9 .loadMetadata(this.beanClassLoader); 10 AnnotationAttributes attributes = getAttributes(annotationMetadata); 11 List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); 12 configurations = removeDuplicates(configurations); 13 configurations = sort(configurations, autoConfigurationMetadata); 14 Set<String> exclusions = getExclusions(annotationMetadata, attributes); 15 checkExcludedClasses(configurations, exclusions); 16 configurations.removeAll(exclusions); 17 configurations = filter(configurations, autoConfigurationMetadata); 18 fireAutoConfigurationImportEvents(configurations, exclusions); 19 return configurations.toArray(new String[configurations.size()]); 20 } 21 catch (IOException ex) { 22 throw new IllegalStateException(ex); 23 } 24 }
我們主要看第11行List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
會給容器中注入眾多的自動配置類(xxxAutoConfiguration),就是給容器中匯入這個場景需要的所有元件,並配置好這些元件。我們跟進去看看
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> 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<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); try { //從類路徑的META-INF/spring.factories中載入所有預設的自動配置類 Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); List<String> result = new ArrayList<String>(); 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啟動的時候從類路徑下的 META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,並將這些值作為自動配置類匯入到容器中,自動配置類就會生效,最後完成自動配置工作。EnableAutoConfiguration預設在spring-boot-autoconfigure這個包中,如下圖
最終有96個自動配置類被載入並註冊進Spring容器中
J2EE的整體整合解決方案和自動配置都在spring-boot-autoconfigure-xxx.jar中。在這些自動配置類中會通過@ConditionalOnClass等條件註解判斷是否匯入了某些依賴包,從而通過@Bean註冊相應的物件進行自動配置。後面我們會有單獨文章講自動配置的內容