Spring. Net learning notes (3)-create object, spring.net learning notes

Source: Internet
Author: User

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();        }    }}

Related Article

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.