Spring IOC DI LifeCycle

Source: Internet
Author: User
Tags object object

Spring IOC three ways to create a JavaBean factory:

1.BeanFactory bf = new Xmlbeanfactory (New Classpathresource ("Bean.xml"));

Beanfactory:spring manages JavaBean's top-level interface, and the factory created directly with this interface has the role of lazy loading

ApplicationContext ac = new Classpathxmlappliactioncontext ("Bean.xml") is commonly used, and all beans are loaded bean.xml when created.

Usually the IOC and DI are used in conjunction with IOC: Control inversion, which is no longer a programmer to create an instance of the class, and all to the spring container to manage the class, the container to create an instance of the class, by the container management class life cycle di: attribute injection

Principle: IOC is to create an instance of the class through the reflection mechanism of Java, which can be initialized for the class by the constructor of the class.

Class Clazz = Class.forName ("Full path of the class");

Object object = Clazz.newinstance ();

The default scenario is to invoke the class's parameterless construct <bean id= "" class = "" ></bean> Default scope is singleton

Scope has four range of values:

Singleton (Singleton, only one bean instance object in the container)

prototype (Non-singleton, each call to Getbean ("") creates a new instance),

Request (Create an instance of a bean and put it in the request domain)

Session (Create an instance of a bean and put it in the session field)

JavaBean initialization based on the non-parametric construction

<bean>

<property name= "" value= "" ref= ""/>

Name: property names the value: property of the base data type ref: Applies the properties of the data type, if the collectiontype of data < property > has a list ,array,set,map, sub-label of typeprops

</bean>

The bean is initialized according to the method of the parameter construction.

<bean>

<construction-arg index= "" value= "" ref= "" type= "" Name= ""/>

index: The position of the parameter .Value: Assignment of the base data type, type data, ref: Assignment of a complex type, type is a collection type to mate with <util> Use name: The names of formal parameters

</bean>

When using the constructor method to initialize the class, the number of parameters of the type to correspond, or error

Spring's life cycle lifecycle

    1. Instantiate Bean Object Instantiation
    2. Populate Properties Package Property
    3. If the Beannameware interface is implemented, set the name for the bean (the default is the value of the id attribute, and if the id attribute has no value, set the value of name)
    4. If the Beanfactoryware or Applicationcontextware interface is implemented, set the ApplicationContext
    5. If Beanpostprocessor is implemented, the Postprocessbeforeinitialization method of Beanpostprocessor is executed, which has two parameters (Object bean,string Beanname) This method is similar to the interceptor, to return the bean, otherwise it will be reported nullprointexception. This method is a global parameter that is configured once in a Bean.xml and manages all the beans in the entire XML file.
    6. If the bean implements the Initializingbean, he will execute the Afterpropertyset () method
    7. Perform a custom Init-method method (required to configure Init-method in Bean.xml) general initialization and destruction will execute this method
    8. If beanpostprocessor (processing bean) is implemented, the Afterpostprocessor method is executed
    9. Execute Business method
    10. If the Disposablebean interface is implemented, the Destroy method in the execution interface
    11. Execute the Destroy-method method (you need to specify the Destroy-method method in Bean.xml)

To perform steps 10 and 11, you need to call the Close method in Classpathxmlapplicationcontext;

The 3-4 step is to set the container's parameters for the bean and let the bean understand the bean

5, 8 step is to enhance the function of the bean

6 and 10,7 and 11 are initialization and destruction of bean 7,11 coupling is low, commonly used 7,11 instead of 6 and 10;

Spring IOC DI LifeCycle

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.