Spring Boot(深入剖析Spring Boot的pom.xml和主啓動類的源碼)

Spring Boot

在我們使用IDEA新建一個Spring Boot項目時會發現自動生成的pom.xml,今天深入剖析一下它的源碼:

pom.xml

pom.xml的代碼:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.chen</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

發現它的主體有 <parent> </parent> (父依賴)和許多的 spring-boot-starter-xxx (場景啓動器),而父依賴本質也是一個啓動器,點進父依賴的 spring-boot-starter-parent 源碼看看。

  • spring-boot-starter-parent
    源碼主體由配置的資源過濾管理插件組成:
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
  
<!-- 資源過濾:Spring Boot推薦使用.yml配置 -->
   <includes>
      <include>**/application*.yml</include>
      <include>**/application*.yaml</include>
      <include>**/application*.properties</include>
    </includes>
<!-- 插件管理 -->    
    <pluginManagement>
    ...
    ...
    ...
    </pluginManagement>

發現 spring-boot-starter-parent 的源碼裏面還有一個重要的依賴管理 spring-boot-dependencies,我們再點開 spring-boot-dependencies 的源碼看看。

  • spring-boot-dependencies
    源碼裏面是各種常用的依賴,所以Spring Boot可以自動幫我們配置依賴,解決了版本不兼容問題:
	<properties>
	...
	...
	</properties>
	
	<dependency>
      ...
      ...
    </dependency>

主啓動類

在我們使用IDEA新建一個Spring Boot項目時會發現自動生成的主啓動類,簡單幾行代碼,卻有很強大的作用,今天深入剖析一下它的源碼。

@SpringBootApplication
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootWeb01Application.class, args);
    }
}

剖析主啓動類就是剖析這兩個:

  • 主類構造器 SpringApplication
  • 註解 @SpringBootApplication
    @SpringBootApplication 的源碼裏面這四個元註解我們不做剖析
@Target:用於描述註解的使用範圍(:被描述的註解可以用在什麼地方)
@Retention:表示需要在什麼級別保存該註釋信息 , 用於描述註解的生命週期(SOURCE < CLASS < RUNTIME)
@Document:說明該註解將被包含在javadoc中
@Inherited:說明子類可以繼承父類中的該註解

瞭解源碼還需要了解一個知識點:JavaConfig
傳統的配置文件:(applicationContext.xml)

<bean  id="" class="" />
<import re />
<context:  component-scan />

再Spring4以後推薦使用JavaConfig編寫配置文件:

@Configuration
@Import(OtherConfig.class)  //引入其他配置
@ComponentScan("com.chen.service")  //掃描包
class MyConfig{

	@Bean  //註冊Bean (id = "user", class = User) 
	public User user(){
		return new user();
	}
}

上面兩個等價,所以在Spring Boot 中沒有配置文件!

思維導圖剖析:
在這裏插入圖片描述

//下篇再見…謝謝
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章