Spring@Import詳解與使用

在應用中,我們會把大量的類注入到IOC中,有時沒有把某個類注入到IOC容器中,但在運用的時候需要獲取該類對應的bean,此時就需要用到@Import註解。

一. @Import 使用

1.1 @Import 介紹

  • 直接導入類
  • 通過導入配置類的方式,此時的配置類需要實現 ImportSelector 或者 ImportBeanDefinitionRegistrar
    注: @Import導入組件,id默認是組件的全類名

1.2 @Import 使用

1.2.1 定義幾個Bean

此時並沒有注入到 IOC 容器中

@Data
public class SuperMan {
    private String name;
    private Integer age;
}
---------
@Data
public class Man {
    private String userName;
    private String email;
}
--------
@Data
public class Bule {
    private Integer code;
}
1.2.2 使用@Import注入
@Configuration
@Import({SuperMan.class, SuperSelector.class,MyDefinitionRegistrar.class})
public class MainConfig {
}

MyDefinitionRegistrar、 SuperSelector是通過配置文件的方式

public class MyDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
                                        BeanDefinitionRegistry beanDefinitionRegistry) {
        boolean beanDefinition = beanDefinitionRegistry.containsBeanDefinition("com.javayh.api.bean.SuperMan");
        if(beanDefinition){
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Bule.class);
            beanDefinitionRegistry.registerBeanDefinition("bule",rootBeanDefinition);
//            beanDefinitionRegistry.removeBeanDefinition("com.javayh.api.bean.Man");
        }

    }
}
public class SuperSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {

        return new String[]{
                "com.javayh.api.bean.Man"
        };
    }
}

1.3 驗證

public class TestMain {
    AnnotationConfigApplicationContext applicationContext =
            new AnnotationConfigApplicationContext(MainConfig.class);

    @Test
    public void testImport(){
        printBeans(applicationContext);
        SuperMan bean = applicationContext.getBean(SuperMan.class);
        Man man = applicationContext.getBean(Man.class);
        Bule bule = applicationContext.getBean(Bule.class);
        System.out.println(bean);
        System.out.println(man);
        System.out.println(bule);
    }


    private void printBeans(AnnotationConfigApplicationContext applicationContext){
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
    }
}

如下圖:在我們使用的時候已經將其注入到IOC中
在這裏插入圖片描述
這是我們可以 將 @Import 註釋掉,或者打開註釋掉的代碼,在運行

如果您打開註釋掉的代碼,會發現,出現下方的錯誤!
在這裏插入圖片描述

二.擴展

@Import在SpringBoot底層應用的很多,如下圖
在這裏插入圖片描述
接下來我們看一下源代碼

public class AutoConfigurationImportSelector 
	implements DeferredImportSelector, 
	BeanClassLoaderAware, ResourceLoaderAware, 
	BeanFactoryAware, EnvironmentAware, Ordered {
 
}

發現實現了很多接口,其中有 DeferredImportSelector 導入選擇器,而DeferredImportSelector 又繼承了ImportSelector

public interface ImportSelector {
    String[] selectImports(AnnotationMetadata var1);
}
 

本文的分享暫時就到這裏,希望對您有所幫助
關注 Java有貨領取更多資料

聯繫小編。微信:372787553,帶您進羣互相學習
左側小編微信,右側獲取免費資料
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章