大家都了解过传统的Xml配置方式的Spring,基本上都知道Xml配置中有<Context:compent-scan>标签,它的作用是开启包扫描,把标注了@Controller、@Service、@Repository、@Component注册进Spring容器中,而Spring注解之@ComponentScan的作用就是代替此功能。
我们来看看使用得比较多的userDefaultFilters、lazyInit、excludeFilters、includeFilters属性。
userDefaultFiters:是否使用默认的过滤规则,默认为truelazyInit:是否启用懒加载加载扫描到的BeanexcludeFiters:使用Filter,指定哪些类型不需要扫描includeFiters:使用Filter,指定哪些类型需要扫描上面的ComponentScan的excludeFiters、includeFiters都使用了Filter,在这里我们再来了解一下Filter,但是在了解@Filter的使用之前,我们需要先了解FilterType枚举。
FilterType就是我们的过滤类型,如自定义过滤类型、注解过滤类型。Filter的作用就是利用我们的多种过滤类型,指定我们怎么过滤,需要过滤哪些东西。 /** * Enumeration of the type filters that may be used in conjunction with * {@link ComponentScan @ComponentScan}. * * @author Mark Fisher * @author Juergen Hoeller * @author Chris Beams * @since 2.5 * @see ComponentScan * @see ComponentScan#includeFilters() * @see ComponentScan#excludeFilters() * @see org.springframework.core.type.filter.TypeFilter */ public enum FilterType { /** * Filter candidates marked with a given annotation. * @see org.springframework.core.type.filter.AnnotationTypeFilter */ ANNOTATION, /** * Filter candidates assignable to a given type. * @see org.springframework.core.type.filter.AssignableTypeFilter */ ASSIGNABLE_TYPE, /** * Filter candidates matching a given AspectJ type pattern expression. * @see org.springframework.core.type.filter.AspectJTypeFilter */ ASPECTJ, /** * Filter candidates matching a given regex pattern. * @see org.springframework.core.type.filter.RegexPatternTypeFilter */ REGEX, /** Filter candidates using a given custom * {@link org.springframework.core.type.filter.TypeFilter} implementation. */ CUSTOM }此枚举的作用就是定义过滤类型,但是这里我们只讲解使用频率较高注解和自定义过滤类型,其他的多种过滤类型大家可以去参考官方文档,自行摸索。
/** * Declares the type filter to be used as an {@linkplain ComponentScan#includeFilters * include filter} or {@linkplain ComponentScan#excludeFilters exclude filter}. */ @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 * — 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 {}; }Filter中的type指定过滤类型,classes是指定的类为value属性的别名。
指定扫描org.example.componentscan 不使用自定义的过滤器 开启懒加载加载Bean 使用excludeFilters,基于注解的方式排除Conntroller 使用includeFilters,基于注解的方式添加Controller,Service
测试 @Test public void componentScanAnnotationTest(){ AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanConfig.class); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for (String beanName : beanDefinitionNames) { System.out.println("beanName:" + beanName + "===" + "bean对象:" + applicationContext.getBean(beanName)); } } 测试结果 我们可以看到Controller被排除了,@Service注册了,因为我们没有使用默认的过滤方式,所以Dao也没有注册进来。自定义过滤类型需要实现TypeFilter接口,我们来看看TypeFilter接口的源码
/* * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.core.type.filter; import java.io.IOException; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; /** * Base interface for type filters using a * {@link org.springframework.core.type.classreading.MetadataReader}. * * @author Costin Leau * @author Juergen Hoeller * @author Mark Fisher * @since 2.5 */ @FunctionalInterface public interface TypeFilter { /** * Determine whether this filter matches for the class described by * the given metadata. * @param metadataReader the metadata reader for the target class * @param metadataReaderFactory a factory for obtaining metadata readers * for other classes (such as superclasses and interfaces) * @return whether this filter matches * @throws IOException in case of I/O failure when reading metadata */ boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException; }这个接口是个函数式接口,match方法如果返回true就注册,如果false反之。
实现TypeFilter接口 package org.example.componentscan; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.TypeFilter; import java.io.IOException; /** * @author shiKui */ public class MyFilterType implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { return true; } }所有匹配到的都返回true,也就是都能注册进Spring
重新定义一个配置类 package org.example.componentscan; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; @Configuration @ComponentScan(value = "org.example.componentscan",includeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM,classes = MyFilterType.class),useDefaultFilters = false) public class ComponentScanCustomConfig { }扫描org.example.componentscan,不使用默认的过滤,使用includeFilters 属性通过我们自定义的过滤器,注册组件。
测试方法 @Test public void componentScanCustomTest(){ AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanCustomConfig.class); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for (String beanName : beanDefinitionNames) { System.out.println("beanName:" + beanName + "===" + "bean对象:" + applicationContext.getBean(beanName)); } } 测试结果 结果是意料之中。 至此相信大家对@ComponentScan也有一定的了解了,如果大家想更深入的了解,可以跟着Spring的官方文档、源码去一步步深入了解。 至此,如果我哪里不对,或者大家有什么想法可以在评论中提出。