天天看點

【Spring注解驅動開發】深入了解Spring的ImportSelector接口

ImportSelector接口概述

ImportSelector接口是至spring中導入外部配置的核心接口,在SpringBoot的自動化配置和@EnableXXX(功能性注解)都有它的存在。我們先來看一下ImportSelector接口的源碼,如下所示。

package org.springframework.context.annotation;
import java.util.function.Predicate;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.Nullable;
public interface ImportSelector {
    String[] selectImports(AnnotationMetadata importingClassMetadata);
    @Nullable
    default Predicate<String> getExclusionFilter() {
        return null;
    }
}      

該接口文檔上說的明明白白,其主要作用是收集需要導入的配置類,selectImports()方法的傳回值就是我們向Spring容器中導入的類的全類名。如果該接口的實作類同時實作EnvironmentAware, BeanFactoryAware  ,BeanClassLoaderAware或者ResourceLoaderAware,那麼在調用其selectImports方法之前先調用上述接口中對應的方法,如果需要在所有的@Configuration處理完在導入時可以實作DeferredImportSelector接口。

在ImportSelector接口的selectImports()方法中,存在一個AnnotationMetadata類型的參數,這個參數能夠擷取到目前标注@Import注解的類的所有注解資訊。

ImportSelector接口探秘

在這裡我舉個Spring中的執行個體來看一下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {
    boolean proxyTargetClass() default false;
    AdviceMode mode() default AdviceMode.PROXY;
    int order() default Ordered.LOWEST_PRECEDENCE;
}      

此注解是開啟聲明式事務的注解,那麼它的@Import所導入的類為TransactionManagementConfigurationSelector,那麼我們看一下其類圖:

【Spring注解驅動開發】深入了解Spring的ImportSelector接口

由此可知該類實作類ImportSelector接口。

前面說過,在SpringBoot的自動化配置和@EnableXXX(功能性注解)都有ImportSelector接口的存在,那我們就來自己定義一個@EnableXXX注解來更加深刻的了解ImportSelector接口。

【Spring注解驅動開發】深入了解Spring的ImportSelector接口
自定義@EnableXXX注解

在這裡我們先準備兩個Spring的項目工程:spring-project與ssm-project,其中spring-project裡我們先建立好如下結構目錄:

建立實體類

package org.hzgj.spring.study.bean
public class StudentBean{
    private Integer id;
    private String name;
    //省略setter和gettter
}      

建立ImportSelector接口的實作類

package org.hzgj.spring.study.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class SpringStudySelector implements ImportSelector, BeanFactoryAware {
    private BeanFactory beanFactory;
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        importingClassMetadata.getAnnotationTypes().forEach(System.out::println);
        System.out.println(beanFactory);
        return new String[]{AppConfig.class.getName()};
    }
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}      

在這裡我們實作ImportSelector接口和BeanFactoryAware接口,重寫selectImports方法,最後我們傳回的是AppConfig的類名,同時列印出相關的注解中繼資料與BeanFactory

自定義@EnableSpringStudy注解

package org.hzgj.spring.study.annotation;
import org.hzgj.spring.study.config.SpringStudySelector;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
@Import(SpringStudySelector.class)
public @interface EnableSpringStudy {
}      

在這裡我們仿照@EnableTransactionManagement來實作自定義注解,注意使用@Import導入我們剛才寫的SpringStudySelector。

建立配置類

package org.hzgj.spring.study.config;
import org.hzgj.spring.study.bean.StudentBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
    @Bean
    public StudentBean studentBean() {
        StudentBean studentBean = new StudentBean();
        studentBean.setId(19);
        studentBean.setName("admin");
        return studentBean;
    }
}      

當都完成以後我們打個jar包,準備引入至其他工程:

使用自定義@EnableXXX注解

完成ssm-project工程中的AppConfig配置類

1) 首先我們将剛才的spring.jar導入到ssm-project工程裡

2) 在對應的配置類上添加上spring-project中定義的@EnableSpringStudy注解

