美文网首页
SpringBoot--排除指定的bean--方法/实例

SpringBoot--排除指定的bean--方法/实例

作者: 分布式与微服务 | 来源:发表于2022-10-26 09:01 被阅读0次

简介

说明

本文介绍SpringBoot如何排除某个指定的类(不将它扫描到容器中)。

本文说的这种情况是排除本来使用@Component等加入容器的类。如果想要排除自动配置(即:自定义starter,通过spring.factories来指定的配置类),见此文:SpringBoot--排除自动配置类--方法/实例_IT利刃出鞘的博客-CSDN博客

需求

使用 RuoYi(若依)源码,不想用 RuoYi 自带的权限管理(ShiroConfig) ,但是不删除它,只是不让它生效。

方案1:@ComponentScan

@ComponentScan(excludeFilters = 
    {@ComponentScan.Filter(
        type = FilterType.REGEX, 
        pattern = {com.ruoyi.framework.config.ShiroConfig"})})
@SpringBootConfiguration
public class RuoYiApplication {
    ...
}

1.在@ComponentScan 中指定 excludeFilters 属性。该属性指定排除的Bean/类。
2.使用正则表达式方式(FilterType.REGEX)排除类 "com.ruoyi.framework.config.ShiroConfig"

方案2:@ComponentScans

@ComponentScans说明

@ComponentScans由所有的@ComponentScan组成,每个 @ComponentScan 都会影响 @ComponentScans。

自定义@ComponentScans的方法

@Configuration
@EnableAutoConfiguration
@ComponentScans({
    @ComponentScan(
            basePackages = {"com.ruoyi"}, 
            excludeFilters = {
                    @Filter(type = FilterType.REGEX, pattern = {
                            "com.ruoyi.framework.config.ShiroConfig"})}),
    @ComponentScan(basePackages = {"com.third.package"}),
})
public class RuoYiApplication {
    ...
}

@ComponentScan.Filter详解

简介

说明

可以看到,上边都是用了一个注解:@ComponentScan.Filter。

含义说明

@ComponentScan(excludeFilters = {
    @ComponentScan.Filter(
        type = FilterType.ASSIGNABLE_TYPE, 
        value = {Xxx.class, Yyy.class})})

上边这段代码的含义:Xxx和Yyy这两个类不会作为bean加入容器(即使它们类上标注了@Component等)。

@ComponentScan.Filter源码

package org.springframework.context.annotation;
 
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
 
    // 其他
 
    Filter[] includeFilters() default {};
 
    Filter[] excludeFilters() default {};
 
    @Retention(RetentionPolicy.RUNTIME)
    @Target({})
    @interface Filter {
 
        FilterType type() default FilterType.ANNOTATION;
 
        @AliasFor("classes")
        Class<?>[] value() default {};
 
        @AliasFor("value")
        Class<?>[] classes() default {};
 
        String[] pattern() default {};
 
    }
}

FilterType

package org.springframework.context.annotation;
 
public enum FilterType {
    ANNOTATION,
    ASSIGNABLE_TYPE,
    ASPECTJ,
    REGEX,
    CUSTOM
}

1.ANNOTATION:注解类型
表示:加了指定的注解的类不加入容器。
例:(下边Xxx和Yyy都是注解类)
excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {Xxx.class, Yyy.class})}

2.ASSIGNABLE_TYPE:指定类型
含义:指定的类不作为bean加入容器。
例:(下边Xxx和Yyy都是注解类)
excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {Xxx.class, Yyy.class})}

3.ASPECTJ:按照Aspectj表达式
含义:符合指定Aspectj表达式的类不加入容器。

4.REGEX:按照正则表达式
含义:符合指定正则表达式的类不加入容器。
例:
excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = {"com.example.", "com.abc.def."})}

5.CUSTOM:自定义规则

可使用多个

@ComponentScan(encludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Xxx.class)
        , @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Yyy.class)
        , @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Zzz.class)})

相关文章

  • SpringBoot--排除指定的bean--方法/实例

    简介 说明 本文介绍SpringBoot如何排除某个指定的类(不将它扫描到容器中)。 本文说的这种情况是排除本来使...

  • CABasicAnimation使用总结

    实例化 使用方法animationWithKeyPath:对CABasicAnimation进行实例化,并指定La...

  • CABasicAnimation笔记

    实例化 使用方法animationWithKeyPath:对 CABasicAnimation进行实例化,并指定L...

  • CABasicAnimation使用总结

    实例化 使用方法animationWithKeyPath:对 CABasicAnimation进行实例化,并指定L...

  • Swift 中的协议

    实例属性,实例方法,类方法,运算符,下标 语法 属性 指定属性的 名字 和 类型,以及 getterable or...

  • Java 实例 - 字符串分割

    Java 实例 - 字符串分割 Java 实例 以下实例使用了 split(string) 方法通过指定分隔符将字...

  • hibernate +常用方法

    、hibetnateTemplate常用方法 1、delete(Object entity) 删除指定的持久化实例...

  • call() && apply() 实例

    实例 call() 方法 call() 简单用法: 传入指定对象,改变 this 指向 apply() 方法 ap...

  • Linux tar命令打包

    打包指定目录: 解压到指定目录: 打包排除指定文件(这里排除的是cache目录): 安卓magisk 需要这样:

  • 4.6 自定义节点-svg

    方法1:registerNode方法 markup 1 .selector元素,实例化对象时,重新指定元素样式的时...

网友评论

      本文标题:SpringBoot--排除指定的bean--方法/实例

      本文链接:https://www.haomeiwen.com/subject/nmoowrtx.html