【1】@Import
The parameter value receives a Class array, and adds the class you passed in to the IOC container with the full class name as the id.
Simple, no detailed explanation here
【2】ImportSelector
ImportSelector emphasizes reusability. Using it requires creating a class to implement the ImportSelector interface. The return value of the implementation method is a string array, which means the full class name of the component that needs to be injected into the container. The id is also the full class name.
On the code:
//Custom logic returns the components that need to be importedpublic class MyImportSelector implements ImportSelector { // The return value is the full class name of the component imported into the container // AnnotationMetadata: All annotation information of the class currently annotated @Import annotation public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[] { "", "", "" }; } }
【3】ImportBeanDefinitionRegistrar
This is how spring officially implements the dynamic injection mechanism of annotations such as @Component and @Service. Define an ImportBeanDefinitionRegistrar implementation class, and then use @Import to import on the configuration class with @Configuration annotation
Specific use: Create an ImportBeanDefinitionRegistrar implementation class, implement registerBeanDefinitions method, and inject components.
public class MyBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { /** * @param annotationMetadata All annotation information of the class currently annotated @Import annotation * @param beanDefinitionRegistry BeanDefinition registration class */ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) { RootBeanDefinition beanDefinition = new RootBeanDefinition(); ("cat",beanDefinition); } }
Configuration class MyConfig:
@Configuration @Import(value = {}) public class MyConfig { } /**Test results beanName: beanName: beanName: beanName: beanName: beanName: myConfig beanName: cat */
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.