@Configuration //表明此類是配置類
@ComponentScan // 掃描自定義的元件(repository service component controller)
@PropertySource("classpath:application.properties") // 讀取application.properties
@MapperScan("com.bdqn.lyrk.ssm.study.app.mapper") //掃描Mybatis的Mapper接口
@EnableTransactionManagement //開啟事務管理
@EnableSpringStudy
public class AppConfig {
  //....省略配置代碼      
}      

3)編寫Main方法

public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        StudentBean studentBean = applicationContext.getBean(StudentBean.class);
        System.out.println(studentBean.getName());
}      

運作後輸出結果:

org.springframework.context.annotation.Configuration
org.springframework.context.annotation.ComponentScan
org.springframework.context.annotation.PropertySource
org.mybatis.spring.annotation.MapperScan
org.springframework.transaction.annotation.EnableTransactionManagement
org.hzgj.spring.study.annotation.EnableSpringStudy
org.springframework.beans.factory.support.DefaultListableBeanFactory@4b9e13df: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,appConfig,propertiesConfig,logAspect,studentService]; root of factory hierarchy
admin      

從這裡我們可以看到ImportSelector接口中的方法參數,可以擷取ssm-project項目下AppConfig的所有注解,并且能夠擷取目前BeanFactory所有配置的Bean。

ImportSelector源碼分析

這個接口在哪裡調用呢?我們可以來看一下ConfigurationClassParser這個類的processImports方法。

private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass,
                            Collection<SourceClass> importCandidates, boolean checkForCircularImports) {
    if (importCandidates.isEmpty()) {
        return;
    }
    if (checkForCircularImports && isChainedImportOnStack(configClass)) {
        this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));
    }
    else {
        this.importStack.push(configClass);
        try {
            for (SourceClass candidate : importCandidates) {            //對ImportSelector的處理
                if (candidate.isAssignable(ImportSelector.class)) {
                    // Candidate class is an ImportSelector -> delegate to it to determine imports
                    Class<?> candidateClass = candidate.loadClass();
                    ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class);
                    ParserStrategyUtils.invokeAwareMethods(
                        selector, this.environment, this.resourceLoader, this.registry);
                    if (this.deferredImportSelectors != null && selector instanceof DeferredImportSelector) {                //如果為延遲導入處理則加入集合當中
                        this.deferredImportSelectors.add(
                            new DeferredImportSelectorHolder(configClass, (DeferredImportSelector) selector));
                    }
                    else {                //根據ImportSelector方法的傳回值來進行遞歸操作
                        String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());
                        Collection<SourceClass> importSourceClasses = asSourceClasses(importClassNames);
                        processImports(configClass, currentSourceClass, importSourceClasses, false);
                    }
                }
                else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
                    // Candidate class is an ImportBeanDefinitionRegistrar ->
                    // delegate to it to register additional bean definitions
                    Class<?> candidateClass = candidate.loadClass();
                    ImportBeanDefinitionRegistrar registrar =
                        BeanUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class);
                    ParserStrategyUtils.invokeAwareMethods(
                        registrar, this.environment, this.resourceLoader, this.registry);
                    configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata());
                }
                else {              // 如果目前的類既不是ImportSelector也不是ImportBeanDefinitionRegistar就進行@Configuration的解析處理
                    // Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar ->
                    // process it as an @Configuration class
                    this.importStack.registerImport(
                        currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
                    processConfigurationClass(candidate.asConfigClass(configClass));
                }
            }
        }
        catch (BeanDefinitionStoreException ex) {
            throw ex;
        }
        catch (Throwable ex) {
            throw new BeanDefinitionStoreException(
                "Failed to process import candidates for configuration class [" +
                configClass.getMetadata().getClassName() + "]", ex);
        }
        finally {
            this.importStack.pop();
        }
    }
}      

在這裡我們可以看到ImportSelector接口的傳回值會遞歸進行解析,把解析到