以前的文章中進行過springboot的一些使用,再說一說一些增強的內容.java
咱們都知道springboot十分強大,能夠實現零配置/少配置運行,以及開箱即用的特性,那麼他是怎麼作到的呢?web
當咱們建立一個springboot項目,並只在建立時導入spring web依賴時能夠看到pom.xml中有什麼配置:spring
<?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> <groupId>com.jt</groupId> <artifactId>springboot_demo1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_demo1</name> <description>入門案例</description> <!--parent標籤做用: 定義了SpringBoot中全部關聯項目的版本號信息.--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> <!--項目打包時,跳過測試類打包--> <skipTests>true</skipTests> </properties> <!--開箱即用:SpringBoot項目只須要引入少許的jar包及配置,便可擁有其功能. spring-boot-starter 擁有開箱即用的能力. maven項目中依賴具備傳遞性. A 依賴 B 依賴 C項目 導入A bc會自動依賴 --> <dependencies> <!--直接依賴web springMVC配置--> <dependency> <groupId>org.springframework.boot</groupId> <!--springBoot-start SpringBoot啓動項 --> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--SpringBoot重構了測試方式 能夠在測試類中 直接引入依賴對象--> <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,則程序發佈時否則會報 項目中沒有main方法. --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
pom.xml文件內標籤的含義在上述代碼中有所註釋.apache
開箱即用:指項目中只須要注入少許的jar包及配置,就可擁有其功能.springboot
就是上述的pom.xml中的spring-starter啓動項擁有開箱即用的能力.maven
上圖是開箱即用特性的一個梳理,接下來講一下:
首先啓動類會執行spring-boot
SpringApplication.run(SpringbootDemo01Application.class, args);
run方法會加載@SpringBootApplication註解:測試
@Target(ElementType.TYPE)//註解對類有效 @Retention(RetentionPolicy.RUNTIME)//在運行期有效 @Documented//動態生成文檔信息 @Inherited//能夠被繼承
2.加載對象,但要排除的過濾器ui
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
3.springboot配置類spa
@SpringBootConfiguration又由@Configuration配置類以及元註解修飾
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration {}
意味着主啓動類自己就是一個超大的配置文件,能夠去加載其餘@Configuration註解描述的類,當啓動類加載時,其餘類都會加載.
4.完成開箱即用配置
@EnableAutoConfiguration由元註解以及@AutoConfigurationPackage和@Import(AutoConfigurationImportSelector.class)修飾
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {}
@AutoConfigurationPackage--自動按照包掃描的方式實例化對象,以後全部的配置都須要在啓動類所在包以及子孫包中進行定義.
@Import(AutoConfigurationImportSelector.class)--當程序啓動時,根據SpringBoot中的Seletor選擇器,去檢查pom.xml文件中是否添加了啓動項的包.