1、場景啟動器
1.1依賴
<!--Hello World項目的父工程是org.springframework.boot--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.1.RELEASE</version><relativePath/></parent><!--org.springframework.boot他的父項目是spring-boot-dependencies他來真正管理Spring Boot應用里面的所有依賴版本;Spring Boot的版本仲裁中心;以后我們導入依賴默認是不需要寫版本;(沒有在dependencies里面管理的依賴自然需要聲明版本號)--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.2.1.RELEASE</version><relativePath>../../spring-boot-dependencies</relativePath></parent>
1.2、SpringBoot場景啟動器
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
? spring-boot-starter:spring-boot場景啟動器;幫我們導入了web模塊正常運行所依賴的組件;
Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啟動器),只需要在項目里面引入這些starter相關場景的所有依賴都會導入進來。要用什么功能就導入什么場景的啟動器.
2、自動配置
package com.lizhengi;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** springBootApplication:標注一個主程序類,表示這個是一個Springboot應用*/@SpringBootApplication
public class HelloWorldApplication {public static void main(String[] args) {//Spring應用啟動SpringApplication.run(HelloWorldApplication.class, args);}
}
@SpringBootApplication: Spring Boot應用標注在某個類上說明這個類是SpringBoot的主配置類,SpringBoot就應該運行這個類的main方法來啟動SpringBoot應用;
看一下@SpringBootApplication這個注解類的源碼
@Target({ElementType.TYPE}) //可以給一個類型進行注解,比如類、接口、枚舉
@Retention(RetentionPolicy.RUNTIME) //可以保留到程序運行的時候,它會被加載進入到 JVM 中
@Documented //將注解中的元素包含到 Javadoc 中去。
@Inherited //繼承,比如A類上有該注解,B類繼承A類,B類就也擁有該注解@SpringBootConfiguration@EnableAutoConfiguration/*
*創建一個配置類,在配置類上添加 @ComponentScan 注解。
*該注解默認會掃描該類所在的包下所有的配置類,相當于之前的 <context:component-scan>。
*/
@ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class}
), @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication
- @SpringBootConfiguration:Spring Boot的配置類;標注在某個類上,表示這是一個Spring
Boot的配置類; - @SpringBootConfiguration:Spring Boot的配置類;標注在某個類上,表示這是一個Spring
Boot的配置類; - @EnableAutoConfiguration:開啟自動配置功能:以前我們需要配置的東西,Spring Boot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啟自動配置功能;這樣自動配置才能生效;
- @AutoConfigurationPackage:自動配置包。