细说Spring——IoC详解(注解驱动开发之包扫描过滤和FactoryBean)

一、前言

上一篇博客(细说Spring——IoC详解(注解驱动开发之Bean的注入))中简单的介绍了将组件注入容器的三种方法,这次我们就了解一下如何在包扫描时将不想要的组件排除,或者只添加特定的组件,然后我们学习一下FactoryBean的作用,不知道FactoryBean的可以参考一下:细说Spring——IoC详解(FactoryBean、方法注入和方法替换)

二、包扫描的过滤

使用@ComponentScan指定要扫描的包,和使用xml配置的包扫描大致类似使用excludeFilters属性添加排除的组件,使用includeFilters属性添加只要的组件,但是要使includeFilters生效,必须先将useDefaultFilters属性设置为false,和xml配置类似,如果使用的是jdk8以上的版本,可以定义多个@ComponentScan,如果jdk8以下的版本,可以使用@ComponentScans,来装多个@ComponentScan达到相同的效果。

下面我们主要看一下怎么添加excludeFiltersincludeFilters属性,我们先看一下这两个属性的源码是什么:

    Filter[] includeFilters() default {};

    /**
     * Specifies which types are not eligible for component scanning.
     * @see #resourcePattern
     */
    Filter[] excludeFilters() default {};

我们可以看到这个个属性的参数都是Filter数组,这里的Filter是在@ComponentScan包里的一个内部注解,我们看一下源码:

    @Retention(RetentionPolicy.RUNTIME)
    @Target({})
    @interface Filter {

        /**
         * The type of filter to use.
         * <p>Default is {@link FilterType#ANNOTATION}.
         * @see #classes
         * @see #pattern
         */
        FilterType type() default FilterType.ANNOTATION;

        /**
         * Alias for {@link #classes}.
         * @see #classes
         */
        @AliasFor("classes")
        Class<?>[] value() default {};

        /**
         * The class or classes to use as the filter.
         * <p>The following table explains how the classes will be interpreted
         * based on the configured value of the {@link #type} attribute.
         * <table border="1">
         * <tr><th>{@code FilterType}</th><th>Class Interpreted As</th></tr>
         * <tr><td>{@link FilterType#ANNOTATION ANNOTATION}</td>
         * <td>the annotation itself</td></tr>
         * <tr><td>{@link FilterType#ASSIGNABLE_TYPE ASSIGNABLE_TYPE}</td>
         * <td>the type that detected components should be assignable to</td></tr>
         * <tr><td>{@link FilterType#CUSTOM CUSTOM}</td>
         * <td>an implementation of {@link TypeFilter}</td></tr>
         * </table>
         * <p>When multiple classes are specified, <em>OR</em> logic is applied
         * &mdash; for example, "include types annotated with {@code @Foo} OR {@code @Bar}".
         * <p>Custom {@link TypeFilter TypeFilters} may optionally implement any of the
         * following {@link org.springframework.beans.factory.Aware Aware} interfaces, and
         * their respective methods will be called prior to {@link TypeFilter#match match}:
         * <ul>
         * <li>{@link org.springframework.context.EnvironmentAware EnvironmentAware}</li>
         * <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}
         * <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}
         * <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}
         * </ul>
         * <p>Specifying zero classes is permitted but will have no effect on component
         * scanning.
         * @since 4.2
         * @see #value
         * @see #type
         */
        @AliasFor("value")
        Class<?>[] classes() default {};

        /**
         * The pattern (or patterns) to use for the filter, as an alternative
         * to specifying a Class {@link #value}.
         * <p>If {@link #type} is set to {@link FilterType#ASPECTJ ASPECTJ},
         * this is an AspectJ type pattern expression. If {@link #type} is
         * set to {@link FilterType#REGEX REGEX}, this is a regex pattern
         * for the fully-qualified class names to match.
         * @see #type
         * @see #classes
         */
        String[] pattern() default {};

    }

我们可以看到内部使用FilterType枚举来表明了当前的Filter是按照什么过滤的,这里常用的有三种:

  • FilterType.ANNOTATION:按照注解来过滤bean
  • FilterType.ASSIGNABLE_TYPE :按照给定的类型
  • FilterType.CUSTOM:按照自己给定的过滤器过滤

下面我们就挨个展示一下这三种的用法。首先是FilterType.ANNOTATION,这个是按照注解来过滤,首先看一下在配置类:

@ComponentScan(value = "com.jiayifan.bean", excludeFilters = {
    @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {myAnno.class}),
    //@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class})
    //@ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyTypeFilter.class)
})


@Configuration//告诉spring这是一个配置类
public class MainConfig {

}

我们用包扫描往容器中注入组件,这里我就注入的两个组件
Person

@Component
public class Person {}

Blue

@Component
@myAnno
public class Blue {
}

