其他分享
首页 > 其他分享> > Spring扩展——@Import注解

Spring扩展——@Import注解

作者:互联网

引言

在Spring中有许多Enable开头的注解,比如以下常见注解
@EnableTransactionManagement
@EanbleAsync
@EnableCache
@EnableAspectJAutoProxy
@EnableSchedulin
这些注解是在什么时候,什么地方被处理的呢?
我们在另一篇博客里面可以找到相应的答案——Spring源码——ConfigurationClassPostProcessor类
ConfigurationClassPostProcessor类继承关系图

@Import原理

@Import源码

/**
 * Indicates one or more <em>component classes</em> to import &mdash; typically
 * {@link Configuration @Configuration} classes.
 *
 * <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
 * Allows for importing {@code @Configuration} classes, {@link ImportSelector} and
 * {@link ImportBeanDefinitionRegistrar} implementations, as well as regular component
 * classes (as of 4.2; analogous to {@link AnnotationConfigApplicationContext#register}).
 *
 * <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes should be
 * accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
 * injection. Either the bean itself can be autowired, or the configuration class instance
 * declaring the bean can be autowired. The latter approach allows for explicit, IDE-friendly
 * navigation between {@code @Configuration} class methods.
 * <p>May be declared at the class level or as a meta-annotation.
 * <p>If XML or other non-{@code @Configuration} bean definition resources need to be
 * imported, use the {@link ImportResource @ImportResource} annotation instead.
 * 此注解,如果被扫描到,ConfigurationClassPostProcessor在处理的时候,会扫描其value字段,会根据Value字段的类型不同,分别调用其不同的处理方法。
 *
 * @author Chris Beams
 * @author Juergen Hoeller
 * @since 3.0
 * @see Configuration
 * @see ImportSelector
 * @see ImportBeanDefinitionRegistrar
 * @see ImportResource
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

	/**
	 * 可以引入普通Bean 配置Bean ImportSelector指定的名字的对象 ImportBeanDefinitionRegistrar手动向Register里面注册BennDefinition
	 * ImportSelector  ImportBeanDefinitionRegistrar 本身的实现类,也是作为一个Bean的,只不过它还能引入其它类。
	 * {@link Configuration @Configuration}, {@link ImportSelector},
	 * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
	 */
	Class<?>[] value();

}

ConfigurationClassPostProcessor属于BDRPP,所以在执行InvokeBeanFactoryPostProcessor的时候,会先执行其postProcessBeanDefinitionRegistry方法,通过这个方法可以向容器里面添加更多的BeanDefinition信息。
在执行其postProcessBeanDefinitionRegistry的时候,会遍历当前容器里BeanDefinitionsMap里面所有的Bean信息,如果是Configuration配置类,则会处理其@Import注解,在处理的时候,是递归进行处理的。
ConfigurationClassPostProcessor在处理Import注解的时候,会扫描其value字段,会根据Value字段的类型不同,分别调用其不同的处理方法。

