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
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!
II. Extended
@Import in many SpringBoot underlying application, as shown below
Next we look at the source code
public class AutoConfigurationImportSelector
implements DeferredImportSelector,
BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware, Ordered {
}
Found implements many interfaces, including DeferredImportSelector
import selectors, but DeferredImportSelector
also 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
- SpringCloud custom schema package https://github.com/Dylan-haiji/javayh-platform
- Java design patterns learning the code https://github.com/Dylan-haiji/design-pattern
- SpringCloud learning Code: https://github.com/Dylan-haiji/javayh-cloud
- AlibabaCloud learning Code: https://github.com/Dylan-haiji/javayh-cloud-nacos
- SpringBoot + Mybatis switching multiple data sources: https://github.com/Dylan-haiji/javayh-boot-data-soure
- Redis, Mongo, Rabbitmq, Kafka learning Code: https://github.com/Dylan-haiji/javayh-middleware
- SpringBoot + SpringSecurity implement custom learning Login Code: https://github.com/Dylan-haiji/javayh-distribution