Spring @ Import explain and use

In the application, we will inject a lot of class to the IOC, it is sometimes not the IOC injected into the container of a class, but not in use when the need to get the class corresponding to the bean, then you need to use @Import comment.

A. @Import use

Introduction 1.1 @Import

  • Directly into the class
  • Manner by introducing configuration class, the configuration at this time or need to achieve ImportSelector ImportBeanDefinitionRegistrar
    NOTE: @Import import components, id for whole class name component

1.2 @Import use

1.2.1 define several Bean

At this time, and is not injected into the vessel 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 injection
@Configuration
@Import({SuperMan.class, SuperSelector.class,MyDefinitionRegistrar.class})
public class MainConfig {
}

MyDefinitionRegistrar, SuperSelector is by way of the configuration file

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 verification

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);
        }
    }
}

FIG follows: when we have used injected into the IOC
Here Insert Picture Description
which we can @Import commented, or open code is commented, running

If you open the code commented out, you will find the errors below occur!
Here Insert Picture Description

II. Extended

@Import in many SpringBoot underlying application, as shown below
Here Insert Picture Description
Next we look at the source code

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

Found implements many interfaces, including DeferredImportSelectorimport selectors, but DeferredImportSelectoralso inherited theImportSelector

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

Share this article temporarily on here, I hope for your help
focus on Java stock to receive more information

contact small series. Micro letter: 372 787 553, takes you into the group to learn from each other
on the left small series of micro letter, to the right to obtain free information
Here Insert Picture Description

Published 78 original articles · won praise 38 · views 9556

Guess you like

Origin blog.csdn.net/weixin_38937840/article/details/104617407