In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Previously on
BeanPostProcessor of the Spring extension interface, let's take a look at its underlying implementation of spring's underlying support for BeanPostProcessor, and know the two methods of BeanPostProcessor: postProcessBeforeInitialization and postProcessAfterInitialization. If you don't see it, you can look back. Originally, the automatic assembly of spring was intended to be explained in detail in the last blog post, but then I felt that the space might be too large, since you all felt it, then I didn't speak in secret. The reason why I didn't put it into the last article was because it was too large. All right, let's get back to the point. the reason why we didn't put it in the first part is that the space is just the reason.
First, the main reason is to find out that I made a mistake! What mistake did you make?
I take it for granted! Taking it for granted that auto-assembly is implemented in AutowiredAnnotationBeanPostProcessor's postProcessBeforeInitialization or postProcessAfterInitialization, let's take a look at the AutowiredAnnotationBeanPostProcessor class inheritance diagram
Cdn.xitu.io/2019/9/18/16d4477ee7effa62?imageView2/0/w/1280/h/960/format/webp/ignore-error/1 ">
It implements BeanPostProcessor indirectly, so let's take a look at those two methods (in the parent class InstantiationAwareBeanPostProcessorAdapter)
@ Override public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException {return bean;} @ Override public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException {return bean;} copy the code
I didn't do anything, but a simple return bean;. When my deep belief was overturned, it felt like killing the dog.
So automatic assembly can not be put together with BeanPostProcessor, we have to open two separate articles, we all know: strong twist is not sweet!
Simple example of automatic assembly
Let's first take a look at a simple example of automatic assembly, the complete example code: spring-boot-BeanPostProcessor
AnimalServiceImpl
@ Service public class AnimalServiceImpl implements IAnimalService {@ Autowired private Dog dog; @ Resource private Cat cat; @ Inject private Pig pig; @ Override public void printName () {System.out.println (dog.getName ()); System.out.println (cat.getName ()); System.out.println (pig.getName ());} copy the code
AnimalTest
@ RunWith (SpringRunner.class) @ SpringBootTest (classes= {Application.class}) public class AnimalTest {@ Autowired private IAnimalService animalService; @ Test public void test () {animalService.printName ()}} copy the code
Running result
We only register the instances of Dog, Cat and Pig in AnimalConfig with the spring container, so why can we apply these instances directly to AnimalServiceImpl instances? we do not manually assign these instances to AnimalServiceImpl instances? In fact, this is the automatic assembly function provided by spring. Although we do not manually assign these instances to AnimalServiceImpl instances, we find that there are some annotations on the attribute instances of AnimalServiceImpl: @ Autowired, @ Resource, @ Inject,spring automatically complete the injection of attribute instances through these annotations, without the need for us to manually assign values; so how does spring achieve automatic assembly? Let's move on (note: the following article will mainly take @ Autowired as an example)
Automatic assembly source code analysis
Instantiation and Registration of AutowiredAnnotationBeanPostProcessor
Anyway, AutowiredAnnotationBeanPostProcessor is still a BeanPostProcessor after all, so its instantiation and registration (beanFactory registered to spring) process is the same as BeanPostProcessor instantiation and registration. During the startup of spring, when the context (refresh) is refreshed, the registerBeanPostProcessors (beanFactory) method will be called to complete the instantiation and registration of BeanPostProcessor, and the above registered BeanPostProcessor will be used when finishBeanFactoryInitialization (beanFactory) is called later to instantiate the singleton bean with non-delayed loading.
The construction method of AutowiredAnnotationBeanPostProcessor is worth seeing.
Public AutowiredAnnotationBeanPostProcessor () {this.autowiredAnnotationTypes.add (Autowired.class); this.autowiredAnnotationTypes.add (Value.class); try {this.autowiredAnnotationTypes.add ((Class)
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.