關(guān)于@SpringBootApplication與@SpringBootTest的區(qū)別及用法
@SpringBootApplication與@SpringBootTest區(qū)別用法
1 @SpringBootApplication 注解的應用
一般情況我們使用 @SpringBootApplication 注解來啟動 SpringBoot 項目
它其實只相當于 @Configuration、@EnableAutoConfiguration、@ComponentScan(包含了兩個filter)
@SpringBootApplication
public class FrameworkUnitRealTestApp {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(FrameworkUnitRealTestApp.class, args);
? ? }
}2 @SpringBootTest 注解的應用
一般情況我們使用 @SpringBootTest 和 @RunWith(SpringRunner.class) 注解來啟動 SpringBoot 測試項目
@RunWith(SpringRunner.class)?
@SpringBootTest
public class FrameworkUnitRealTestApp {
? ? @Test
? ? public void test() {}
}3 @SpringBootApplication 和 @SpringBootTest 的區(qū)別
這兩個注解的區(qū)別的核心在于兩個注解:@EnableAutoConfiguration、@ComponentScan(包含了兩個filter)
@EnableAutoConfiguration 啟動了所有的自動配置類
@ComponentScan(包含了兩個filter):在掃描階段過濾掉 @TestComponent 等專屬于測試的類和過濾掉被 @Configuration 注解的自動配置類(使得自動配置類不會在掃描階段就被注冊 beanDefinition,因為 自動配置類的優(yōu)先級應該是最低的)
可以看出 @SpringBootTest 并沒有啟用任何自動配置類,所以就不需要加 AutoConfigurationExcludeFilter 了
springboot 通過引入 @Test** 注解來在 測試環(huán)境下 引入不同的自動配置類!
4 @ComponentScan(包含了兩個filter) 解析
詳細的代碼如下:添加了 TypeExcludeFilter 和 AutoConfigurationExcludeFilter 兩個 excludeFilter
作用:掃描包的時候過濾掉被這兩個 Filter 匹配的類!
@ComponentScan(excludeFilters = {
?? ??? ?@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
?? ??? ?@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })4.1 TypeExcludeFilter 解析
主要移除測試相關(guān)的類
public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware {
? ?@Override
? ?public boolean match(MetadataReader metadataReader,
? ? ? ? ?MetadataReaderFactory metadataReaderFactory) throws IOException {
? ? ? if (this.beanFactory instanceof ListableBeanFactory
? ? ? ? ? ? && getClass() == TypeExcludeFilter.class) {
? ? ? ? ?Collection<TypeExcludeFilter> delegates = ((ListableBeanFactory) this.beanFactory)
? ? ? ? ? ? ? ?.getBeansOfType(TypeExcludeFilter.class).values();
? ? ? ? ?for (TypeExcludeFilter delegate : delegates) {
? ? ? ? ? ? if (delegate.match(metadataReader, metadataReaderFactory)) {
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ? }
? ? ? ? ?}
? ? ? }
? ? ? return false;
? ?}
}
//delegate.match 走這個類的 match 方法
class TestTypeExcludeFilter extends TypeExcludeFilter {
?? ?private static final String[] CLASS_ANNOTATIONS = { "org.junit.runner.RunWith",
?? ??? ??? ?"org.junit.jupiter.api.extension.ExtendWith" };
?? ?private static final String[] METHOD_ANNOTATIONS = { "org.junit.Test",
?? ??? ??? ?"org.junit.platform.commons.annotation.Testable" };
?? ?@Override
?? ?public boolean match(MetadataReader metadataReader,
?? ??? ??? ?MetadataReaderFactory metadataReaderFactory) throws IOException {
? ? ? ? //是否被 @TestComponent 及其父注解注釋
?? ??? ?if (isTestConfiguration(metadataReader)) {return true;}
? ? ? ? //類上或類中方法上有沒有 CLASS_ANNOTATIONS、METHOD_ANNOTATIONS 中的注解
?? ??? ?if (isTestClass(metadataReader)) {return true;}
?? ??? ?String enclosing = metadataReader.getClassMetadata().getEnclosingClassName();
?? ??? ?if (enclosing != null) {
? ? ? ? ? ? //遞歸內(nèi)部類、父類
? ? ? ? ? ? if (match(metadataReaderFactory.getMetadataReader(enclosing),
? ? ? ? ? ? ? ? ? ? ? metadataReaderFactory)) {
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
?? ??? ?}
?? ??? ?return false;
?? ?}
}4.2 AutoConfigurationExcludeFilter 解析
主要移除被 @Configuration 修飾的 自動配置類
public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoaderAware {
?? ?@Override
?? ?public boolean match(MetadataReader metadataReader,
?? ??? ??? ?MetadataReaderFactory metadataReaderFactory) throws IOException {
? ? ? ? //如果被 @Configuration 注解,并且是 自動配置類就返回 true,即匹配成功?
? ? ? ? //注:被 @Component 等注解并不匹配
?? ??? ?return isConfiguration(metadataReader) && isAutoConfiguration(metadataReader);
?? ?}
}5 @EnableAutoConfiguration 注解解析
作用:啟用自動配置類
@AutoConfigurationPackage
//啟用 AutoConfigurationImportSelector 配置類:掃描得到所有自動配置類
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
? ?String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
? ?//定義不啟用的 自動配置類
? ?Class<?>[] exclude() default {};
? ?//同上
? ?String[] excludeName() default {};
}
//這個注解主要是向容器中注冊 AutoConfigurationPackages.Registrar 類用來存儲自動配置包
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {}
//關(guān)鍵:這個類繼承了 DeferredImportSelector 接口,所以是到最后才解析的!!
public class AutoConfigurationImportSelector implements DeferredImportSelector{
? ? @Override
? ? public String[] selectImports(AnnotationMetadata annotationMetadata) {
? ? ? ? if (!isEnabled(annotationMetadata)) {
? ? ? ? ? ? return NO_IMPORTS;
? ? ? ? }
? ? ? ? AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
? ? ? ? ? ? .loadMetadata(this.beanClassLoader);
? ? ? ? AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
? ? ? ? ? ? autoConfigurationMetadata, annotationMetadata);
? ? ? ? return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
? ? }
}6 @…Test 注解
Spring Boot 中文文檔 對每個 @…Test 注解導入的自動配置類做了詳細的說明
SpringBootTest對比SpringBootApplication
SpringBootTest 是測試使用類的注解,標志這個類是測試用例。
具體看下源碼分析
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ExtendWith({SpringExtension.class})
public @interface SpringBootTest {
@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 {
對比顯示都是復合注解,并且前四個注解是一樣的,后面區(qū)分BootstrapWith和ExtendWith這兩個是測試中包含的
BootstrapWith這個注解中有一個參數(shù)為SpringBootTestContextBootstrapper
具體可以看下里面是什么

這里面申明了一些程序運行所在包的路徑,在去查看繼承的頂級類可以追溯到TestContextBootstrapper 這個接口 :

從里面的方法可以看到是在運行的時候設置上下文 以及如何獲取上下文,來提供測試啟動的必須值。
接下來看下 ExtendWith 這個注解類

這個主要看里面的SpringExtension這個參數(shù)

可以看出這個實現(xiàn)了很多接口,來處理測試需要的各種通知處理,以及在測試接口時可以提前處理請求參數(shù)。
SpringBootApplication中的復合注解則是掃描一些包和配置。雖然測試也是項目啟動的一種,可以看到里面實現(xiàn)還是有些區(qū)別的。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis-plus+達夢數(shù)據(jù)庫實現(xiàn)自動生成代碼的示例
這篇文章主要介紹了MyBatis-plus+達夢數(shù)據(jù)庫實現(xiàn)自動生成代碼的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
JAVA基于數(shù)組實現(xiàn)的商品信息查詢功能示例
這篇文章主要介紹了JAVA基于數(shù)組實現(xiàn)的商品信息查詢功能,結(jié)合實例形式詳細分析了java使用數(shù)組存儲數(shù)據(jù)實現(xiàn)的商品信息查詢功能相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
IntelliJ IDEA遠程Debug Linux的Java程序,找問題不要只會看日志了(推薦)
這篇文章主要介紹了IntelliJ IDEA遠程Debug Linux的Java程序,找問題不要只會看日志了,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
spring boot 添加admin監(jiān)控的方法
這篇文章主要介紹了spring boot 添加admin監(jiān)控的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-02-02
springboot使用hibernate validation對參數(shù)校驗的實現(xiàn)方法
這篇文章主要介紹了spring-boot 使用hibernate validation對參數(shù)進行優(yōu)雅的校驗,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Java多線程程序中synchronized修飾方法的使用實例
synchronized關(guān)鍵字主要北用來進行線程同步,這里我們主要來演示Java多線程程序中synchronized修飾方法的使用實例,需要的朋友可以參考下:2016-06-06

