溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Springboot中TypeFilter的作用是什么

發(fā)布時間:2021-06-18 15:06:51 來源:億速云 閱讀:164 作者:Leah 欄目:大數據

本篇文章為大家展示了Springboot中TypeFilter的作用是什么,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

SpringBootApplication注解分析

Springboot中TypeFilter的作用是什么 從源代碼很容易看出來,它的作用就是自動裝配和掃描我們的包,并將符合的類進行注冊到容器。自動裝配非常簡單,這里不做過多分析,接下來分析一下什么叫做符合規(guī)則的類。在@ComponentScan注解上面的過濾器類型的定義

public enum FilterType {
    ANNOTATION, //注解類型
    ASSIGNABLE_TYPE, //指定的類型
    ASPECTJ, //按照Aspectj的表達式,基本上不會用到
    REGEX, //按照正則表達式
    CUSTOM; //自定義

    private FilterType() {
    }
}
excludeFilters排除過濾器

這個是給我們排除符合的類,不讓他注冊到IOC的時候使用的, Springboot默認使用兩個排除過濾器,很簡單的,網上隨便搜都可以找到相關說明,在這兒我舉個特舒列子就行了.

package com.github.dqqzj.springboot.filter;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author qinzhongjian
 * @date created in 2019-07-30 19:14
 * @description: TODO
 * @since JDK 1.8.0_212-b10
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Dqqzj {
    String value();
}
package com.github.dqqzj.springboot.filter;

import org.springframework.stereotype.Component;

/**
 * @author qinzhongjian
 * @date created in 2019-07-29 22:30
 * @description: TODO
 * @since JDK 1.8.0_212-b10
 */
@Dqqzj(value = "dqqzj")
@Component
public class Tt {
}
package com.github.dqqzj.springboot.filter;

import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

/**
 * @author qinzhongjian
 * @date created in 2019-07-30 19:13
 * @description: TODO
 * @since JDK 1.8.0_212-b10
 */
public class MyTypeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        if (metadataReader.getAnnotationMetadata().isAnnotated(Dqqzj.class.getName())) {
            return true;
        }
        return false;
    }
}

Springboot中TypeFilter的作用是什么 以上代碼是正常邏輯,反過來這樣想,如果將Tt類的@Component注解去掉是不是也行的,所以這種排除注解一般都用在正??梢宰⑷氲饺萜鞯臅r候進行添加的,那么我們上面說過,脫離Spring也可以注入到容器,該怎么實現(xiàn)呢?

includeFilters包含過濾器

脫離Spring原生注解,將將Tt類的@Component注解去掉

package com.github.dqqzj.springboot.filter;

import org.springframework.stereotype.Component;

/**
 * @author qinzhongjian
 * @date created in 2019-07-29 22:30
 * @description: TODO
 * @since JDK 1.8.0_212-b10
 */
@Dqqzj(value = "dqqzj")
//@Component
public class Tt {
}

Springboot中TypeFilter的作用是什么

透過現(xiàn)象看本質

流程進行梳理一下,注解驅動在注入容器的關鍵掃描類(注意這里是指的掃描,而不是什么@Bean,@Import等其余注解都是建立在這個基礎之上的)

  • ComponentScanAnnotationParser

  • ClassPathBeanDefinitionScanner

  • ClassPathScanningCandidateComponentProvider

ClassPathScanningCandidateComponentProvider#registerDefaultFilters
protected void registerDefaultFilters() {
        this.includeFilters.add(new AnnotationTypeFilter(Component.class));
        ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();

        try {
            this.includeFilters.add(new AnnotationTypeFilter(ClassUtils.forName("javax.annotation.ManagedBean", cl), false));
            this.logger.trace("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
        } catch (ClassNotFoundException var4) {
        }

        try {
            this.includeFilters.add(new AnnotationTypeFilter(ClassUtils.forName("javax.inject.Named", cl), false));
            this.logger.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
        } catch (ClassNotFoundException var3) {
        }

    }

上述內容就是Springboot中TypeFilter的作用是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI