您的位置:首页 > 其它

Sprint.Net 依赖注入

2016-09-14 15:47 218 查看
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>
</ob
4000
jects>

</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同样代表类中的属性,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实例的所有属性已经被赋值了

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: