Spring. Net learning notes (3)-create object, spring.net learning notes
I. Development Environment
Compiler: VS2013
. Net version:. net framework4.5
Ii. Assembly
Spring. Core. dll: 1.3
Common. Logging
Iii. Development Process 1. Project Structure
Namespace CreateObjects {public class Person {public override string ToString () {return "I am a Person class";} public class Heart {public override string ToString () {return "I Am a Heart class ";}}}}3. Add GenericClass. cs
namespace CreateObjects{ public class GenericClass<T> { }}
4. Add StaticObjectsFactory. cs
namespace CreateObjects{ public static class StaticObjectsFactory { public static Person CreateInstance() { return new Person(); } }}
5. Add InstanceObjectsFactory. cs
namespace CreateObjects{ public class InstanceObjectsFactory { public Person CreateInstance() { return new Person(); } }}
6. Configure Spring. Net
<? 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 "> </resource> </context> <objects> <! -- Constructor --> <object id = "person" type = "CreateObjects. Person, CreateObjects"> </object> <! -- Nested type --> <object id = "heart" type = "CreateObjects. Person + Heart, CreateObjects"> </object> <! -- Generic type GenericClass <int> --> <object id = "genericClass" type = "CreateObjects. GenericClass <int>, CreateObjects"> </object> <! -- Static factory --> <object id = "staticObjectsFactory" type = "CreateObjects. StaticObjectsFactory, CreateObjects" factory-method = "CreateInstance"> </object> <! -- Instance factory --> <! -- Factory --> <object id = "instanceObjectsFactory" type = "CreateObjects. InstanceObjectsFactory, CreateObjects"> </object> <! -- Created object --> <object id = "instancePerson" factory-method = "CreateInstance" factory-object = "instanceObjectsFactory"> </object> </objects> </spring> <startup> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.5 "/> </startup> </configuration>
7. the console program is as follows:
namespace CreateObjects{ class Program { static void Main(string[] args) { IApplicationContext context = ContextRegistry.GetContext(); Person person = context.GetObject("person") as Person; Console.WriteLine(person); Person.Heart heart = context.GetObject("heart") as Person.Heart; Console.WriteLine(heart); GenericClass<int> genericClass = context.GetObject("genericClass") as GenericClass<int>; Console.WriteLine(genericClass); Person personStaticFactory = context.GetObject("staticObjectsFactory") as Person; Console.WriteLine(personStaticFactory); Person personInstanceFactory = context.GetObject("instancePerson") as Person; Console.WriteLine(personInstanceFactory); Console.ReadKey(); } }}