深入理解Spring的ImportSelector接口

ImportSelector接口是至spring中導入外部配置的核心接口轧拄,在SpringBoot的自動化配置和@EnableXXX(功能性注解)都有它的存在,關于SpringBoot的分析可以參考:深入理解SpringBoot的自動裝配群扶。

一檩互、關于ImportSelector接口

package org.springframework.context.annotation; import org.springframework.core.type.AnnotationMetadata; /** * Interface to be implemented by types that determine which @{@link Configuration}
 * class(es) should be imported based on a given selection criteria, usually one or more
 * annotation attributes.
 *
 * <p>An {@link ImportSelector} may implement any of the following
 * {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective
 * methods will be called prior to {@link #selectImports}:
 * <ul>
 * <li>{@link org.springframework.context.EnvironmentAware EnvironmentAware}</li>
 * <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}</li>
 * <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}</li>
 * <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}</li>
 * </ul>
 *
 * <p>ImportSelectors are usually processed in the same way as regular {@code @Import}
 * annotations, however, it is also possible to defer selection of imports until all
 * {@code @Configuration} classes have been processed (see {@link DeferredImportSelector}
 * for details).
 *
 * @author Chris Beams
 * @since 3.1
 * @see DeferredImportSelector
 * @see Import
 * @see ImportBeanDefinitionRegistrar
 * @see Configuration */
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. */ String[] selectImports(AnnotationMetadata importingClassMetadata);

}

該接口文檔上說的明明白白盯捌,其主要作用是收集需要導入的配置類一也,如果該接口的實現類同時實現EnvironmentAware, BeanFactoryAware 蓬戚,BeanClassLoaderAware或者ResourceLoaderAware夸楣,那么在調用其selectImports方法之前先調用上述接口中對應的方法,如果需要在所有的@Configuration處理完在導入時可以實現DeferredImportSelector接口子漩。

在這里我舉個Spring中的實例來看一下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class) public @interface EnableTransactionManagement { /** * Indicate whether subclass-based (CGLIB) proxies are to be created ({@code true}) as
     * opposed to standard Java interface-based proxies ({@code false}). The default is
     * {@code false}. <strong>Applicable only if {@link #mode()} is set to
     * {@link AdviceMode#PROXY}</strong>.
     * <p>Note that setting this attribute to {@code true} will affect <em>all</em>
     * Spring-managed beans requiring proxying, not just those marked with
     * {@code @Transactional}. For example, other beans marked with Spring's
     * {@code @Async} annotation will be upgraded to subclass proxying at the same
     * time. This approach has no negative impact in practice unless one is explicitly
     * expecting one type of proxy vs another, e.g. in tests. */
    boolean proxyTargetClass() default false; /** * Indicate how transactional advice should be applied. The default is
     * {@link AdviceMode#PROXY}.
     * @see AdviceMode */ AdviceMode mode() default AdviceMode.PROXY; /** * Indicate the ordering of the execution of the transaction advisor
     * when multiple advices are applied at a specific joinpoint.
     * The default is {@link Ordered#LOWEST_PRECEDENCE}. */
    int order() default Ordered.LOWEST_PRECEDENCE;

}

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

image

由此可知該類實現類ImportSelector接口

二幢泼、自定義@EnableXXX注解

在這里我們先準備兩個Spring的項目工程:spring-project與ssm-project紧显,其中spring-project里我們先創(chuàng)建好如下結構目錄:

image

SpringStudySelector

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

AppConfig

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包招驴,準備引入至其他工程:

image

完成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接口的返回值會遞歸進行解析,把解析到的類全名按照@Configuration進行處理

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末触趴,一起剝皮案震驚了整個濱河市氮发,隨后出現的幾起案子,更是在濱河造成了極大的恐慌冗懦,老刑警劉巖爽冕,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異披蕉,居然都是意外死亡扇售,警方通過查閱死者的電腦和手機前塔,發(fā)現死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來承冰,“玉大人,你說我怎么就攤上這事食零±梗” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵贰谣,是天一觀的道長娜搂。 經常有香客問我,道長吱抚,這世上最難降的妖魔是什么百宇? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮秘豹,結果婚禮上携御,老公的妹妹穿的比我還像新娘。我一直安慰自己既绕,他們只是感情好啄刹,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著凄贩,像睡著了一般誓军。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上疲扎,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天昵时,我揣著相機與錄音,去河邊找鬼椒丧。 笑死壹甥,一個胖子當著我的面吹牛,可吹牛的內容都是我干的瓜挽。 我是一名探鬼主播盹廷,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼久橙!你這毒婦竟也來了俄占?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤淆衷,失蹤者是張志新(化名)和其女友劉穎缸榄,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體祝拯,經...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡甚带,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年她肯,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鹰贵。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡晴氨,死狀恐怖,靈堂內的尸體忽然破棺而出碉输,到底是詐尸還是另有隱情籽前,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布敷钾,位于F島的核電站枝哄,受9級特大地震影響,放射性物質發(fā)生泄漏阻荒。R本人自食惡果不足惜挠锥,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望侨赡。 院中可真熱鬧蓖租,春花似錦、人聲如沸辆毡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽舶掖。三九已至球昨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間眨攘,已是汗流浹背主慰。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留鲫售,地道東北人共螺。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像情竹,于是被迫代替她去往敵國和親藐不。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355