上一篇博客(细说Spring——IoC详解(注解驱动开发之Bean的注入))中简单的介绍了将组件注入容器的三种方法,这次我们就了解一下如何在包扫描时将不想要的组件排除,或者只添加特定的组件,然后我们学习一下FactoryBean的作用,不知道FactoryBean的可以参考一下:细说Spring——IoC详解(FactoryBean、方法注入和方法替换)。
使用@ComponentScan
指定要扫描的包,和使用xml
配置的包扫描大致类似使用excludeFilters
属性添加排除的组件,使用includeFilters
属性添加只要的组件,但是要使includeFilters
生效,必须先将useDefaultFilters
属性设置为false
,和xml
配置类似,如果使用的是jdk8
以上的版本,可以定义多个@ComponentScan
,如果jdk8
以下的版本,可以使用@ComponentScans
,来装多个@ComponentScan
达到相同的效果。
下面我们主要看一下怎么添加excludeFilters
和includeFilters
属性,我们先看一下这两个属性的源码是什么:
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.
* 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.
* The following table explains how the classes will be interpreted
* based on the configured value of the {@link #type} attribute.
*
* {@code FilterType} Class Interpreted As
* {@link FilterType#ANNOTATION ANNOTATION}
* the annotation itself
* {@link FilterType#ASSIGNABLE_TYPE ASSIGNABLE_TYPE}
* the type that detected components should be assignable to
* {@link FilterType#CUSTOM CUSTOM}
* an implementation of {@link TypeFilter}
*
* When multiple classes are specified, OR logic is applied
* — for example, "include types annotated with {@code @Foo} OR {@code @Bar}".
*
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}:
*
* - {@link org.springframework.context.EnvironmentAware EnvironmentAware}
* - {@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}
*
- {@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}
*
- {@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}
*
* 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}.
* 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
:按照注解来过滤beanFilterType.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();
}
接下来我们看一下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 {
}
很明显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
:
//创建一个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());