注意这里的Blue上面标有@myAnno注解,而我们的配置类中使用excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {myAnno.class})}
把标有@myAnno注解的组件排除了,现在我们看一下测试的结果:

@Test
public void importTest() {
     printBeans();
}

这里写图片描述
可以看到果然没有blue这个组件。

接下来我们看一下FilterType.ASSIGNABLE_TYPE ,这个是按照类型来过滤的,我们仍然使用上面的例子,只不过变化一下过滤的规则,只修改一下配置类:

@ComponentScan(value = "com.jiayifan.bean", excludeFilters = {
    //@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {myAnno.class}),
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {Person.class})
    //@ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyTypeFilter.class)
})


@Configuration//告诉spring这是一个配置类
public class MainConfig {

}

可以看到我把Pserosn过滤了,这里看一下测试结果:
这里写图片描述

很明显Person类已经被过滤了。

然后我们来学习一下FilterType.CUSTOM,这个需要我们自己定义过滤的规则,我们需要自己实现一个过滤器,这个过滤器实现TypeFilter接口,看一下我实现的一个过滤器:

/**
 * Created by Yifan Jia on 2018/6/12.
 * 自定的扫描规则
 */
public class MyTypeFilter implements TypeFilter{
    /**
     *
     * @param metadataReader 读取到的当前正在扫描的类的信息
     * @param metadataReaderFactory 可以获取到其他任何类信息
     * @return
     * @throws IOException
     */
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {

        //获取当前类的直接信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();

        //获取当前正在扫描的类的信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();

        //获取当前类的资源信息(类的路径)
        Resource resource = metadataReader.getResource();

        //获取当前类的全类名
        String className = classMetadata.getClassName();

        System.out.println("classMetadata:    " + className);

        if(className.contains("B")) {
            return true;
        }

        return false;
    }
}

上面的实现类中我的过滤逻辑就是过滤全类名中含有“B”的类,我们看一下配置类:

package com.jiayifan.config;

/**
 * Created by Yifan Jia on 2018/6/12.
 * 配置类代理xml配置文件
 */

@ComponentScan(value = "com.jiayifan.bean", excludeFilters = {
    //@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {myAnno.class}),
    // @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {Person.class})
    @ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyTypeFilter.class)
})


@Configuration//告诉spring这是一个配置类
public class MainConfig {

}

看一下测试结果:
这里写图片描述

我们在过滤器中还实现了将扫描到的类名打印出来的功能,可以看到我的com.jiayifan.bean包中的类,然后扫描带有@Component的类加入到容器中,但是又将全类名中含有“B”字母的类排除,所以就只剩下person组件了。

三、FactoryBean

还记得上一篇博客中最后说除了三种常用方法可以注入组件外,我们还有一种方法可以向容器中注入组件吗,就是使用FactoryBean
首先我们需要先实现一个自己的FactoryBean

//创建一个Spring定义的工厂bean
public class ColorFactoryBean implements FactoryBean<Color> {
    //返回一个color对象,这个对象会添加到容器中
    //如果该bean是多实例的,就会在创建实例的时候调用getObjectType方法
    public Color getObject() throws Exception {
        System.out.println("ColorFactoryBean....getObject");
        return new Color();
    }


    public Class<?> getObjectType() {
        return Color.class;
    }

    //该bean是否是单实例的
    public boolean isSingleton() {
        return true;
    }
}

然后我们将这个FactoryBean注入容器:

@Configuration
public class MainConfig2 {
    @Bean
    public ColorFactoryBean colorFactoryBean() {
        return new ColorFactoryBean();
    }
}

然我我们看一下测试类:

    @Test
    public void importTest() {
         printBeans();
    }

    private void printBeans() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for(String name : beanDefinitionNames) {
            System.out.println(name);
        }
        //工厂bean获取的是调用getObject获得的对象
        Object colorFactoryBean = applicationContext.getBean("colorFactoryBean");
        //Object colorFactoryBean2 = applicationContext.getBean("colorFactoryBean");
        System.out.println("获得是:  " + colorFactoryBean.getClass());
    }

测试结果:
这里写图片描述
我们可以看到我们在打印容器中有哪些组件时,容器中的是colorFactoryBean,可是在我们获取到colorFactoryBean这个组件时,发现获取到的是color,这个功能虽然我们并不常用,但是还是需要了解一下,我们如果就是想要获得colorFactoryBean,我们只需要:

  Object colorFactoryBean = applicationContext.getBean("colorFactoryBean");
  //前面加一个&
  Object colorFactoryBean2 = applicationContext.getBean("&colorFactoryBean");
  System.out.println("获得是:  " + colorFactoryBean.getClass());
  System.out.println("获得是:  " + colorFactoryBean2.getClass());

然后获取到的就是colorFactoryBean
这里写图片描述

猜你喜欢

转载自blog.csdn.net/q982151756/article/details/80769291