Spring注解05 @Import 给容器快速导入一个组件

给容器中注册组件:

一、包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)[自己写的类]
二、@Bean[导入的第三方包里面的组件]
三、*@Import[快速给容器中导入一个组件]
1)、@Import(要导入到容器中的组件);容器中就会自动注册这个组件,id默认是全类名
2)、ImportSelector:返回需要导入的组件的全类名数组;
3)、ImportBeanDefinitionRegistrar:手动注册bean到容器中
四、**使用Spring提供的 FactoryBean(工厂Bean);
1)、默认获取到的是工厂bean调用getObject创建的对象
2)、要获取工厂Bean本身,我们需要给id前面加一个&&colorFactoryBean

配置类 MainConfigImport .java

@Configuration
@ComponentScan(value = "com.tommy", useDefaultFilters = false)
@Import({MyImportSelector.class, MyImportBeanDefinitionRegistrar.class, Yellow.class})
public class MainConfigImport {
    //设置FacotryBean,只能通过这个方式注入,其他的方式会有问题
    @Bean
    public MyBlackFactoryBean myBlackFactoryBean(){
        return  new MyBlackFactoryBean();
    }
}

MyImportSelector.java

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.tommy.bean.Tomcat", "com.tommy.bean.Tomdog"};
    }
}

MyImportBeanDefinitionRegistrar.java

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        final boolean b = registry.containsBeanDefinition("com.tommy.bean.Blue");
        final boolean b2 = registry.containsBeanDefinition("com.tommy.bean.Tomcat");
        if (!b && b2) {
            final BeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(Blue.class);
            registry.registerBeanDefinition("blue", beanDefinition);
        }
    }
}

MyBlackFactoryBean.java

public class MyBlackFactoryBean implements FactoryBean {
    @Override
    public Black getObject() throws Exception {
        System.out.println("MyBlackFactoryBean start new Black() ");
        return new Black();
    }
    @Override
    public Class getObjectType() {
        return Black.class;
    }
    @Override
    public boolean isSingleton() {
        return true;
    }
}

测试类

public class IOCTestImport {
    @Test
    public void testImport() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigImport.class);
        printDefindName(applicationContext);
        final Object blackFactoryBean = applicationContext.getBean("myBlackFactoryBean");
        System.out.println("blackFactoryBean class: " + blackFactoryBean.getClass());
        final Object blackFactoryBean2 = applicationContext.getBean("myBlackFactoryBean");
        System.out.println("blackFactoryBean == blackFactoryBean2: " + (blackFactoryBean == blackFactoryBean2));
        final Object bean3 = applicationContext.getBean("myBlackFactoryBean");
        System.out.println("bean3: "+bean3.getClass());
    }
    private void printDefindName(ApplicationContext applicationContext) {
        final String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
    }
}

测试结果:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfigImport
com.tommy.bean.Tomcat
com.tommy.bean.Tomdog
com.tommy.bean.Yellow
myBlackFactoryBean
blue
MyBlackFactoryBean start new Black() 
blackFactoryBean class: class com.tommy.bean.Black
blackFactoryBean == blackFactoryBean2: true
bean3: class com.tommy.bean.Black

Process finished with exit code 0

你可能感兴趣的:(Spring注解05 @Import 给容器快速导入一个组件)