Spring.NET IoC容器的用法。
通过简单的例子学习Spring.Net
1、先创建一个控制台程序项目。
2、添加IUserInfoDal 接口。
namespace Spring.Net { public interface IUserInfoDal { void Show(); } }
3、添加AdoNetUserInfoDal类和EFUserInfoDal类,继承IUserInfoDal接口。
AdoNetUserInfoDal.cs
public class AdoNetUserInfoDal:IUserInfoDal { public void Show() { Console.WriteLine("我是 AdoNet Dal ); } }
EFUserInfoDal.cs
public class EFUserInfoDal: IUserInfoDal { public void Show() { Console.WriteLine("我是EF Dal); } }
4、引用Spring.Net程序集 Spring.Core.dll 和 Common.Logging.dll
5、添加Spring.Net配置节点,配置object节点
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!--一定要在紧跟着configuration下面添加--> <configSections> <!--跟下面Spring.Net节点配置是一一对应关系--> <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> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <!--Spring.Net节点配置--> <spring> <context> <!--容器配置--> <resource uri="config://spring/objects"/> </context> <objects xmlns="http://www.springframework.net"> <!--这里放容器里面的所有节点--> <description>An example that demonstrates simple IoC features.</description> <!--name 必须要唯一的,type=类的全名称,所在的程序集--> <object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net"> </object> </spring> </configuration>
6、开始写主函数,创建spring容器上下文
namespace Spring.Net { class Program { static void Main(string[] args) { //控制权没有反转 //IUserInfoDal infoDal = new EFUserInfoDal(); //Spring.Net 创建实例的方式转为容器帮我们创建 //创建spring容器上下文 IApplicationContext ctx = ContextRegistry.GetContext(); //通过容器创建对象 IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal; efDal.Show(); Console.ReadKey(); } } }
7、属性注入
<!--Spring.Net节点配置--> <spring> <context> <!--容器配置--> <resource uri="config://spring/objects"/> </context> <objects xmlns="http://www.springframework.net"> <!--这里放容器里面的所有节点--> <description>An example that demonstrates simple IoC features.</description> <!--name 必须要唯一的,type=类的全名称,所在的程序集--> <object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net"> <property name="Name" value="张三"/> <!--ref指向下面的属性注入--> <property name="UserInfo" ref="UserInfo"/> </object> <!--属性注入--> <object name="UserInfo" type="Spring.Net.UserInfo, Spring.Net"> <property name="Name" value="李四"/> <property name="Age" value="15"/> </object> </objects> </spring>
8、构造函数注入
<!--构造函数注入--> <object name="UserInfoDal2" type="Spring.Net.AdoNetUserInfoDal, Spring.Net"> <constructor-arg index="0" value="张三"/> <constructor-arg index="1" ref="UserInfo"/> </object>
9、容器配置
<context> <!--容器配置--> <resource uri="config://spring/objects"/> <!--xml文件方式,更改属性,复制到输出目录:始终复制--> <!--<resource uri="file://objects.xml"/>--> <!--嵌入程序集方式,assembly://程序集名/项目名/objects.xml,更改属性,始终复制,生成操作,嵌入的资源--> <!--<resource uri="assembly://Spring.Net/Spring.Net/objects.xml"/>--> </context>
10、完整例子
IUserInfoDal.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Spring.Net { public interface IUserInfoDal { UserInfo UserInfo { get; set; } string Name { get; set; } void Show(); } }
AdoNetUserInfoDal.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Spring.Net { public class AdoNetUserInfoDal:IUserInfoDal { public AdoNetUserInfoDal(string name, UserInfo userInfo) { Name = name; UserInfo = userInfo; } public UserInfo UserInfo { get; set; } public string Name { get; set; } public void Show() { Console.WriteLine("我是 AdoNet Dal ,属性注入:Name=" + Name); Console.WriteLine("UserInfo ,Name=" + UserInfo.Name + " Age=" + UserInfo.Age); } } }
EFUserInfoDal.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Spring.Net { public class EFUserInfoDal: IUserInfoDal { public EFUserInfoDal() { } public UserInfo UserInfo { get; set; } public string Name { get; set; } public void Show() { Console.WriteLine("我是EF Dal,属性注入:Name=" + Name); Console.WriteLine("UserInfo ,Name=" + UserInfo.Name + " Age=" + UserInfo.Age); } } }
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!--一定要在紧跟着configuration下面添加--> <configSections> <!--跟下面Spring.Net节点配置是一一对应关系--> <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> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <!--Spring.Net节点配置--> <spring> <context> <!--容器配置--> <resource uri="config://spring/objects"/> <!--xml文件方式,更改属性,复制到输出目录:始终复制--> <!--<resource uri="file://objects.xml"/>--> <!--嵌入程序集方式,assembly://程序集名/项目名/objects.xml,更改属性,始终复制,生成操作,嵌入的资源--> <!--<resource uri="assembly://Spring.Net/Spring.Net/objects.xml"/>--> </context> <objects xmlns="http://www.springframework.net"> <!--这里放容器里面的所有节点--> <description>An example that demonstrates simple IoC features.</description> <!--name 必须要唯一的,type=类的全名称,所在的程序集--> <object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net"> <property name="Name" value="张三"/> <!--ref指向下面的属性注入--> <property name="UserInfo" ref="UserInfo"/> </object> <!--构造函数注入--> <object name="UserInfoDal2" type="Spring.Net.AdoNetUserInfoDal, Spring.Net"> <constructor-arg index="0" value="张三"/> <constructor-arg index="1" ref="UserInfo"/> </object> <!--属性注入--> <object name="UserInfo" type="Spring.Net.UserInfo, Spring.Net"> <property name="Name" value="李四"/> <property name="Age" value="15"/> </object> </objects> </spring> </configuration>
Program.cs
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //Spring.Aop.dll 面向切面编程 //Spring.Core.dll spring框架基础 //Common.Logging.dll 这个必须也要引用 namespace Spring.Net { class Program { static void Main(string[] args) { //控制权没有反转 //IUserInfoDal infoDal = new EFUserInfoDal(); //Spring.Net 创建实例的方式转为容器帮我们创建 //第一步,引用Spring.Net程序集 Spring.Core.dll 和 Common.Logging.dll //第二步,添加Spring.Net配置节点 //第三步,配置object节点 //第四步,创建spring容器上下文 IApplicationContext ctx = ContextRegistry.GetContext(); //第五步,通过容器创建对象 IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal; efDal.Show(); IUserInfoDal adoDal = ctx.GetObject("UserInfoDal2") as IUserInfoDal; adoDal.Show(); Console.ReadKey(); } } }
下面是MVC项目中使用Spring.Net融合的例子
1、将Spring.net程序集复制到项目下的packages文件夹中。
2、项目中添加引用Spring.Core.dll、Common.Logging.dll、Spring.Web.Mvc4.dll、Spring.Web.dll
3、修改Web.config,配置节点
<configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!-- Spring.Net配置节点 --> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/> </sectionGroup> </configSections> <!-- Spring.Net节点详细配置 --> <spring> <context> <resource uri="file://~/Config/controllers.xml"/> </context> </spring> </configuration>
4、项目中创建Config文件夹,controllers.xml文件。
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object type="HX.Shop.UI.Main.Controllers.AdminController, HX.Shop.UI.Main" singleton="false" > <property name="AdminService" ref="AdminService" /> </object> <object name="AdminService" type="HX.Shop.BLL.AdminService, HX.Shop.BLL" singleton="false" > </object> </objects>
5、Global.asax 中修改 SpringMvcApplication
public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication //System.Web.HttpApplication
摘自:http://www.cnblogs.com/han1982/p/4177850.html
自己学习的同时,写了个例子,通过360免费云盘进行分享
下载链接:https://yunpan.cn/cYWTKQcZUPnxn (提取码:880a)
开发环境:VS2013