In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the principle of @ Autowired,@Resource and @ Inject implementation of Spring". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the principle of @ Autowired,@Resource and @ Inject implementation of Spring"?
First, the use of spring dependency injection
@ Autowired is an annotation provided by the spring framework to implement dependency injection. It mainly supports bean injection in the set method, field, and constructor. The injection method is to find bean by type, that is, byType. If there are multiple bean of the same type, use @ Qualifier to specify the bean of which beanName to inject. The difference from JDK's @ Resource: @ Resource is based on the name of bean, that is, beanName, to find bean injection from spring's IOC container, while @ Autowried is based on the type byType to find bean injection. The difference from JDK's @ Inject: @ Inject also looks for bean injection based on type. If you need to specify the name beanName, you can use the @ Named annotation in conjunction with the @ Autowired annotation to specify the name beanName.
2. The implementation principle of spring dependency injection annotation.
Annotation processor
In the internal implementation of the spring framework, annotation implementation injection takes effect mainly through the implementation class of the BeanPostProcessor interface of the bean post processor. The BeanPostProcessor post processor executes immediately after the bean object instance is created when the spring container starts, and processes the bean object instance. @ Autowired implements dependency injection of bean objects on other bean objects through the implementation class AutowiredAnnotationBeanPostProcessor of the BeanPostProcessor interface; @ Resource and @ Inject are implemented through the implementation class CommonAnnotationBeanPostProcessor of the BeanPostProcessor interface, where, as the name suggests, the common annotation CommonAnotation,CommonAnnotationBeanPostProcessor is a BeanPostProcessor in spring that uniformly handles annotations defined in JDK. Other annotations that this class handles include @ PostConstruct,@PreDestroy and so on.
Activation conditions of the annotation processor
The conditions under which AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor are added to the BeanPostProcessor of the spring container, that is, the conditions for activating these processors are as follows:
1. Spring configuration based on xml
In the configuration xml file of the corresponding spring container, such as applicationContext.xml, add and, or use only. The difference between the two is that only the existing bean is found and activated, for example, the bean loaded into the spring container is generated through the bean tag of the xml file, instead of scanning the bean annotated such as @ Controller, finding it and injecting it; in addition to the functions, it also loads the class under the package specified by the basePackages attribute, which is scanned by @ Controller,@Service,@Component,@Repository annotation by default. If you do not specify basePackages, it is under the classpath, or if you use the annotation @ ComponentScan, it is under the package of the current class and its subpackages.
two。 Spring configuration based on configuration Class
If spring is configured based on a configuration class rather than an applicationContext.xml, such as SpringBoot, the IOC container used internally is implemented as AnnotationConfigApplicationContext or its derived classes, and the above BeanPostProcessor is automatically created and activated within AnnotationConfigApplicationContext. If there is both xml-based configuration and configuration class configuration, and in terms of injection time, annotation-based injection precedes XML-based injection, so XML-based injection overrides annotation-based injection.
At this point, I believe you have a deeper understanding of "what is the principle of @ Autowired,@Resource and @ Inject implementation of Spring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.