一、 基本概念
Spring.Net是一个轻量级的控制反转(Ioc)和面向切面的(Aop)的容器框架;
Ioc:控制反转:简单的说就是将创建对象的控制权转交给外部容器(IApplicationContext);
DI:依赖注入:其实就是将通过容器创建对象的时候,给属性、构造函数的参数注入默认值;
Spring.Net不是一门技术,而是一种思想,其目的就是实现松耦合;
二、使用容器创建对象
在配置文件中配置容器对象
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"><!--声明两个模块,context和object--> <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"/> <!--资源文件指向当前配置文件的objects节点,也可以另外指定文件--> </context> <objects xmlns="http://www.springframework.net"> <!--创建对象,对象名为name的值,Type:类型的全名称(命名空间.类名)+程序集的名称;--> <object name="myPerson" type="SpringNet.Person,SpringNet"> </object> </objects> </spring> </configuration>
关于ConfigSection的使用,看http://www.cnblogs.com/jhxk/articles/1609182.html
配置好容器后,要想创建对象,首先要获取一个容器对象(IApplicationContext ctx=ContextRegistry.GetContext()) 然后就可以用容器去创建配置文件中配置的对象。
配置对象的时候要注意:
1、配置对象的配置文件可以在当前配置文件(App.Config/Web.Config),当然也可以另外指定文件,指定文件的路径应该在<Context>=><resource/>节点下的uri属性中指定,例如:<resource uri=file://config//TestObjects.xml/> 2、 应该在<objects>节点下配置对象(<object></object>)形式如下:
<objects xmlns="http://www.springframework.net"> <!--创建对象,对象名为name的值,Type:类型的全名称(命名空间.类名)+程序集的名称;--> <object name="myPerson" type="SpringNet.Person,SpringNet"> </object> </objects>
对象的name值必须唯一;
3、用容器获取对象:
using Spring.Context; using Spring.Context.Support; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringNet { class Program { static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); Person p = (Person)ctx.GetObject("myPerson"); Console.WriteLine(p.Name); Console.ReadKey(); } } //Person类 public class Person { public string Name { get; set; } public int Age { get; set; } } }
三、属性和构造函数的注入
1、简单的属性输入
在<Property>节点中,指定属性名和值;
<object id="myPerson" type="SpringNet.Person,SpringNet"> <property name="Name" value="cm"/> </object>
2、构造函数参数的注入
<objects xmlns="http://www.springframework.net"> <!--创建对象,对象名为name的值,Type:类型的全名称(命名空间.类名)+程序集的名称;--> <object name="myPerson" type="SpringNet.Person,SpringNet"> <constructor-arg index="0" value="蔡猛"/> <constructor-arg index="1" value="18"/> <property name="Name" value="cm"/> </object> </objects>
Index表示第几个参数,value表示参数的值。也可以这么写=>(<constructor-arg name="Name" value=""哆啦的哆啦多">)
构造器的注入要在属性之前,否则会报错
3、复杂属性的注入
假设Person类有一个住址属性 public Adress PerSonAdress{get;set}
关于Adress类的定义如下:
public class Adress { public string ProvinceName { get; set; }//省 public string CityName { get; set; }//市 public string CountyName { get; set; }//县 }
现在为新建的Person对象的PersonAdress属性注入值,配置如下:
<objects xmlns="http://www.springframework.net"> <!--创建对象,对象名为name的值,Type:类型的全名称(命名空间.类名)+程序集的名称;--> <object name="myPerson" type="SpringNet.Person,SpringNet"> <constructor-arg index="0" value="蔡猛"/> <constructor-arg index="1" value="18"/> <property name="Name" value="cm"/> <property name="PerSonAdress" ref="zhangsanPersonAdress"/> </object> <object name="zhangsanPersonAdress" type="SpringNet.Adress,SpringNet"> <property name="ProvinceName" value="安徽省"/> <property name="CityName" value="亳州市"/> <property name="CountyName" value="蒙城县"/> </object> </objects>
此时,在为myPerson注入PersonAdress属性的时候,要先去创建一个Adress对象,然户在myPerson中,将PersonAdress的属性值指向新创建的Adress对象即可;