Springboot项目实现将类从@ComponentScan中排除

 更新时间:2021年11月05日 10:17:57   作者:SenKnight  
这篇文章主要介绍了Springboot项目实现将类从@ComponentScan中排除,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

将类从@ComponentScan中排除

问题描述

最近在学习SpringCloud的Ribbon,在使用

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

为服务指定负载均衡策略的时候,根据Ribbon官方文档介绍,自定义的Ribbon配置类不允许被Springboot的**@ComponentScan**注解扫描到,所以需要将自定义的配置类RibbonConfig从在Springboot自动注入的范围内排除

方案一

我们都知道,Springboot的**@SpringBootApplication**会自动扫描本类所在包下的所有类和子类,所以只需要将RibbonConfig定义在Springboot启动类所在包外面即可

方案二

通过在启动类中添加

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

通过FilterType.ASSIGNABLE_TYPE来指定要排除的类

如果需要排除的类太多了这个就很麻烦

方案三

通过自定义注解实现

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

与方案二不同的是,这里用的是FilterType.ANNOTATION

方案四

通过实现TypeFilter类来自定义过滤器

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

此处给出的就是**@SpringbootApplication中的实现方式,通过FilterType.CUSTOM**来根据自动一过滤器来排除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 的作用就是根据定义的扫描路径,把符合扫描规则的类装配到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: 用于指定包的路径,进行扫描
  • basePackageClasses: 用于指定某个类的包的路径进行扫描
  • nameGenerator: bean的名称的生成器
  • useDefaultFilters: 是否开启对@Component,@Repository,@Service,@Controller的类进行检测
  • includeFilters: 包含的过滤条件

FilterType.ANNOTATION:按照注解过滤

FilterType.ASSIGNABLE_TYPE:按照给定的类型

FilterType.ASPECTJ:使用ASPECTJ表达式

FilterType.REGEX:正则

FilterType.CUSTOM:自定义规则

  • excludeFilters: 排除的过滤条件,用法和includeFilters一样

我的工程结构如下,测试对controller和service的扫描,其中HelloController没有加@Controller等任何注解,就是一个普通类。

修改配置类如下:应用默认的过滤器,扫描service包:

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

系统注入了两个service进去

改成如下所示:HelloController所在的包的类也被扫描了进去

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

系统中会注入下面就给类

把默认的过滤器关掉,扫描带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 {
}

输出结果:

输出spring容器中的bean的测试类:只过滤输出了名字中含有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();
    } 
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot在 POM 中引入本地 JAR 包的方法

    SpringBoot在 POM 中引入本地 JAR 包的方法

    在开发 Spring Boot 应用程序时,您可能需要使用本地 JAR 包来添加自定义库或功能,本文将介绍在 Spring Boot 项目的 POM 文件中如何引入本地 JAR 包,感兴趣的朋友跟随小编一起看看吧
    2023-08-08
  • Java中如何使用正则表达式提取各种类型括号中的内容

    Java中如何使用正则表达式提取各种类型括号中的内容

    最近在工作中遇到一个问题,就是需要一个字符串中每一个中括号里的内容,下面这篇文章主要给大家介绍了关于Java中如何使用正则表达式提取各种类型括号中的内容,需要的朋友可以参考下
    2023-06-06
  • ArrayList源码和多线程安全问题分析

    ArrayList源码和多线程安全问题分析

    这篇文章主要介绍了ArrayList源码和多线程安全问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面小编和大家一起来学习一下吧
    2019-05-05
  • 详解idea打包jar的多种方式

    详解idea打包jar的多种方式

    本篇文章总结出用IDEA打包jar包的多种方式。项目打包Jar包可以参考如下形式:用IDEA自带的打包形式;用Maven插件maven-shade-plugin打包;用Maven插件maven-assembly-plugin打包。下面跟着小编一起来看下吧
    2017-01-01
  • 关于struts2中Action名字的大小写问题浅谈

    关于struts2中Action名字的大小写问题浅谈

    这篇文章主要给大家介绍了关于struts2中Action名字大小写问题的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • Java图书管理系统,课程设计必用(源码+文档)

    Java图书管理系统,课程设计必用(源码+文档)

    本系统采用Java,MySQL 作为系统数据库,重点开发并实现了系统各个核心功能模块,包括采编模块、典藏模块、基础信息模块、流通模块、期刊模块、查询模块、评论模块、系统统计模块以及帮助功能模块
    2021-06-06
  • 详解Java程序启动时-D指定参数是什么

    详解Java程序启动时-D指定参数是什么

    java服务启动的时候,都会指定一些参数,下面这篇文章主要给大家介绍了关于Java程序启动时-D指定参数是什么的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • 浅谈对象与Map相互转化

    浅谈对象与Map相互转化

    这篇文章主要介绍了利用BeanMap进行对象与Map的相互转换,在文中列举了完整代码,需要的朋友可以参考下。
    2017-09-09
  • 代码分析Java中线程的等待与唤醒

    代码分析Java中线程的等待与唤醒

    本篇文章给大家分享了关于Java中线程的等待与唤醒的知识点内容,有需要的朋友们可以学习下。
    2018-10-10
  • Mybatis基于注解与XML开发使用流程

    Mybatis基于注解与XML开发使用流程

    MyBatis是Java的持久化框架,目的是为了使操作数据库更加方便、灵活、高效,可以通过Java注解和XML文件来映射Java对象和SQL语句,提供了非常灵活的SQL编写方式和动态SQL语句的创建方式,这篇文章主要介绍了Mybatis基于注解与XML开发,需要的朋友可以参考下
    2023-07-07

最新评论