最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot項(xiàng)目實(shí)現(xiàn)將類從@ComponentScan中排除

 更新時(shí)間:2021年11月05日 10:17:57   作者:SenKnight  
這篇文章主要介紹了Springboot項(xiàng)目實(shí)現(xiàn)將類從@ComponentScan中排除,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

將類從@ComponentScan中排除

問題描述

最近在學(xué)習(xí)SpringCloud的Ribbon,在使用

@RibbonClient(name = "SPRINGCLOUD-P-DEPT", 
   configuration = RibbonConfig.class)

為服務(wù)指定負(fù)載均衡策略的時(shí)候,根據(jù)Ribbon官方文檔介紹,自定義的Ribbon配置類不允許被Springboot的**@ComponentScan**注解掃描到,所以需要將自定義的配置類RibbonConfig從在Springboot自動(dòng)注入的范圍內(nèi)排除

方案一

我們都知道,Springboot的**@SpringBootApplication**會(huì)自動(dòng)掃描本類所在包下的所有類和子類,所以只需要將RibbonConfig定義在Springboot啟動(dòng)類所在包外面即可

方案二

通過在啟動(dòng)類中添加

@ComponentScan(excludeFilters = @ComponentScan.Filter(
  type = FilterType.ASSIGNABLE_TYPE, 
  classes = RibbonConfig.class))

通過FilterType.ASSIGNABLE_TYPE來指定要排除的類

如果需要排除的類太多了這個(gè)就很麻煩

方案三

通過自定義注解實(shí)現(xiàn)

@ComponentScan(excludeFilters = @ComponentScan.Filter(
  type = FilterType.ANNOTATION, 
  classes = ScanIgnore.class))

與方案二不同的是,這里用的是FilterType.ANNOTATION

方案四

通過實(shí)現(xiàn)TypeFilter類來自定義過濾器

@ComponentScan(excludeFilters = { 
 @Filter(
  type = FilterType.CUSTOM, 
  classes = TypeExcludeFilter.class),
 @Filter(
  type = FilterType.CUSTOM, 
  classes = AutoConfigurationExcludeFilter.class) })

此處給出的就是**@SpringbootApplication中的實(shí)現(xiàn)方式,通過FilterType.CUSTOM**來根據(jù)自動(dòng)一過濾器來排除bean

最后貼出枚舉類FilterType:

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.context.annotation;
/**
 * Enumeration of the type filters that may be used in conjunction with
 * {@link ComponentScan @ComponentScan}.
 *
 * @author Mark Fisher
 * @author Juergen Hoeller
 * @author Chris Beams
 * @since 2.5
 * @see ComponentScan
 * @see ComponentScan#includeFilters()
 * @see ComponentScan#excludeFilters()
 * @see org.springframework.core.type.filter.TypeFilter
 */
public enum FilterType {
 /**
  * Filter candidates marked with a given annotation.
  * @see org.springframework.core.type.filter.AnnotationTypeFilter
  */
 ANNOTATION,
 /**
  * Filter candidates assignable to a given type.
  * @see org.springframework.core.type.filter.AssignableTypeFilter
  */
 ASSIGNABLE_TYPE,
 /**
  * Filter candidates matching a given AspectJ type pattern expression.
  * @see org.springframework.core.type.filter.AspectJTypeFilter
  */
 ASPECTJ,
 /**
  * Filter candidates matching a given regex pattern.
  * @see org.springframework.core.type.filter.RegexPatternTypeFilter
  */
 REGEX,
 /** Filter candidates using a given custom
  * {@link org.springframework.core.type.filter.TypeFilter} implementation.
  */
 CUSTOM
}

@ComponentScan 詳解

