相关文章推荐
聪明伶俐的铁板烧  ·  Spring Boot ...·  4 周前    · 
谦虚好学的太阳  ·  Swagger UI "Try it ...·  3 周前    · 
酷酷的羊肉串  ·  “JavascriptException: ...·  1 年前    · 
飘逸的橙子  ·  更新Android studio, ...·  1 年前    · 
爱逃课的葡萄酒  ·  Amazon Live·  1 年前    · 

技术公众号:后端技术解忧铺
关注微信公众号:CodingTechWork,一起学习进步。

在抽取公共swagger配置类时,将swagger放入com.test.common.config包内,其他模块通过 @ComponentScan 进行进行引用,但有的模块在引用时,会扫描到 common.config 包路径下的其他配置类而引发错误,如引用到 RedisConfig 类而报错,此时需要将该类排除掉。

通过 @ComponentScan 中的 excludeFilters 属性进行排除类。

@SpringBootApplication
@ComponentScan(basePackages = {"com.test.common.config"},
    excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {RedisConfig.class})})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

附:FilterType

package org.springframework.context.annotation;
public enum FilterType {
    ANNOTATION,
    ASSIGNABLE_TYPE,
    ASPECTJ,
    REGEX,
    CUSTOM;
    private FilterType() {
  1. ANNOTATION:注解类型
  2. ASSIGNABLE_TYPE:指定类型
  3. ASPECTJ:按照Aspectj表达式
  4. REGEX:按照正则表达式
  5. CUSTOM:自定义规则
问题  在抽取公共swagger配置类时,将swagger放入com.test.common.config包内,其他模块通过@ComponentScan进行进行引用,但有的模块在引用时,会扫描到common.config包路径下的其他配置类而引发错误,如引用到RedisConfig类而报错,此时需要将该类排除掉。解决方案  通过@ComponentScan中的excludeFilters属性进行排除类。@SpringBootApplication@ComponentScan(basePackage
@ComponentScan注解排除属性 @ComponentScan组件源码,直截取了用到的属性: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Repeatable(ComponentScans.class) public @interface ComponentScan { // 是否使用默认过滤规则:默认为true,被@Component、@Repository、@Service、@Co
文章目录前因方式一方式二方式三方式四方式五方式六总结 我们都知道component-scan在springmvc或者springboot中可以扫描包路径,但是我们如何使用里面的属性排除不需要扫描? 使用spring的xml配置方式实现,这个是基本功,知道这种方式,那么注解方式就容易理解了 <!-- 定义项目扫描包的路径,并且排除ApplicationContextConfig和WebSpringMVCServletConfig--> <context:component-
SpringBoot项目中有时候某些不需要被 @ComponentScan注解扫描到, 比如在给某个服务的Ribbon自定义配置时,为得防止 @Configuration注解所在的包与 @ComponentScan扫描的包重叠. 使用 @ComponeentScan 的 excludeFilters 属性进行设定我们需要排除. @ComponentScan(excludeFilters
指定包扫描的根路径,让 Spring 来扫描指定包及子包下的组件。不过在上面的声明中有显式的指定了两个过滤条件: @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, c...
@ComponentScan用于或接口上,主要是指定扫描路径并把带有指定注解注册到Spring容器中。 会被自动装配的注解包括@Component、@Bean、@Controller、@Service、@Repository等等。 二、定义组件 1. @Service注解 MyService扫描后,会生成名为myBeanService的实例 package info.pigg.study.java.service; import org.sprin
在项目初始化时,会将加@component,@service...相关注解添加到spring容器中。 但是项目需要,项目初始化时自动过滤某包下面的,不将其添加到容器中。 有两种实现方案, 1.如果要过滤比较少,直接在启动@SpringbootApplication(scanPackage="xxx.xxx.xxx")该注解上添加exclude属性,过滤某个class 也可...
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX,pattern = "com.action.other.*") public class Application { public static void ma...
最近接手一套基于SpringBoot项目,对项目进行重构调整,将公共部分抽离成子项目。在实践的过程中,发现抽离之后的模板中组件并没有被初始化。于是将排查解决过程中搜集到的方案及知识汇总分享给大家。 问题的原因很简单,因多套系统的package命名不一致。比如业务系统的包命名为com.abc.xx,而公共(common)部分的包命名为com.efg.xx,引入公共jar包时默认是无法初始化的。 对于SpringBoot项目,我们知道扫描的路径从启动所在包开始,扫描当前包及其子级包下的所有文件。上
最近在学习SpringCloud的Ribbon,在使用 @RibbonClient(name = "SPRINGCLOUD-P-DEPT", configuration = RibbonConfig.class) 为服务指定负载均衡策略的时候,自定义的Ribbon配置不能被Springboot的**@ComponentScan**注解扫描到,所以需要将自定义的配置Ribbo...