1. Property Injection
In the previous article, we briefly mentioned the purpose of dependency injection. Review the content and find that <property name = "tool" ref = "computer"/> is used under the object node. The property tag is used for property injection. Ref is used to identify the object to be associated. The name attribute refers to the property name. As follows:
- <Object ID = "modernperson" type = "springnetioc. modernperson, springnetioc">
- <Property name = "tool" ref = "computer"/>
- </Object>
Copy code
Value Type Injection requires the Value Attribute of the property node. For example, <property name = "name" value = "Liu Dong"/>
As the inline type, you can use the following:
- <Property name = "friend">
- <Object type = "springnetdi. Person, springnetdi"/>
- </Property>
Copy code
Similarly, the inline type can be a circular reference object (see code ).
2. constructor Injection
Constructor-Arg label is used for Constructor injection. In the same way as property injection, name, ref, and value are used as the attributes of the constructor injection, as follows:
- <Constructor-Arg name = "argperson" ref = "person"/>
- <Constructor-Arg name = "intprop" value = "1"/>
Copy code
The program code is as follows:
- Public class person
- {
- Public string name {Get; set ;}
- Public int age {Get; set ;}
- Public Person friend {Get; set ;}
- }
Copy code
Persondao
- Public class persondao
- {
- Private person argperson;
- Private int intprop;
- Public persondao (person argperson, int intprop)
- {
- This. argperson = argperson;
- This. intprop = intprop;
- }
- Public void get ()
- {
- // Integer parameter of constructor Injection
- Console. writeline (string. Format ("intprop: {0}", intprop ));
- // Construct the person injected by the function.
- Console. writeline (string. Format ("argperson name: {0}", argperson. Name ));
- Console. writeline (string. Format ("argperson age: {0}", argperson. Age ));
- // Inline object friend
- Console. writeline (string. Format ("argperson friend name: {0}", argperson. Friend. Name ));
- Console. writeline (string. Format ("argperson friend age: {0}", argperson. Friend. Age ));
- // Circular reference of inline objects
- Console. writeline (string. Format ("argperson friend name: {0}", argperson. friend. Friend. Name ));
- Console. writeline (string. Format ("argperson friend age: {0}", argperson. friend. Friend. Age ));
- }
- }
Copy code
App. config
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Configuration>
- <Configsections>
- <Sectiongroup name = "Spring">
- <Section name = "context" type = "Spring. Context. Support. contexthandler, spring. Core"/>
- <Section name = "objects" type = "Spring. Context. Support. defaultsectionhandler, spring. Core"/>
- </Sectiongroup>
- </Configsections>
- <Spring>
- <Context>
- <Resource uri = "config: // spring/objects"/>
- </Context>
- <Objects xmlns = "http://www.springframework.net">
- <Object ID = "person" type = "springnetdi. Person, springnetdi">
- <! -- Property Value Type Injection -->
- <Property name = "name" value = "Liu Dong"/>
- <Property name = "Age" value = "27"/>
- <! -- Inline object injection -->
- <Property name = "friend">
- <Object type = "springnetdi. Person, springnetdi">
- <Property name = "name" value = "beggar"/>
- <Property name = "Age" value = "23"/>
- <Property name = "friend" ref = "person"/>
- </Object>
- </Property>
- </Object>
- <Object ID = "persondao" type = "springnetdi. persondao, springnetdi">
- <! -- Constructor injection -->
- <Constructor-Arg name = "argperson" ref = "person"/>
- <Constructor-Arg name = "intprop" value = "1"/>
- </Object>
- </Objects>
- </Spring>
- </Configuration>
Copy code
Program
- Class Program
- {
- Static void main (string [] ARGs)
- {
- Iapplicationcontext CTX = contextregistry. getcontext ();
- Persondao Dao = CTX. GetObject ("persondao") as persondao;
- Dao. Get ();
- Console. Readline ();
- }
- }
Copy code
The output result is as follows: