Chapter 4 how spring. Net manages automatic assembly of your ___ objects

Source: Internet
Author: User

The notes are not updated because they have been busy for the past few days.

Spring. Net has the ability to automatically assemble, that is, spring. Net can automatically distinguish the collaborative objects of an object through the definition of the object. Automatic Assembly is for a single object (by: For each collaborative object), so you can enable automatic assembly for some objects and disable automatic assembly for some objects (: that is, automatic assembly of some collaboration objects, rather than automatic assembly of other collaboration objects ). Automatic Assembly can reduce or even completely eliminate the setting of attributes or parameter values.

The above is an official definition, which is hard to understand. To put it bluntly, we are using most frameworks, including spring. net Framework for software development has a complex configuration file, and spring. NET provides a setProgramThe mechanism of staff laziness (less configuration file writing. We can imagine the number of <Object ID = "…" in the objects. xml file when there are a large number of complex business logic in our system "...." Type = "…"> .... </Object> label. The mechanism that can reduce configuration is called automatic assembly.

Automatic Assembly means spring. Net searches for relevant attributes and automatically assembles them according to the specified automatic mode. In this way, the configuration is reduced.

Currently, there are five automatic assembly modes:

    1. NO : automatic assembly is not performed. This is the default setting. It is generally not recommended to modify this setting, because explicitly specifying the object collaboration relationship can make developers know what they are doing and help to create documents for the software structure.
    2. byname : Spring. net will check the objects in the container, find the object definition with the same name as the assembled property, and use it as the value of this property. For example, if you set an object definition to name-based assembly and the object has an attribute named name, spring. net will find the object definition named name and use it as the value of the name attribute. That is to say, byname is used to search for the ID of the object with the same property name in the container. If not, the property Assembly fails.
    3. bytype : Spring. Net searches for collaboration Objects Based on the specified type. Assume that an object definition has a collaboration object of the person type, spring. Net searches for the object definition of the person type in the entire object factory to assemble it. If you cannot find an object of this type, or find more than one object of this type, spring. net will throw an exception, so that the object cannot be automatically assembled. It tries to query the object with the same property type in the container. If not found, the property Assembly fails. If multiple objects of the same type are found, an unsatisfieddependencyexception exception is thrown.
    4. constructor : similar to bytype, but the object to be searched must be assigned a value to the constructor parameter. If there is no or more of the same object definition as the parameter type in the object factory, a fatal exception is thrown. Search for one or more objects that are consistent with the construction parameters of the automatically assembled object. If an uncertain object or constructor parameter exists, an exception is thrown.
    5. autodetect : automatically determines whether the constructor or bytype mode is used based on the object's own configuration information. If the object has a default constructor, The bytype mode is used. Note that this method is no longer supported in spring. Java 3.0.

To use automatic assembly, you must setAutowireAttributes include:

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?>  2   <  Objects  Xmlns  = "Http://www.springframework.net"  >  3   4     <  Object  ID = "Dog"  Type  = "Cnbloglesson_4_4_2.dog, cnbloglesson_4_4_2"  >  5       <  Property  Name  = "Name"  Value  = "Jeffy"  />  6     </  Object  >  7    8     <  Object  ID  = "P1"  Type  = "Cnbloglesson_4_4_2.person, cnbloglesson_4_4_2"  >  9       <  Property  Name  = "Price"  Value  = "1"  />  10      <  Property  Name  = "Dog"  Ref  = "Dog"  />  11     </  Object  >  12     13     <! --  Automatic Assembly by name  -->  14    <  Object  ID  = "P2"  Type  = "Cnbloglesson_4_4_2.person, cnbloglesson_4_4_2"  Autowire  = "Byname"  > </  Object  >  15     16     <! --  Automatic Assembly by type  --> 17     <  Object  ID  = "P3"  Type  = "Cnbloglesson_4_4_2.person, cnbloglesson_4_4_2"  Autowire  = "Bytype"  > </  Object  >  18   19   </  Objects  > 

Take a look at the program. CS classCode:

 1   Using  System;  2   Using  Spring. context;  3   Using  Spring. Context. Support;  4   5   Namespace  Cnbloglesson_4_4_2  6   { 7       Class  Program  8   {  9           Static   Void Main ( String  [] ARGs)  10   {  11               //  Use iapplicationcontext to configure  12 Iapplicationcontext context =Contextregistry. getcontext ();  13   14 Dog dog = context. GetObject ( "  Dog  " ) As  Dog;  15   16 Person p1 = context. GetObject ( "  P1  " ) As  Person; 17   18 Person P2 = context. GetObject ( "  P2  " ) As  Person;  19   20 Person P3 = context. GetObject ( "  P3  " ) As  Person;  21  22   23   Console. readkey ();  24   }  25   }  26 }

According to the monitoring variables, P3 automatically assembles the dog object to the attributes of P3 through autowire = "bytype ".

Through the automatic assembly mechanism, on the surface, the configuration file is much simpler. However, automatic assembly is not perfect. Whether we use the byname or bytype method, spring may not be able to find the desired object accurately. In addition, if automatic assembly is used, the readability of the configuration file is greatly reduced. We cannot easily see the dependency between objects, which also reduces the maintainability of the program to a certain extent. From another perspective, automatic assembly hides many details, which is not conducive to programmer control. Therefore, we must weigh the advantages and disadvantages when using automatic assembly.

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

 

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.