Spring.net Dependency Injection Framework learning--common methods for injecting objects

Source: Internet
Author: User

Spring.net Dependency Injection Framework Learning---Instantiation of container common methods

This article learns two ways to instantiate spring.net containers

1. Create a spring.net container via xmlobjectfactory
New Filesystemresource ("objects.xml"new xmlobjectfactory (input);

This can be done through Factory's GetObject ("ObjectName"); Get this object

2. Create a spring.net container via Iapplicationcontext
Iapplicationcontext CTX = Contextregistry.getcontext ();

This can be done through Iapplicationcontext's GetObject ("ObjectName"); Get this object

Program examples

Example code: Person.cs
namespace spring.net01{    publicclass  person    {        public person  ()        { }         ~ Person()        {}        publicvoid  print ()        {            Console.WriteLine (  " I am a person object ");     }}}
App. Config file
<?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> <!--the source of metadata objects <resource uri="config://spring/objects"></resource> </context> <objects xmlns="http://www.springframework.net"> <!--a Person object--<ObjectId="Person1"Type="spring.net01.person,spring.net01"> </Object> </objects> </spring></configuration>
Add Objects.xml where the Objects property value must be added
<?xml version="1.0"encoding="Utf-8"? ><objects xmlns="http://www.springframework.net"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://www.springframework.nethttp//www.springframework.net/xsd/spring-objects.xsd "><ObjectId="Person2"Type="spring.net01.person,spring.net01"> </Object></objects>
Test code:
 classProgram {Static voidMain (string[] args) {            //Normal object CreationConsole.WriteLine ("--------Common Object creation method--------"); Person Person=NewPerson ();            Person.print (); //injecting objects through spring.net IOC IapplicationcontextConsole.WriteLine ("--------Iapplicationcontext Method--------"); Iapplicationcontext content=Contextregistry.getcontext (); Person Bennanhai= (person) content. GetObject ("Person1");            Bennanhai.print (); //injecting objects through spring.net IOC xmlobjectfactoryConsole.WriteLine ("--------Xmlobjectfactory Method--------"); Iresource input=NewFilesystemresource ("Objects.xml"); Iobjectfactory Factory=Newxmlobjectfactory (input); Person Bennanhai2= (person) factory. GetObject ("Person2");            Bennanhai2.print ();        Console.read (); }    }
Run results

Source Code Engineering Download

Spring.net Dependency Injection Framework learning--common methods for injecting objects

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.