Chapter 4 how to manage the initialization method of your ___ object in spring. net

Source: Internet
Author: User

In spring. net, object initialization can be divided into two methods:

① Eager to instantiate, that is, the object is first instantiated during spring. Net container initialization.

② Delayed instantiation, that is, the object is instantiated only when the GetObject method is called.

Spring. net uses the eager instantiation (lazy-init = "false") method by default to initialize the object. Lazy-init = "false" literally means "Do not delay instantiation (eager instantiation)", and lazy-init = "true" means "delay instantiation. As long as an object (created through the spring container) is not displayed and set to lazy-init = "true", then spring. net container will instantiate this object when it is initialized.

When lazy-init is set, it is like a double-edged sword. When lazy-init = "true" is set to delay instantiation, our applicationProgramIt will be faster at startup, but it cannot help us detect program errors during startup. When an error occurs during a call, the consequences are unimaginable. For example, a program references a non-existent object definition or a circular dependency. When it is set to delayed instantiation, it will be postponed until the object is actually created. That is to say, if an object or its Dependencies cannot be correctly created, an exception is thrown when a container can be created normally. If the attribute that should be assigned is missing or the assigned attribute value is invalid, an exception is thrown and the object cannot be created correctly. This is also the reason why iapplicationcontext uses eager instantiation (lazy-init = "false") and Singleton = "ture" as the default deployment mode. All objects are created before they are actually needed. This time and space overhead can ensure that iapplicationcontext can detect problems early during creation. If you want to, you can override this default behavior at any time and set any object to delayed creation.

I. Eager instantiation

Let's take a look at the default settings: eager instantiation.

Objects. xml configuration file:

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?> 2   <  Objects  Xmlns  = "Http://www.springframework.net"  >  3   4     <! --  By default, the object is set to eagerly instantiate lazy-init = "false". The settings are displayed here.  -->  5     <  Object  ID  = "Hexu" Type  = "Cnbloglesson_4_4.model.person, cnbloglesson_4_4"  Lazy-init  = "False"  >  6       <  Property  Name  = "ID"  Value  = "1"  />  7       <  Property Name  = "Name"  Value  = "Hexu"  />  8       <  Property  Name  = "Isstudent"  Value  = "False"  />  9     </  Object  > 10     11   </  Objects  > 

To better prove the effect of lazy-init in spring. net, we add a constructor to person. CS and print a sentence in the constructor: "The person object has been initialized ".

Person. CSCode:

 1   Using  System;  2   Using  System. Collections. Generic;  3   4  Namespace  Cnbloglesson_4_4.model  5   {  6       ///   <Summary>  7       ///  Human  8       ///   </Summary>  9       Public   Class  Person 10   {  11           Public  Person (){  12 Console. writeline ( "  The person object is initialized.  "  );  13   }  14   15           ///   <Summary>  16          ///  No.  17           ///   </Summary>  18           Public   Int Id { Get ; Set  ;}  19   20           ///   <Summary>  21           /// Person Name  22           ///   </Summary>  23           Public   String Name { Get ; Set  ;}  24   25           ///   <Summary>  26           ///  Whether it is a student 27           ///   </Summary>  28           Public   Bool Isstudent { Get ; Set  ;}  29   }  30 }

We run the program and we can see that the breakpoint is stillConsole. writeline ("just executed here! ");In this case, the console prints out"The person object is initialized". Therefore, we can see that the default initialization behavior of spring. Net objects (eager to instantiate) is initialized when the container is initialized, and the person object is not really waitedPerson hexu1 = context. GetObject ("hexu") as person;.

Ii. Delayed instantiation

Let's take a look at the effect after we manually set the delay instantiation (lazy-init = "true") for the object.

Objects. xml configuration file:

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?>  2   <  Objects Xmlns  = "Http://www.springframework.net"  >  3   4     <! --  We manually set the object delay instantiation lazy-init = "true"  -->  5     <  Object  ID  = "Hexu_lazyinit"  Type  = "Cnbloglesson_4_4.model.person, cnbloglesson_4_4" Lazy-init  = "True"  >  6       <  Property  Name  = "ID"  Value  = "2"  />  7       <  Property  Name  = "Name"  Value = "Hexu"  />  8       <  Property  Name  = "Isstudent"  Value  = "False"  />  9     </  Object  >  10     11   </ Objects  > 

When we run the program, we can see that "just executed here !" Before this statement is output, the constructor of the hexu object is called, while the hexu_lazyinit object isConsole. writeline ("just executed here! ");Is initialized only after execution.

Sample Code: http://download.csdn.net/detail/cilence6788/5098911

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.