In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the scope and life cycle of Bean in Spring". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the scope and life cycle of Bean in Spring".
First, the scope of Bean
When you create an instance of Bean through the Spring container, you can not only instantiate Bean, but also use the scope property of Bean to set the scope for bean.
Syntax format:
Type of scope: (sing)
The difference between singleton and prototype: (the two are more commonly used)
① singleton single instance, prototype multiple instance
When the ② setting scope value is singleton, a single instance object is created when the spring configuration file is loaded.
When the scope value is set to prototype, the object is temporarily not created when the spring configuration file is loaded, and the multi-instance object is created only when the getBean method is called.
Singleton scope:
/ / Cat.javapublic class Cat {private String name; public void setName (String name) {this.name = name;}}
Profile beans5.xml
Test:
/ / scope test method of Bean @ Testpublic void catTest () {/ / 1. Initialize the Spring container and load the configuration file ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("beans5.xml"); / / 2. Obtain the Bean instance Cat cat1 = (Cat) applicationContext.getBean ("cat"); Cat cat2 = (Cat) applicationContext.getBean ("cat") through the Spring container; / / 3. Output the acquired instance System.out.println (cat1); System.out.println (cat2);}
Prototype scope:
Change the scope property in the configuration file beans5.xml to prototype, and run the test again:
3 Test:
II. The life cycle of Bean
The life cycle of Bean from creation to destruction is called Bean. In general, the life cycle of Bean consists of seven steps:
(1) create a bean instance through a no-parameter constructor
(2) call the property setter method to set the value for the property of bean.
(3) the method postProcessBeforeInitialization of passing bean instance to bean post processor
(4) call the initialization method of bean (the initialization method needs to be configured)
(5) the method postProcessAfterInitialization of passing bean instance to bean post processor
(6) get and use the created bean
(7) when the container is closed, call the destroy method of bean (the method of destroying needs to be configured)
Note:
① initialization method and destroy method need to be manually configured as attributes in Bean.
② only Bean in singleton scope executes the destroy method
Use code to demonstrate the lifecycle of Bean
(1) ordinary Java Bean:Cat.java
Public class Cat {private String name; public void setName (String name) {this.name = name; System.out.println ("second step: call the property setter method to set the value of the property of bean");} public Cat () {System.out.println ("first step") Create bean instance through no-parameter constructor ");} / / initialization method (configuration implementation call in configuration file) public void initMethod () {System.out.println (" step 4: calling the initialization method of bean ") } / / destroy method (configure the implementation call in the configuration file) public void destroyMethod () {System.out.println ("step 7: call bean's destroy method when the container is closed");}}
(2) myBeanPostProcessor implements BeanPostProcessor interface and post processor: myBeanPostProcessor.java
(AOP in spring is implemented by implementing the BeanPostProcessor interface.)
/ / myBeanPostProcessor implements the BeanPostProcessor interface and implements the post processor public class myBeanPostProcessor implements BeanPostProcessor {@ Override public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException {System.out.println ("the third step: the method of passing the bean instance to the bean post processor"); return bean } @ Override public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException {System.out.println ("step 5: how to pass the bean instance to the bean post processor"); return bean;}}
(3) configuration file: beans5.xml
(4) testing
Testpublic void catTest () {/ / 1. Initialize the Spring container and load the configuration file ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext ("beans5.xml"); / / 2. Obtain the Bean instance Cat cat = (Cat) applicationContext.getBean ("cat") through the Spring container; / / 3. Output the acquired instance System.out.println ("step 6: get creating Bean instance" + cat); / / 4. Manually close applicationContext.close ();}
Thank you for your reading, the above is "what is the scope and life cycle of Bean in Spring". After the study of this article, I believe you have a deeper understanding of what the scope and life cycle of Bean in Spring is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.