Spring框架的ImportSelector詳細(xì)解讀
前言
最近一直在鉆研Spring源碼,感覺要把自己看吐了,但是看到奧妙的地方還是會(huì)拍手稱快。
這次就總結(jié)一下Spring中一個(gè)非常重要的注解@Import中的ImportSelector接口的作用以及它到底有啥作用。
也會(huì)捎帶一部分源碼說一下DeferredImportSelector是干啥的,以及Spring解析這個(gè)和ImportSelector有什么區(qū)別。
ImportSelector
說到ImportSelector這個(gè)接口就不得不說這里面最重要的一個(gè)方法:selectImports()。
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. * 選擇并返回需要導(dǎo)入的類的名稱,這些類基于AnnotationMetadata * 并且導(dǎo)入到@Configuration注解的類中的 * @return the class names, or an empty array if none * 返回所有的class name,如果沒有,就返回空 */ String[] selectImports(AnnotationMetadata importingClassMetadata); /** * 返回排除的類,是一個(gè)類過濾器,但是這個(gè)方法被default注解了, * 可見Spring公司也知道,這個(gè)基本沒啥人用 */ @Nullable default Predicate<String> getExclusionFilter() { return null; } }
源碼的注解里說了一大堆,就直接說這個(gè)方法能干啥吧。這個(gè)方法的返回值是一個(gè)字符串?dāng)?shù)組,只要在配置類被引用了,這里返回的字符串?dāng)?shù)組中的類名就會(huì)被Spring容器new出來,然后再把這些對(duì)象放到工廠當(dāng)中去。所以這有啥用呢?我們還是用一個(gè)例子演示一下。
ImportSelector簡單例子
首先我們先有一個(gè)實(shí)現(xiàn)了ImportSelector的類MyImportSelect,再構(gòu)造一個(gè)業(yè)務(wù)類IndexDao,然后配置類用@Import引入,最后測試類Test。
/** * 由于我們使用的ImportSelector所以就不需要放到Spring容器當(dāng)中了。 * 我們要用@Import這個(gè)注解引入進(jìn)去。 */ public class MyImportSelect implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{IndexDao.class.getName()}; } }
@ComponentScan("com.demo") @Import(MyImportSelect.class) public class AppConfig { }
public class IndexDao { public void query(){ System.out.println("query IndexDao for MyImportSelect"); } }
public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext anno=new AnnotationConfigApplicationContext(AppConfig.class); anno.getBean(IndexDao.class).query(); } } 運(yùn)行打印 query IndexDao for MyImportSelect
從上面的例子看,盡管程序上沒有把MyImportSelect類放到Spring容器中,也沒有把IndexDao放到Spring容器中,但是在測試上就可以把IndexDao從容器中拿出來,并且正常執(zhí)行。
不知道大家看到這里有什么感覺,到這里我其實(shí)是有疑問的。我這么做有個(gè)卵用噻。我是有病吧,直接把類上加個(gè)@Component注冊(cè)進(jìn)去不香嗎?所以這個(gè)ImportSelector把程序搞這么復(fù)雜是有毛病吧,把簡單的功能搞這么復(fù)雜。
ImportSelector真正的作用
其實(shí)Spring公司既然這么設(shè)計(jì),那肯定是有用的。那么有什么用呢?設(shè)想這樣一個(gè)場景,如果有些功能我們并不需要Spring在一開始就加載進(jìn)去,而是需要Spring幫助我們把這些功能動(dòng)態(tài)加載進(jìn)去,這時(shí)候這個(gè)ImportSelector的作用就來了。我們完全可以把實(shí)現(xiàn)這個(gè)接口的類做成一個(gè)開關(guān),用來開啟或者關(guān)閉某一個(gè)或者某些功能類。
比如說我們上面的例子IndexDao,假設(shè)這個(gè)IndexDao實(shí)現(xiàn)的功能是一個(gè)擴(kuò)展功能,在正式的生產(chǎn)上不一定用的到。如果說一個(gè)包下有100個(gè)類,那么使用掃描去屏蔽這個(gè)類就很麻煩,但是屏蔽這個(gè)類用ImportSelector去做就很容易了。下次如果需要用到了,我再放開這個(gè)開關(guān),直接可以使用IndexDao的功能了,這樣就做到了一個(gè)靈活的功能掌控。
再舉一個(gè)實(shí)際的用例,假設(shè)我們的IndexDao不是打印而是返回一個(gè)針對(duì)代理IndexDao3的代理對(duì)象,比如輸出一個(gè)log。但是這個(gè)方法我不一定會(huì)用到,因?yàn)橹挥行枰玫酱淼臅r(shí)候,這個(gè)方法才有意義。我不需要去代理,這里的代碼就不要運(yùn)行。只有給一個(gè)顯示的通知,我這個(gè)代理才會(huì)去執(zhí)行。
public class IndexDao { public void query(){ System.out.println("log for IndexDao3"); return Proxy.newProxyInstance(IndexDao3);//偽碼 } }
看到這里,大家有沒有聯(lián)想到SpringAOP其實(shí)就是這個(gè)樣子。能夠做到動(dòng)態(tài)加載與卸載,與我們的程序沒有什么耦合關(guān)系。怎么才能實(shí)現(xiàn)這個(gè)所謂的動(dòng)態(tài)開啟呢?
ImportSelector開關(guān)
為了完成這個(gè)開關(guān),我們也模仿Spring寫一個(gè)EnableMySelector的注解,然后@Import我們自己的ImportSelector接口類。
@Retention(RetentionPolicy.RUNTIME) //開啟運(yùn)行時(shí)加載 @Import(MyImportSelect.class) public @interface EnableMySelector { }
做好了這一步在AppConfig這個(gè)配置里面就可以直接使用這個(gè)@ EnableMySelector自定義注解去開關(guān)一個(gè)類了。
@ComponentScan("com.demo") @EnableMySelector public class AppConfig { } 運(yùn)行,一樣打印 query IndexDao for MyImportSelect
講道理其實(shí)Spring中那么多的EnableXXXX的注解底層就是這樣的原理。到此還有誰敢說ImportSelector用處?。?/p>
連帶說一下DeferredImportSelector
這個(gè)是看Spring源碼的時(shí)候發(fā)現(xiàn)的,直接翻譯就是延時(shí)加載ImportSelector,實(shí)現(xiàn)這個(gè)接口的類,將會(huì)在@Configuration后面被加載,用法什么的和ImportSelector功能基本一樣。因?yàn)橛玫谋容^稀有就不多做解釋了,僅僅作為一個(gè)只是擴(kuò)展點(diǎn)介紹下。在ConfigurationClassParser中會(huì)有一個(gè)判斷,是不是這個(gè)接口,如果是就會(huì)放到后面解析。以下摘自源碼:
org.springframework.context.annotation.ConfigurationClassParser#processImports //這里攔截了DeferredImportSelector然后使用handle() if (selector instanceof DeferredImportSelector) { this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector); }
進(jìn)入handle()方法,發(fā)現(xiàn)和這個(gè)接口相關(guān)的都被加入了一個(gè)deferredImportSelectors的list中。
public void handle(ConfigurationClass configClass, DeferredImportSelector importSelector) { DeferredImportSelectorHolder holder = new DeferredImportSelectorHolder(configClass, importSelector); if (this.deferredImportSelectors == null) { DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler(); handler.register(holder); handler.processGroupImports(); } else { //加入到了一個(gè)ArrayList中 this.deferredImportSelectors.add(holder); } }
最終這個(gè)ArrayList在parse()方法的最后被處理了
org.springframework.context.annotation.ConfigurationClassParser#parse(java.util.Set<org.springframework.beans.factory.config.BeanDefinitionHolder>) public void parse(Set<BeanDefinitionHolder> configCandidates) { //根據(jù)BeanDefinition的類型做不同的處理,一般都會(huì)調(diào)用ConfigurationClassParser.parse()進(jìn)行解析 for (BeanDefinitionHolder holder : configCandidates) { BeanDefinition bd = holder.getBeanDefinition(); //拿出BeanDefinition try { if (bd instanceof AnnotatedBeanDefinition) { //判斷是不是加了注解的 // 解析注解對(duì)象,并且把解析出來的bd方法map中,但是這里的bd指的的普通的 // 普通和不普通的怎么區(qū)分。比如@Bean和各種beanFactoryPostProcessor得到的bean //如果被加了注解,又調(diào)用了一個(gè)parse()方法 parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName()); } else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) { parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName()); } else { parse(bd.getBeanClassName(), holder.getBeanName()); } } catch (BeanDefinitionStoreException ex) { throw ex; } catch (Throwable ex) { throw new BeanDefinitionStoreException( "Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex); } } //處理,而此時(shí)上面其他的Import已經(jīng)處理完了 this.deferredImportSelectorHandler.process(); }
到此這篇關(guān)于Spring框架的ImportSelector詳細(xì)解讀的文章就介紹到這了,更多相關(guān)ImportSelector詳細(xì)解讀內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot中使用Server-Sent Events (SSE) 實(shí)
Server-Sent Events (SSE) 是HTML5引入的一種輕量級(jí)的服務(wù)器向?yàn)g覽器客戶端單向推送實(shí)時(shí)數(shù)據(jù)的技術(shù),本文主要介紹了Spring Boot中使用Server-Sent Events (SSE) 實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)推送教程,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03

mybatis update set 多個(gè)字段實(shí)例

Spring整合Mybatis具體代碼實(shí)現(xiàn)流程

Java實(shí)現(xiàn)迅雷地址轉(zhuǎn)成普通地址實(shí)例代碼

注冊(cè)中心配置了spring?security后客戶端啟動(dòng)報(bào)錯(cuò)