// 遍历每一个@Import注解的类
				for (SourceClass candidate : importCandidates) {
					// 检验配置类Import引入的类是否是ImportSelector子类
					if (candidate.isAssignable(ImportSelector.class)) {
						// Candidate class is an ImportSelector -> delegate to it to determine imports
						// 候选类是一个导入选择器->委托来确定是否进行导入
						Class<?> candidateClass = candidate.loadClass();
						// 通过反射生成一个ImportSelect对象
						ImportSelector selector = ParserStrategyUtils.instantiateClass(candidateClass, ImportSelector.class,
								this.environment, this.resourceLoader, this.registry);
						// 获取选择器的额外过滤器
						Predicate<String> selectorFilter = selector.getExclusionFilter();
						if (selectorFilter != null) {
							exclusionFilter = exclusionFilter.or(selectorFilter);
						}
						// 判断引用选择器是否是DeferredImportSelector接口的实例
						// 如果是则应用选择器将会在所有的配置类都加载完毕后加载
						if (selector instanceof DeferredImportSelector) {
							// 将选择器添加到deferredImportSelectorHandler实例中,预留到所有的配置类加载完成后统一处理自动化配置类
							this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector);
						}
						else {
							// 获取引入的类,然后使用递归方式将这些类中同样添加了@Import注解引用的类
							String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());
							Collection<SourceClass> importSourceClasses = asSourceClasses(importClassNames, exclusionFilter);
							// 递归处理,被Import进来的类也有可能@Import注解
							processImports(configClass, currentSourceClass, importSourceClasses, exclusionFilter, false);
						}
					}
					// 如果是实现了ImportBeanDefinitionRegistrar接口的bd
					else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
						// Candidate class is an ImportBeanDefinitionRegistrar ->
						// delegate to it to register additional bean definitions
						// 候选类是ImportBeanDefinitionRegistrar  -> 委托给当前注册器注册其他bean
 						Class<?> candidateClass = candidate.loadClass();
						ImportBeanDefinitionRegistrar registrar =
								ParserStrategyUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class,
										this.environment, this.resourceLoader, this.registry);
						/**
						 * 放到当前configClass的importBeanDefinitionRegistrars中
						 * 在ConfigurationClassPostProcessor处理configClass时会随之一起处理
						 */
						configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata());
					}
					else {
						// Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar ->
						// process it as an @Configuration class
						// 候选类既不是ImportSelector也不是ImportBeanDefinitionRegistrar-->将其作为@Configuration配置类处理
						this.importStack.registerImport(
								currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
						/**
						 * 如果Import的类型是普通类,则将其当作带有@Configuration的类一样处理
						 * 将candidate构造为ConfigurationClass,标注为importedBy,意味着它是通过被@Import进来的
						 * 后面处理会用到这个判断将这个普通类注册进DefaultListableBeanFactory
						 */
						processConfigurationClass(candidate.asConfigClass(configClass), exclusionFilter);
					}
				}

@Import在处理的时候,根据它的value值的类型来选择不同的处理流程

@Import引入Bean

1. ImportSelector

public interface ImportSelector {

	/**
	 * Select and return the names of which class(es) should be imported based on
	 * the {@link AnnotationMetadata} of the importing @{@link Configuration} class.
	 * @return the class names, or an empty array if none
	 * 选择并返回应基于哪个类导入的名称
	 */
	String[] selectImports(AnnotationMetadata importingClassMetadata);

	/**
	 * Return a predicate for excluding classes from the import candidates, to be
	 * transitively applied to all classes found through this selector's imports.
	 * <p>If this predicate returns {@code true} for a given fully-qualified
	 * class name, said class will not be considered as an imported configuration
	 * class, bypassing class file loading as well as metadata introspection.
	 * @return the filter predicate for fully-qualified candidate class names
	 * of transitively imported configuration classes, or {@code null} if none
	 * @since 5.2.4
	 * 需要排除那项类的名称
	 */
	@Nullable
	default Predicate<String> getExclusionFilter() {
		return null;
	}
}

如果@Import引入的是ImportSelector的子类,将它添加到deferredImportSelectorHandler实例中,预留到所有的配置类加载完成后统一处理自动化配置类
此类的主要功能是:根据给定的条件(通常是一个或多个注解属性)判断要导入哪个配置类

2. ImportBeanDefinitionRegistrar

ublic interface ImportBeanDefinitionRegistrar {
	/**
	 * @since 5.2
	 * @see ConfigurationClassPostProcessor#IMPORT_BEAN_NAME_GENERATOR
	 * @see ConfigurationClassPostProcessor#setBeanNameGenerator
	 */
	default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
			BeanNameGenerator importBeanNameGenerator) {
		registerBeanDefinitions(importingClassMetadata, registry);
	}
	/**
	 * <p>Note that {@link BeanDefinitionRegistryPostProcessor} types may <em>not</em> be
	 * registered here, due to lifecycle constraints related to {@code @Configuration}
	 * class processing.
	 * <p>The default implementation is empty.
	 * @param importingClassMetadata annotation metadata of the importing class
	 * @param registry current bean definition registry
	 */
	default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	}
}