@ComponentScan 的作用就是根據(jù)定義的掃描路徑,把符合掃描規(guī)則的類裝配到spring容器中,注解定義如下。

 
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    @AliasFor("basePackages")
    String[] value() default {};
    @AliasFor("value")
    String[] basePackages() default {};
    Class<?>[] basePackageClasses() default {};
    Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
    Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
    ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;
    String resourcePattern() default "**/*.class";
    boolean useDefaultFilters() default true;
    ComponentScan.Filter[] includeFilters() default {};
    ComponentScan.Filter[] excludeFilters() default {};
    boolean lazyInit() default false;
    @Retention(RetentionPolicy.RUNTIME)
    @Target({})
    public @interface Filter {
        FilterType type() default FilterType.ANNOTATION;
        @AliasFor("classes")
        Class<?>[] value() default {};
        @AliasFor("value")
        Class<?>[] classes() default {};
        String[] pattern() default {};
    }
}
  • basePackagesvalue: 用于指定包的路徑,進(jìn)行掃描
  • basePackageClasses: 用于指定某個(gè)類的包的路徑進(jìn)行掃描
  • nameGenerator: bean的名稱的生成器
  • useDefaultFilters: 是否開啟對(duì)@Component,@Repository,@Service,@Controller的類進(jìn)行檢測(cè)
  • includeFilters: 包含的過濾條件

FilterType.ANNOTATION:按照注解過濾

FilterType.ASSIGNABLE_TYPE:按照給定的類型

FilterType.ASPECTJ:使用ASPECTJ表達(dá)式

FilterType.REGEX:正則

FilterType.CUSTOM:自定義規(guī)則

  • excludeFilters: 排除的過濾條件,用法和includeFilters一樣

我的工程結(jié)構(gòu)如下,測(cè)試對(duì)controller和service的掃描,其中HelloController沒有加@Controller等任何注解,就是一個(gè)普通類。

修改配置類如下:應(yīng)用默認(rèn)的過濾器,掃描service包:

@Configuration
@ComponentScan(value = "com.xhx.spring.service",
        useDefaultFilters = true
)
public class MyConfig {
}

系統(tǒng)注入了兩個(gè)service進(jìn)去

改成如下所示:HelloController所在的包的類也被掃描了進(jìn)去

@Configuration
@ComponentScan(value = "com.xhx.spring.service",
        useDefaultFilters = true,
        basePackageClasses = HelloController.class
)
public class MyConfig {
}

系統(tǒng)中會(huì)注入下面就給類

把默認(rèn)的過濾器關(guān)掉,掃描帶Controller注解的。

@Configuration
@ComponentScan(value = "com.xhx.spring",
        useDefaultFilters = false,
        includeFilters = {
            @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
        }
)
public class MyConfig {
}

按照類的類型掃描,雖然HelloController沒有加注解,但是被注入到了spring容器中

@Configuration
@ComponentScan(value = "com.xhx.spring",
        useDefaultFilters = false,
        includeFilters = {
            @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {HelloController.class})
        }
)
public class MyConfig {
}

自定義掃描過濾器

package com.xhx.spring.componentscan.config; 
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;
 
/**
 * xuhaixing
 * 2018/9/18 23:07
 **/
public class MyTypeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        String className = metadataReader.getClassMetadata().getClassName();
        if(className.contains("Controller")){
            return true;
        }
        return false;
    }
}

修改配置類

@Configuration
@ComponentScan(value = "com.xhx.spring",
        useDefaultFilters = false,
        includeFilters = {
            @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})
        }
)
public class MyConfig {
}

輸出結(jié)果:

輸出spring容器中的bean的測(cè)試類:只過濾輸出了名字中含有hello的類。

