Spring Bean life cycle

Source: Internet
Author: User

1.Bean creation: The Beanfactory container looks for the bean's definition information, reads the bean definition file, and instantiates it, generating individual bean instances.
2. Attribute injection: Using dependency injection, spring configures all bean properties according to the bean definition information.
3.BeanNameAware Setbeanname (): Pass the Bean's ID.
4.BeanFactoryAware setbeanfactory (): The factory calls the Setbeanfactory () method to pass in to the factory itself.
If you are using ApplicationContext to generate and manage beans, it is slightly different to use ApplicationContext to generate and manage bean instances, in the execution of Beanfactoryaware setbeanfactory () Stage "4", if the Bean class has implemented the Org.springframework.context.ApplicationContextAware interface, then its Setapplicationcontext () method is executed, Then execute the beanpostprocessors processbeforeinitialization () and subsequent processes

Pre-initialization of 5.BeanPostProcessors processbeforeinitialization ():
If any of the Org.springframework.beans.factory.config.BeanPostProcessors instances are related to the bean instance. Executes the Processbeforeinitialization () method of the Beanpostprocessors instance.
6.initializingBean Afterpropertiesset ():
If the Bean class has implemented the Org.springframework.beans.factory.InitializingBean interface, execute his afterpropertiesset () method.
Init-method defined in the 7.Bean definition file:
You can use the "Init-method" property to set the method name in the bean definition file
Example: <bean calss= "Onlyfun.caterpillar.HelloBean" init-method= "Initbean" >
8.BeanPostProcessors processafterinitialization ()-After initialization
If any of the Beanpostprocessors instances are associated with the bean instance, the Processafterinitialization () method of the Beanpostprocessors instance is executed.
At this point, the bean can already be used by the application system and will remain in beanfactory to know that it is not being used. There are two ways to remove it from beanfactory
9.DisposableBean Destroy ()
If the Bean class has implemented the Org.springframework.beans.factory.DisposableBean interface when the container is closed, his destroy () method is executed.
Definition destroy-method in 10.Bean definition file





Use the opportunity provided by spring to customize the bean creation process

Only beans with singleton behavior accept the container management life cycle.
The bean,spring container for Non-singleton behavior is simply the replacement of new, and the container is only responsible for creation.

Spring can manage the behavior of the end of the instantiation and before the destruction, managing the bean's life cycle behavior without the following two opportunities:
After the bean all depends on injection
Before the bean is about to be destroyed

1) Behavior implementation after dependency injection:
There are two methods: A. Writing the Init method B. Implementing the Initializingbean interface
Afterpropertiesset and Init appear at the same time, the former executes before the latter, using the Init method, you need to add the Init-method property to the configuration file

2) behavior before the bean is destroyed
There are two ways to do this: a. Writing the Close Method B. Implementing the Disposablebean interface
Destroy and close appear at the same time, prior to the latter execution, using the Close method, you need to add the Destroy-method property to the configuration file
* Please describe the life cycle of spring bean
First, the bean definition
Spring typically defines a bean through a configuration file. Such as:
<?xml version= "1.0″encoding=" utf-8″?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd ">
<bean id= "HelloWorld" class= "Com.pqf.beans.HelloWorld" >
<property name= "MSG" >
<value>HelloWorld</value>
</property>
</bean>
</beans>
This configuration file defines a bean that is identified as HelloWorld. Multiple beans can be defined in a configuration document.
Second, the initialization of the bean
There are two ways of initializing a bean.
1, in the configuration document by specifying the Init-method property to complete
Implement a method in the Bean's class that initializes the bean properties, such as Init (), such as:
public class helloworld{
Public String Msg=null;
Public Date Date=null;
public void init () {
Msg= "HelloWorld";
Date=new Date ();
}
......
}
Then, set the Init-mothod property in the configuration file:
<bean id= "HelloWorld" class= "Com.pqf.beans.HelloWorld" init-mothod= "init" >
</bean>
2. Implement Org.springframwork.beans.factory.InitializingBean interface
The bean implements the Initializingbean interface and adds the Afterpropertiesset () method:
public class HelloWorld implement Initializingbean {
Public String Msg=null;
Public Date Date=null;
public void Afterpropertiesset () {
msg= "Say hello to the world! ”;
Date=new Date ();
}
......
}
Then, when all the properties of this bean are set by spring's beanfactory, the Afterpropertiesset () method is automatically called to initialize the bean, so the configuration file does not have to specify the Init-method attribute.
Third, the Bean call
There are three ways to get a bean and make a call:
1. Using Beanwrapper
HelloWorld hw=new HelloWorld ();
Beanwrapper bw=new Beanwrapperimpl (HW);
Bw.setpropertyvalue ("msg", "HelloWorld");
System.out.println (Bw.getpropertycalue ("MSG"));
2. Using Beanfactory
InputStream is=new fileinputstream ("config");
Xmlbeanfactory factory=new xmlbeanfactory (IS);
HelloWorld hw= (HelloWorld) Factory.getbean ("HelloWorld");
System.out.println (Hw.getmsg ());
3. Using Applicationconttext
ApplicationContext actx=new flesystemxmlapplicationcontext ("config");
HelloWorld hw= (HelloWorld) Actx.getbean ("HelloWorld");
System.out.println (Hw.getmsg ());
Iv. The destruction of beans
1. Using the Destory-method attribute in the configuration file
Similar to the initialization property Init-methods, implements a method of undoing the bean in the bean's class, and then specifies it through Destory-method in the configuration file, and spring automatically invokes the specified destroy method when the bean is destroyed.
2. Implement Org.springframwork.bean.factory.DisposebleBean interface
If the Disposeblebean interface is implemented, spring will automatically invoke the Destory method in the bean to destroy it, so the Destory method must be provided in the bean.

* How to get beans in spring
Through the XML configuration file
Bean configuration in XML, Spring provides a variety of ways to read the configuration file to get ApplicationContext.
The first way: Filesystemxmlapplicationcontext
By the time the program initializes, it imports the bean configuration file and then gets the bean instance:
ApplicationContext ac = new Filesystemxmlapplicationcontext ("Applicationcontext.xml")
Ac.getbean ("Beanname");
The second way: Webapplicationcontextutil
In the B/s system, the bean's configuration file is typically initialized in Web. XML, and then ApplicationContext is obtained by Webapplicationcontextutil. For example:
ApplicationContext CTX = webapplicationcontextutils.getrequiredwebapplicationcontext (ServletContext SC);
ApplicationContext CTX = webapplicationcontextutils.getwebapplicationcontext (ServletContext SC);
Where ServletContext SC can be specifically replaced by Servlet.getservletcontext () or This.getservletcontext () or request.getsession (). Getservletcontext ();
In addition, because spring is an injected object placed in ServletContext, you can remove the Webapplicationcontext object directly from the ServletContext:
Webapplicationcontext Webapplicationcontext = (webapplicationcontext) Servletcontext.getattribute ( Webapplicationcontext.root_web_application_context_attribute);

Spring Bean life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.