欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于@SpringBootApplication與@SpringBootTest的區(qū)別及用法

 更新時間:2022年01月19日 08:48:03   作者:開心小蝸牛  
這篇文章主要介紹了關(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)文章

  • 淺談一下Java中枚舉的用法

    淺談一下Java中枚舉的用法

    這篇文章主要介紹了淺談一下Java中枚舉的用法,枚舉是一個被命名的整型常數(shù)的集合,用于聲明一組帶標識符的常數(shù),當一個變量有幾種固定可能的取值時,就可以將它定義為枚舉類型,需要的朋友可以參考下
    2023-04-04
  • MyBatis-plus+達夢數(shù)據(jù)庫實現(xiàn)自動生成代碼的示例

    MyBatis-plus+達夢數(shù)據(jù)庫實現(xiàn)自動生成代碼的示例

    這篇文章主要介紹了MyBatis-plus+達夢數(shù)據(jù)庫實現(xiàn)自動生成代碼的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • JAVA基于數(shù)組實現(xiàn)的商品信息查詢功能示例

    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程序,找問題不要只會看日志了(推薦)

    這篇文章主要介紹了IntelliJ IDEA遠程Debug Linux的Java程序,找問題不要只會看日志了,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • spring boot 添加admin監(jiān)控的方法

    spring boot 添加admin監(jiān)控的方法

    這篇文章主要介紹了spring boot 添加admin監(jiān)控的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-02-02
  • MDC在多線程中的使用方式

    MDC在多線程中的使用方式

    這篇文章主要介紹了MDC在多線程中的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • springboot使用hibernate validation對參數(shù)校驗的實現(xiàn)方法

    springboot使用hibernate validation對參數(shù)校驗的實現(xiàn)方法

    這篇文章主要介紹了spring-boot 使用hibernate validation對參數(shù)進行優(yōu)雅的校驗,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Java多線程程序中synchronized修飾方法的使用實例

    Java多線程程序中synchronized修飾方法的使用實例

    synchronized關(guān)鍵字主要北用來進行線程同步,這里我們主要來演示Java多線程程序中synchronized修飾方法的使用實例,需要的朋友可以參考下:
    2016-06-06
  • Mybatis的Mapper中的方法為什么不能重載

    Mybatis的Mapper中的方法為什么不能重載

    這篇文章主要介紹了Mybatis的Mapper中的方法為什么不能重載,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 新手了解java 異常處理基礎知識

    新手了解java 異常處理基礎知識

    Java中異常提供了一種識別及響應錯誤情況的一致性機制,有效地異常處理能使程序更加健壯、易于調(diào)試。那么這篇文章總結(jié)了Java有效處理異常的三個原則,有需要的朋友們可以參考借鑒
    2021-07-07

最新評論