In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the principle and function of Spring Aware". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the principle and function of Spring Aware"?
Why do you talk about Spring Bean life cycle and Aware? Come down and light up the rest and you may understand:
The origin before Spring Bean Ready for Use is, of course, to call the constructor, so Constructor is undoubtedly the first step in creating a Spring Bean
Dependency injection is accomplished through the Setter method, SDI (Setter Dependency Injection)
Once dependency injection ends, BeanNameAware.setBeanName () is called, which sets the name of the bean in Bean Factory.
Next, call BeanClassLoaderAware.setBeanClassLoader () to provide a class loader for the bean instance. We know that all classes are loaded into the context through the class loader. The loading mechanism of the class / parent delegation model (the interview questions that big manufacturers like to ask) will be given later to give you a thorough understanding.
Then BeanFactoryAware.setBeanFactory () is called to provide the factory owned by the bean instance
I'd like to say a little more about 1 and 2, please take a look at the following code:
Here, we try to access the automatically injected field Environment env through the constructor. When the constructor is called, the Spring Bean has not been fully initialized, which will cause the NullPointerExceptions; to change our way:
In this way, the method of the @ PostConstruct tag is called after the Environment instance has been securely injected, so that the NullPointerException is not thrown.
This will look back at the cycle chart, is it suddenly enlightened?
Knock on the blackboard
It is safe to wait until all Spring Bean has completed dependency injection (the Setter Methods part of the cycle diagram) before using the reference to bean.
There will be a chapter to talk about the embarrassing questions of Spring which are often asked in interviews. Please pay attention to the follow-up articles.
Now that we can finally talk about Aware, let's take a look.
Aware
The translation of Aware can be understood as "perceived; noticed; perceived", which means that XxxxAware is right. Perceptual, is there no perception without Aware? That's right.
The highlight of Spring's dependency injection is that all Bean are unaware of the existence of Spring containers, using the "where do I come from" in the Spring Bean lifecycle? In the article, "pupils join the Young Pioneers" as an example, primary school students are still primary school students, and the rules for joining the Young Pioneers or the Communist Youth League are different, but in practical projects, we inevitably have to use the resources provided by the Spring container itself (it is inevitable that something needs the help of the Young Pioneers organization). At this time, it is necessary to make Bean actively aware of the existence of the Spring container before we can invoke the resources provided by Spring. This is Spring Aware. In fact, Spring Aware is designed by Spring for internal use of the framework, if used, your Bean will be coupled with the Spring framework, so I do not use it alone, but I hope you are no longer vague when reading the source code of the framework.
Common Spring Aware interface
The BeanNameAware subinterface describes that BeanNameAware acquires the name of the Bean in the container BeanFactoryAware gets the current BeanFactory, so you can call the container's service ApplicationContextAware as above. In the difference between BeanFactory and ApplicationContext, it has been clearly stated that MessageSourceAware acquires MessageSource-related text information, ApplicationEventPublisherAware release event ResourceLoaderAware gets the resource loader, thus obtaining external resource files
Look at the class diagram:
Of course, not only these Aware, but usually the purpose of using Spring Aware is to give Bean access to the services of the Spring container.
Code sample BeanNameAware
Custom bean to implement BeanNameAware
Register for bean
Running
As expected, the output result of BeanName is myCustomBeanName. If the name attribute of @ Bean annotation is removed, the output result is getMyBeanName.
Summary
In most cases, we should avoid using any Aware interfaces unless we need them. Implementing these interfaces will couple the code to the Spring framework, but I hope you will think more clearly after reading the framework source code after reading this section
At this point, I believe you have a deeper understanding of "what is the principle and function of Spring Aware". 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.