Spring. Net study notes 7-dependent object injection (basic)

Source: Internet
Author: User

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:

  1. <Object ID = "modernperson" type = "springnetioc. modernperson, springnetioc">
  2. <Property name = "tool" ref = "computer"/>
  3. </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:

  1. <Property name = "friend">
  2. <Object type = "springnetdi. Person, springnetdi"/>
  3. </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:

  1. <Constructor-Arg name = "argperson" ref = "person"/>
  2. <Constructor-Arg name = "intprop" value = "1"/>

Copy code

The program code is as follows:

  1. Public class person
  2. {
  3. Public string name {Get; set ;}
  4. Public int age {Get; set ;}
  5. Public Person friend {Get; set ;}
  6. }

Copy code

Persondao

  1. Public class persondao
  2. {
  3. Private person argperson;
  4. Private int intprop;
  5. Public persondao (person argperson, int intprop)
  6. {
  7. This. argperson = argperson;
  8. This. intprop = intprop;
  9. }
  10. Public void get ()
  11. {
  12. // Integer parameter of constructor Injection
  13. Console. writeline (string. Format ("intprop: {0}", intprop ));
  14. // Construct the person injected by the function.
  15. Console. writeline (string. Format ("argperson name: {0}", argperson. Name ));
  16. Console. writeline (string. Format ("argperson age: {0}", argperson. Age ));
  17. // Inline object friend
  18. Console. writeline (string. Format ("argperson friend name: {0}", argperson. Friend. Name ));
  19. Console. writeline (string. Format ("argperson friend age: {0}", argperson. Friend. Age ));
  20. // Circular reference of inline objects
  21. Console. writeline (string. Format ("argperson friend name: {0}", argperson. friend. Friend. Name ));
  22. Console. writeline (string. Format ("argperson friend age: {0}", argperson. friend. Friend. Age ));
  23. }
  24. }

Copy code

App. config

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Configuration>
  3. <Configsections>
  4. <Sectiongroup name = "Spring">
  5. <Section name = "context" type = "Spring. Context. Support. contexthandler, spring. Core"/>
  6. <Section name = "objects" type = "Spring. Context. Support. defaultsectionhandler, spring. Core"/>
  7. </Sectiongroup>
  8. </Configsections>
  9. <Spring>
  10. <Context>
  11. <Resource uri = "config: // spring/objects"/>
  12. </Context>
  13. <Objects xmlns = "http://www.springframework.net">
  14. <Object ID = "person" type = "springnetdi. Person, springnetdi">
  15. <! -- Property Value Type Injection -->
  16. <Property name = "name" value = "Liu Dong"/>
  17. <Property name = "Age" value = "27"/>
  18. <! -- Inline object injection -->
  19. <Property name = "friend">
  20. <Object type = "springnetdi. Person, springnetdi">
  21. <Property name = "name" value = "beggar"/>
  22. <Property name = "Age" value = "23"/>
  23. <Property name = "friend" ref = "person"/>
  24. </Object>
  25. </Property>
  26. </Object>
  27. <Object ID = "persondao" type = "springnetdi. persondao, springnetdi">
  28. <! -- Constructor injection -->
  29. <Constructor-Arg name = "argperson" ref = "person"/>
  30. <Constructor-Arg name = "intprop" value = "1"/>
  31. </Object>
  32. </Objects>
  33. </Spring>
  34. </Configuration>

Copy code

Program

  1. Class Program
  2. {
  3. Static void main (string [] ARGs)
  4. {
  5. Iapplicationcontext CTX = contextregistry. getcontext ();
  6. Persondao Dao = CTX. GetObject ("persondao") as persondao;
  7. Dao. Get ();
  8. Console. Readline ();
  9. }
  10. }

Copy code

The output result is as follows:

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.