如果@Import引入的是ImportBeanDefinitionRegistrar的子类,将它添加到ConfigurationClass对象的importBeanDefinitionRegistrars集合中,待后面统一进行处理
它的功能主要是:可以非常灵活地手动注册BeanDefinitions信息

3. 引入普通Bean对象

如果是引入的普通Bean对象,则将其看作是Configuration配置类进行处理,递归处理

@Import扩展

Import引入普通Bean对象

在需要的时候,通过注解的形式,在项目中引入SpringUtil工具类

1.自定义注解@EnableSpringUtil

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SpringUtil.class)
public @interface EnableSpringUtil {

}

2.SpringUtil工具类

public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    /**
     * 注入ApplicationContext
     *
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.applicationContext = applicationContext;
    }

在获取到ApplicationContext后,就可以在代码里面获取任意指定的Bean对象了。
@EnableSpringUtil注解作为一个第三方包的注解,我们在需要的项目中,只需要在主类上面写上此注解,就可以优雅地使用SpringUtil工具类了

Import之ImportSelector扩展

通过自定义注解上的参数,选择引入不同的Bean组件

1.自定义注解@EnableXXX

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(XXXSelector.class)
public @interface EnableXXX {
    PersonRole value() default PersonRole.TEACHER;
}

2.实现XXXSelector

public class XXXSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        MergedAnnotations annotations = importingClassMetadata.getAnnotations();
        PersonRole xxValue = annotations.get(EnableXXX.class).getEnum("value", PersonRole.class);

        switch (xxValue) {
            case STUDENT:
                return new String[]{StudentService.class.getName()};
            case TEACHER:
                return new String[]{TeacherService.class.getName()};
            default:
                return new String[]{TeacherService.class.getName()};
        }
    }
}

根据@EnableXXX注解的参数值引入不同的Bean对象

3.使用@EnableXXX

@EnableAspectJAutoProxy
@Configuration
@EnableXXX(PersonRole.TEACHER)
public class XXXConfiguration {

}

在需要使用的地方,写上@EnableXXX,并写上指定的参数就可以使用了

Import之ImportBeanDefinitionRegistrar扩展

通过@EnableCustomComponent注解,将我们指定包里面的,同时被标注了指定注解的类,引入到容器中作为Bean对象

1.自定义注解@EnableCustomComponent

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(CustomImportBeanDefinitionRegistrar.class)
public @interface EnableCustomComponent {
    //需要扫描的包路径
    String[] basePackage();
    //需要扫描的注解
    Class<? extends Annotation>[]  annotations();

}

2.自定义一个需要被扫描到的注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomComponent {

}

如果指定包里面的类,被标注了此注解,这个类就会被扫描到,加入到容器中作为Bean组件

3.实现CustomImportBeanDefinitionRegistrar

public class CustomImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        System.out.println("registerBeanDefinitions2");
        ClassPathBeanDefinitionScanner scanner
                = new ClassPathBeanDefinitionScanner(registry, false);
        MergedAnnotation<EnableCustomComponent> enableCustomComponentMergedAnnotation
                = importingClassMetadata.getAnnotations().get(EnableCustomComponent.class);
        String[] basePackages = enableCustomComponentMergedAnnotation.getStringArray("basePackage");
        Class<?>[] annotations = enableCustomComponentMergedAnnotation.getClassArray("annotations");
        for (String basePackage : basePackages) {
            System.out.println(basePackage);
        }
        for (Class<?> annotation : annotations) {
            scanner.addIncludeFilter(new AnnotationTypeFilter((Class<? extends Annotation>) annotation));
        }
        int scan = scanner.scan(basePackages);
        System.out.println("scan result " + scan);
    }
}

4.使用@EnableCustomComponent注解

@Configuration
@EnableCustomComponent(
        basePackage = "com.mashibing.ycb.importtest.test3.service",
        annotations = {CustomComponent.class, CustomComponent2.class})
public class KKKConfiguration {
}

这样就实现了,通过一个注解,将某一个包里面的被指定注解标注了的类,扫描到Bean容器中来

标签:Spring,ImportBeanDefinitionRegistrar,ImportSelector,Import,注解,Configuration,clas
来源: https://www.cnblogs.com/yanchuanbin/p/14734819.html