006---@Import註解 直接導入組件、使用ImportSelector接口 和 使用 ImportBeanDefinitionRegistrar接口 的三種@Import方式(方式三)

導入組件的方法有

方式一:自己編寫的類 加入了@Controller、@Service、@Repository、@Component註解的類

方式二:通過@Bean註解導入第三方類

方式三:通過@Import直接導入第三方類(默認在Ioc容器內的名稱就是全類名(包名+類名))

示例:

@Import的使用方式一

組件:下面的構造方法我加入了一條輸出語句

package bean;

public class HelloWorld {
    String hello="Hello demo";

    public HelloWorld() {
        super();
        System.out.println("導入成功");
    }

    @Override
    public String toString() {
        return "HelloWorld{" +
                "hello='" + hello + '\'' +
                '}';
    }
}

spring配置類,原先通過@Bean註解導入現在直接通過@Import導入,如果輸出了上面的那條“導入成功”說明導入了IOC容器中 

package config;

import bean.HelloWorld;
import org.springframework.context.annotation.*;


@Configuration
@Import(HelloWorld.class)
public class Config {

//    @Scope("prototype")
//    @Conditional({DemoCondition.class})
//    @Bean("hello")
//    public HelloWorld hello() {
//        System.out.println("bean加入ioc容器");
//        return new HelloWorld();
//    }
}

測試類

package config;

import bean.HelloWorld;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


import static org.junit.Assert.*;

public class ConfigTest {

    @Test
    public void test1(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
    }
}

運行結果:

@Import註解的使用方式二 

 實現ImportSelector接口,

package config;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

public class DemoImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{"全類名組件1","全類名組件2"};
    }
}

使用方式和前面差不多:

@Import({HelloWorld.class,DemoImportSelector.class})

其中組件的全類名要記得修改!替換其中的中文

@Import的使用方式三,實現ImportBeanDefinitionRegistrar接口

package config;

import bean.HelloWorld;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class DemoImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        boolean b1 = beanDefinitionRegistry.containsBeanDefinition("IOC容器中的組件名1");
        boolean b2 = beanDefinitionRegistry.containsBeanDefinition("IOC容器中的組件名2");
        if (b1 && b2){
            //組件1和組件2都存在則添加新組件
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(HelloWorld.class);
            beanDefinitionRegistry.registerBeanDefinition("HelloWorld",rootBeanDefinition);
        }
    }
}

使用方式和前面import一樣

@Import({HelloWorld.class,DemoImportBeanDefinitionRegistrar.class})

 

發佈了259 篇原創文章 · 獲贊 15 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章