In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to understand and master the Spring Bean life cycle". In the daily operation, I believe many people have doubts about how to understand and master the Spring Bean life cycle. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to understand and master the Spring Bean life cycle". Next, please follow the editor to study!
How are these Spring Beans generated?
Before formally answering this question, I'd like to answer some conceptual questions about Java Bean, Spring Bean, and Spring IoC containers, which I'd like to illustrate with the following example:
Primary school students (Java Bean) join the Young Pioneers (Spring Ioc container) by submitting material applications (metadata configuration). After learning some spirit and rules, they become Young Pioneers (Spring Bean).
It can be seen here that both Java Bean and Spring Bean are objects with specific functions, and the pupil is still the pupil, but after joining the Young Pioneers, they have a new identity, and the new identity has to fulfill specific obligations in accordance with the provisions of the organization (Spring Ioc).
Let's take a look at the following picture to deepen our understanding.
First of all, there must be a container. Instantiating the Spring Ioc container is very simple. The interface org.springframework.context.ApplicationContext represents the Spring IoC container, which is responsible for instantiating, configuring and assembling the above bean. The container obtains instructions about the objects to instantiate, configure, and assemble by reading configuration metadata. Configuration metadata is usually expressed in the form of XML,Java annotations or code. It allows you to express the objects that make up your application and the rich interdependencies between them, such as this:
ApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"spring.xml", "spring1.xml"})
With the container, what do we need to do to make its internal objects Ready for Use?
We need to instantiate them through the Spring container, and Spring provides us with three ways:
Three initialization methods InitializingBean
Spring provides us with InitializingBean interface.
Public interface InitializingBean {void afterPropertiesSet () throws Exception;}
We can instantiate in its only method, afterPropertiesSet, by implementing the InitializingBean interface, but Spring Framework officials do not recommend that we instantiate Bean in this way. This is a strong coupling approach, and we will only use this method at the framework level.
@ PostConstruct
This method is advocated by Spring very much. We usually mark it on the method, which is usually named init ().
@ PostConstructpublic void init () {System.out.println ("Inside init () method...");} init-method
You should have seen this initialization:
Public class MyClass {public void init () {/ / perform post-creation logic here} @ Configurationpublic class AppConfig {@ Bean (initMethod = "init") public MyClass myclass () {return new MyClass ();}}
You should also have seen this configuration:
Yes, this is just a different implementation of the same function. These are the three ways to initialize Spring Beans. We have seen three ways to use them together in the framework, so what is the calling order of the combination?
First, @ PostConstruct will be called first.
Second, the InitializingBean.afterPropertiesSet () method will be called
Finally, call the init-method method configured through XML or the method of setting the initMethod property by setting the @ Bean annotation
Knowing this, you will understand how Spring Bean came into being.
Illustrate by illustration:
Is it difficult to remember the sequence of calls by combining shying?
PostConstruct (P), afterPropertiesSet (A), init-method (I)-> PAI (pi)
BeanPostProcessor
You should also have an impression of the BeanPostProcessor interface. There are only two methods:
Public interface BeanPostProcessor {Object postProcessBeforeInitialization (Object var1, String var2) throws BeansException; Object postProcessAfterInitialization (Object var1, String var2) throws BeansException;}
Looking at the method names, BeforeInitialization and AfterInitialization, we should guess that this is a global aspect before and after the above three ways. We often use the postProcessAfterInitialization method to complete some subsequent logic writing and attribute settings by reading the annotations of Bean. Now this is the case before Ready for Use:
Before Ready for Use, understanding these contents can basically meet the daily work content, but this is not the whole content of Ready for Use. The whole lifecycle process of Spring Bean should be like this. Subsequent articles will gradually highlight:
At this point, the study on "how to understand and master the Spring Bean life cycle" 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.
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.