Spring中FilterType的说明

说明

我们在使用spring配置文件或者是注解的时有时会看到以下内容:

  • 配置文件:
  <!--包扫描 扫描后注入@Controller @Service @Component @Repository到容器中-->
   <context:component-scan base-package="Spring" >
       <!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>-->
       <context:exclude-filter type="custom" expression="Spring.config.MyComponentFilter"></context:exclude-filter>
   </context:component-scan>
    <bean name="myFilter" class="Spring.config.MyComponentFilter"></bean>
  • 注解
@ComponentScans(value = {
        @ComponentScan(basePackages = {"Spring"},excludeFilters = {
               // @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class),
               // @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = PsersonDao.class)+
                @ComponentScan.Filter(type = FilterType.CUSTOM,classes = MyComponentFilter.class)
        })
})

那么这里的FilterType到底是什么意思呢?下面为大家说明下。

FilterType

在spring中FilterType包括以下几类

public enum FilterType {
    ANNOTATION, //按照注解过滤
    ASSIGNABLE_TYPE, //按照类型过滤
    ASPECTJ,//按照ASPECTJ表达式过滤
    REGEX,//按照正则表达式过滤
    CUSTOM;//按照自定义的过滤规则过滤
    private FilterType() {
    }
}
  • 按照注解过滤:
    就是看要注入到容器的类上有哪些注解类似@Controller @Service 这些。
  • 按照类型过滤:
    可以指定需要过滤的组件的类型,类似于xxx.class
  • 按照ASPECTJ表达式过滤:
    就是用用ASPECTJ定义过滤规则
  • 按照正则表达式过滤:
    使用正则表达式定义过滤规则
  • 自定义的过滤规则过滤:
    spring提供了可自定的过滤规则的方式,按照你自己定义的规则进行过滤。
    接下来通过配置和注解两种方式来为大家说明:

配置文件方式实现

  1. 如果不使用自定义的过滤方式,那么只需要在spring的配置文件中按照以下方式配置
 <!--包扫描 扫描后注入@Controller @Service @Component @Repository到容器中-->
   <context:component-scan base-package="Spring" >
   <!-- 以下方式就是按照注解配置的方式 需要过滤的是Controller注解,除了自定义之外的其他方式也可以参考一下方式定义-->
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
   </context:component-scan>
  1. 如果要使用自定义的过滤规则,这里就需要大家注意下,需要自己手动写一个过滤规则的类,并且需要实现org.springframework.core.type.filter.TypeFilter。如下:
/**
 * @Description
 * @auther Eleven
 * @create 2020-04-11 15:51
 **/
public class MyComponentFilter implements TypeFilter {
    /**
     * @param metadataReader        读取当前正在扫描的类的信息
     * @param metadataReaderFactory 可以读取到其他任何类的信息
     * @return
     * @throws IOException
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        String className = metadataReader.getClassMetadata().getClassName();
        System.out.println("当前正在扫描的类名:"+className);
        //这里的规则是如果类名中包含Service就返回true
        return className.contains("Service");
    }
}

接下来需要在配置文件中配置如下:

   <context:component-scan base-package="Spring" >
       <context:exclude-filter type="custom" expression="Spring.config.MyComponentFilter"></context:exclude-filter>
   </context:component-scan>
    <bean name="myFilter" class="Spring.config.MyComponentFilter"></bean>
  1. 测试方法
    可以建立一些类,放到被扫描的包下,然后看看扫描到的类的内容
public static void main( String[] args )
    {
        //读取配置文件 spring配置文件这里命名为bean.xml
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
       //获得IOC容器中的加载的类并且输出
        String[] arr = applicationContext.getBeanDefinitionNames();
        for (String i:arr){
            System.out.println(i);
        }
        System.out.println( p );  
    }

注解方式

  1. 如果不使用自定义的过滤方式, 在配置类中写如下注解即可

/**
 * @Description
 * @auther Eleven
 * @create 2020-04-11 14:58
 **/
//标记这是一个配置文件
@Configuration
@ComponentScans(value = {
        @ComponentScan(basePackages = {"Spring"}, excludeFilters = {
                 @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class),
                 @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = PsersonDao.class)
        })
})
public class MyConfig {
    
}
  1. 如果要使用自定义规则过滤也要参考配置文件实现中的第二步写一个自定的过滤规则类这里不再赘述。
  2. 测试方法,首先要在自定义的配置类上写入以下注解
/**
 * @Description
 * @auther Eleven
 * @create 2020-04-11 14:58
 **/
//标记这是一个配置文件
@Configuration
@ComponentScans(value = {
        @ComponentScan(basePackages = {"Spring"}, excludeFilters = {
                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyComponentFilter.class)
        })
})
public class MyConfig {
}
public class App 
{
    public static void main( String[] args )
    {
        //读取注解
        ApplicationContext applicationContext1 = new AnnotationConfigApplicationContext(MyConfig.class);
        //打印出IOC容器中包含的组件
         String[] arr = applicationContext1.getBeanDefinitionNames();
        for (String i:arr){
            System.out.println(i);
        }
    }
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值