Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of spring annotation @ Import

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report