Sprint.Net ——依赖注入

1.首先我们创建一个控制台应用程序,并且添加Spring.Aop,Spring.Core,Spring.Data,Common.Loggin四个dll文件

2.创建Person实体类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpringNetIocExample
{
    public class Person
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public Person Friend { get; set; }

    }
}

3.创建PersonInitializtion类

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpringNetIocExample
{
    public class PersonInitializtion
    {
        private Person argPerson;
        private int intProp;

        public PersonInitializtion(Person argPerson, int intProp)
        {
            this.argPerson = argPerson;
            this.intProp = intProp;
        }

        public void WriteLine()
        {
            Console.WriteLine(intProp);
            //构造函数注入的整型参数
            Console.WriteLine(string.Format("intProp:{0}", intProp));

            //构造函数注入的Person
            Console.WriteLine(string.Format("argPerson Name:{0}", argPerson.Name));
            Console.WriteLine(string.Format("argPerson Age:{0}", argPerson.Age));

            //内联对象Friend
            Console.WriteLine(string.Format("argPerson Friend Name:{0}", argPerson.Friend.Name));
            Console.WriteLine(string.Format("argPerson Friend Age:{0}", argPerson.Friend.Age));

            //内联对象的循环引用
            Console.WriteLine(string.Format("argPerson Friend Friend Name:{0}", argPerson.Friend.Friend.Name));
            Console.WriteLine(string.Format("argPerson Friend Friend Age:{0}", argPerson.Friend.Friend.Age));

        }

    }
}

4.将app.config文件中的内容替换如下

 

 

<?xml version="1.0"?>
<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" />
    </context>

    <objects xmlns="http://www.springframework.net">
      <object id="person" type="SpringNetIocExample.Person,SpringNetIocExample">
        <!--属性值类型注入-->
        <property name="Name" value="jackyong"/>
        <property name="Age" value="25"/>

        <!--内联对象注入-->
        <property name="Friend">
          <object type="SpringNetIocExample.Person, SpringNetIocExample">
            <property name="Name" value="jackpeng"/>
            <property name="Age" value="23"/>
            <property name="Friend" ref="person"/>
          </object>
        </property>
      </object>

      <object id="PersonInitializtion" type=" SpringNetIocExample.PersonInitializtion, SpringNetIocExample">
        <!--构造器注入-->
        <constructor-arg name="argPerson" ref="person"/>
        <constructor-arg name="intProp" value="1"/>
      </object>
    </objects>

  </spring>


</configuration>




5.替换program类中Main方法中的内容如下

 

 

            IApplicationContext ctx = ContextRegistry.GetContext();
            //属性注入
            Person person = ctx.GetObject("person") as Person;
            Console.WriteLine("person name:"+person.Name);
            Console.WriteLine("person age:" + person.Age);
            //构造器注入
            PersonInitializtion initializtion = ctx.GetObject("PersonInitializtion") as PersonInitializtion;
            initializtion.WriteLine();


运行结果如下:

 

结果出来了,下面我们来详细解释一下依赖注入:

主要分为属性注入和构造器注入

1.属性注入:

 

 <object id="person" type="SpringNetIocExample.Person,SpringNetIocExample">
        <!--属性值类型注入-->
        <property name="Name" value="jackyong"/>
        <property name="Age" value="25"/>
</object>

SpringNetIocExample.Person是程序集的名称加上类名,后面的SpringNetIocExample是当前程序集的名称

 

property节点中name是对象的属性,value是要给该属性赋的值

2.构造器注入

 

 <object id="PersonInitializtion" type=" SpringNetIocExample.PersonInitializtion, SpringNetIocExample">
        <!--构造器注入-->
        <constructor-arg name="argPerson" ref="person"/>
        <constructor-arg name="intProp" value="1"/>
</object>

 

 

使用constructor-arg标签作为标示,同样具有于属性注入相同的方式,使用name,ref,value作为构造器注入的属性。

constructor-arg节点中name同样代表类中的属性,value是要给该属性赋的值,ref 是用来表示是关联到哪个object

 

 

ContextRegistry.GetContext()方法:

该方法是获取配置文件中的如下内容,且把它们装入容器中,在配置这段xml信息时,spring和context节点的名称不是任意的,必须是"spring"和"context",Spring.NET本身将"spring/context"作为字符串常量定义在了AbstractApplicationContext类中以表示上下文的节点名称

 

  <spring>
    <context>
      <resource uri="config://spring/objects" />
    </context>
    <objects xmlns="http://www.springframework.net">
      <object id="person" type="SpringNetIocExample.Person,SpringNetIocExample">
        <!--属性值类型注入-->
        <property name="Name" value="jackyong"/>
        <property name="Age" value="25"/>

        <!--内联对象注入-->
        <property name="Friend">
          <object type="SpringNetIocExample.Person, SpringNetIocExample">
            <property name="Name" value="jackpeng"/>
            <property name="Age" value="23"/>
            <property name="Friend" ref="person"/>
          </object>
        </property>
      </object>

      <object id="PersonInitializtion" type=" SpringNetIocExample.PersonInitializtion, SpringNetIocExample">
        <!--构造器注入-->
        <constructor-arg name="argPerson" ref="person"/>
        <constructor-arg name="intProp" value="1"/>
      </object>
    </objects>
  </spring>

 

 

 

Person person = ctx.GetObject("person") as Person;

这句代码就是从容器中寻找id 为person的object(包含了所有的子节点),将其实例化为person,此时person实例的所有属性已经被赋值了

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值