package com.xhx.spring.componentscan; 
import com.xhx.spring.componentscan.config.MyConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner; 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class ComponentScanApplicationTests { 
    @Test
    public void testLoads() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        List<String> hello = Arrays.stream(context.getBeanDefinitionNames()).collect(Collectors.toList());
        hello.stream().filter(name->name.contains("hello")).peek(System.out::println).count();
    } 
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot項(xiàng)目監(jiān)控開發(fā)小用例(實(shí)例分析)

    springboot項(xiàng)目監(jiān)控開發(fā)小用例(實(shí)例分析)

    這篇文章主要介紹了springboot項(xiàng)目監(jiān)控開發(fā)小用例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java中Timer定時(shí)器的使用和啟動(dòng)方式

    java中Timer定時(shí)器的使用和啟動(dòng)方式

    這篇文章主要介紹了java中Timer定時(shí)器的使用和啟動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring中的refreshContext源碼分析

    Spring中的refreshContext源碼分析

    這篇文章主要介紹了Spring中的refreshContext源碼分析,在SpringBoot啟動(dòng)流程中,主要的兩個(gè)階段是初始化SpringApplication對(duì)象以及SpringApplication.run方法執(zhí)行的內(nèi)容,今天主要細(xì)講的是SpringApplication.run中的刷新容器refreshContext方法,需要的朋友可以參考下
    2023-12-12
  • 匯總Java中List 去重的 6 種方法

    匯總Java中List 去重的 6 種方法

    這篇文章主要介紹了匯總Java中List 去重的 6 種方法,文章圍繞主題展開List去重的方法,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • Java編寫實(shí)現(xiàn)窗體程序顯示日歷

    Java編寫實(shí)現(xiàn)窗體程序顯示日歷

    這篇文章主要為大家詳細(xì)介紹了Java編寫實(shí)現(xiàn)窗體程序顯示日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • GC算法實(shí)現(xiàn)垃圾優(yōu)先算法

    GC算法實(shí)現(xiàn)垃圾優(yōu)先算法

    為什么會(huì)存在那么多的垃圾回收算法呢?我想這個(gè)問題的答案可能是沒有任何一種內(nèi)存回收算法是完美的,所以在針對(duì)不同的情景需求下,不同的內(nèi)存回收算法有其獨(dú)特的優(yōu)勢(shì),所以最后就延續(xù)了多種回收算法
    2022-01-01
  • SpringBoot聲明式事務(wù)的簡(jiǎn)單運(yùn)用說明

    SpringBoot聲明式事務(wù)的簡(jiǎn)單運(yùn)用說明

    這篇文章主要介紹了SpringBoot聲明式事務(wù)的簡(jiǎn)單運(yùn)用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • SpringBoot?實(shí)現(xiàn)流控的操作方法

    SpringBoot?實(shí)現(xiàn)流控的操作方法

    本文介紹了限流算法的基本概念和常見的限流算法,包括計(jì)數(shù)器算法、漏桶算法和令牌桶算法,還介紹了如何在Spring?Boot中使用Guava庫和自定義注解以及AOP實(shí)現(xiàn)接口限流功能,感興趣的朋友一起看看吧
    2024-12-12
  • 詳解Java設(shè)計(jì)模式編程中命令模式的項(xiàng)目結(jié)構(gòu)實(shí)現(xiàn)

    詳解Java設(shè)計(jì)模式編程中命令模式的項(xiàng)目結(jié)構(gòu)實(shí)現(xiàn)

    這篇文章主要介紹了Java設(shè)計(jì)模式編程中命令模式的項(xiàng)目結(jié)構(gòu)實(shí)現(xiàn),命令模式將請(qǐng)求與執(zhí)行分離,可以多個(gè)命令接口的實(shí)現(xiàn)類,隱藏真實(shí)的被調(diào)用方,需要的朋友可以參考下
    2016-04-04
  • springboot自動(dòng)重連Redis的實(shí)現(xiàn)方法

    springboot自動(dòng)重連Redis的實(shí)現(xiàn)方法

    由于網(wǎng)絡(luò)或服務(wù)器問題,Redis連接可能會(huì)斷開,導(dǎo)致應(yīng)用程序無法繼續(xù)正常工作,本文主要介紹了springboot自動(dòng)重連Redis的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02

最新評(píng)論

正安县| 府谷县| 普定县| 密云县| 裕民县| 青河县| 汉沽区| 高密市| 达州市| 华池县| 穆棱市| 铜鼓县| 定日县| 墨玉县| 景泰县| 长宁区| 巴林右旗| 蒲江县| 千阳县| 青铜峡市| 甘泉县| 红河县| 札达县| 北海市| 额济纳旗| 拜泉县| 姚安县| 汕尾市| 娄烦县| 华蓥市| 图们市| 额敏县| 安溪县| 华坪县| 衡山县| 崇文区| 长丰县| 长汀县| 遵义市| 和平区| 上栗县|