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