Get the App
SLTechnology News&Howtos  ›  Development  › 

The usage of spring annotation @ Import

Shulou Source: shulou.com Published: 2022-06-03 14:06:09 10月31日 Update

This article mainly introduces "the usage of spring annotation @ Import". In daily operation, I believe many people have doubts about the usage of spring annotation @ Import. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the usage of spring annotation @ Import! Next, please follow the editor to study!

[1] @ Import

The parameter value receives an array of Class and adds the class you passed in as the full class name as id to the IOC container

It's relatively simple. I won't explain it in detail here.

[2] ImportSelector

ImportSelector emphasizes reusability. To use it, you need to create a class to implement the ImportSelector interface, and the return value of the implementation method is an array of strings, that is, the full class name of the component that needs to be injected into the container. Id is also a full class name.

The above code:

/ / the custom logic returns the component to be imported public class MyImportSelector implements ImportSelector {/ / the return value is the full class name of the component imported into the container / / AnnotationMetadata: all the annotation information of the class annotated with @ Import currently public String [] selectImports (AnnotationMetadata importingClassMetadata) {return new String [] {"test.spring.ZhangSan", "test.spring.LiSi", "test.spring.WangWu"};}}

[3] ImportBeanDefinitionRegistrar

It is in this way that spring officials implement the dynamic injection mechanism for annotations such as @ Component, @ Service, and so on. Define an implementation class for ImportBeanDefinitionRegistrar, and then use @ Import import on the configuration class annotated with @ Configuration

Specific use: create an implementation class of ImportBeanDefinitionRegistrar, implement the registerBeanDefinitions method, and inject the component.

Public class MyBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {/ * @ param annotationMetadata all annotation information of the class currently annotated with @ Import * @ param beanDefinitionRegistry BeanDefinition registered class * / public void registerBeanDefinitions (AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {RootBeanDefinition beanDefinition = new RootBeanDefinition (Cat.class); beanDefinitionRegistry.registerBeanDefinition ("cat", beanDefinition);}}

Configuration class MyConfig:

@ Configuration@Import (value = {MyBeanDefinitionRegistrar.class}) public class MyConfig {} / * * Test results beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessorbeanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessorbeanName: org.springframework.context.annotation.internalCommonAnnotationProcessorbeanName: org.springframework.context.event.internalEventListenerProcessorbeanName: org.springframework.context.event.internalEventListenerFactorybeanName: myConfigbeanName: cat*/

At this point, the study of "the usage of spring annotations @ Import" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Annotations class names components learning containers methods information that is arrays more help configuration utility and then the previous generation that is dynamic parameters characters strings Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn Shulou Technology Shulou Information Apple