Spring.Net快速入门:控制翻转、依赖注入、面向切面编程 微软企业库Unity学习笔记(一)

Spring.Net主要功能:html


1.IoC:控制翻转(Inversion of Control)  理解成抽象工厂
翻转控制:就是建立对象的权利由开发人员本身控制New,转到了由容器来控制。

2.DI:依赖注入(Dependency Injection)
依赖注入:就是在经过容器开建立对象的时候,在对象的初始化是能够给一些属性、构造方法的参数等注入默认值(能够是复杂的类型).

3.AOP:面向切面编程  (相似:管道、MVC过滤器等)web

 

1、IoC示例Demo:spring

1.新建WinForm项目数据库

 

2.在解决方案下新建文件夹Lib,用来存放Spring.Net用到的dll和文件编程

Spring.Core.dll、Common.Logging.dll(Spring.Core.dll内部使用到的)、Spring.Core.pdb、Spring.Core.xml  app

 

3.首先添加Spring.Net核心dll:Spring.Core.dll 和 Common.Logging.dll引用框架

 

4.配置app.config文件:post

 

5.代码调用:学习

using Spring.Context; using Spring.Context.Support; using System; using System.Windows.Forms; namespace CZBK.HeiMaOA.SpringNetDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { IApplicationContext context = ContextRegistry.GetContext();//建立容器 IUserInfoService userInfoService = (IUserInfoService)context.GetObject("UserInfoService"); MessageBox.Show(userInfoService.ShowMsg()); } } }

UserInfoService类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CZBK.HeiMaOA.SpringNetDemo { public class UserInfoService : IUserInfoService { public string ShowMsg() { return "Hello World"; } } }
IUserInfoService接口:
namespace CZBK.HeiMaOA.SpringNetDemo { public interface IUserInfoService { string ShowMsg(); } }

6.效果:url

 

7.Demo源码下载:点击下载>>

 

2、DI:依赖注入示例Demo:

 

1.添加一个复杂类型:

namespace CZBK.HeiMaOA.SpringNetDemo { public class Person { public int Age { get; set; } } }

 

2.修改下以前代码:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CZBK.HeiMaOA.SpringNetDemo { public class UserInfoService : IUserInfoService { public string Name { get; set; } public Person Person { get; set; } public string ShowMsg() { return "Hello World:" + Name + " 年龄是:" + Person.Age; } } }

 

3.修改App.config配置:

<?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"/> </context> <objects xmlns="http://www.springframework.net"> <object name="UserInfoService" type="CZBK.HeiMaOA.SpringNetDemo.UserInfoService, CZBK.HeiMaOA.SpringNetDemo"> <property name="Name" value="张三"/>  <property name="Person" ref="Person"/> </object>  <object name="Person" type="CZBK.HeiMaOA.SpringNetDemo.Person, CZBK.HeiMaOA.SpringNetDemo"> <property name="Age" value="20"/> </object> </objects> </spring> </configuration>

 

4.运行效果:

 

5.源码下载:点击下载>>

 

3、改进:

将app.config中的objects节点配置放到一个单独的xml文件中存放

 

1.将objects节点剪切到objects.xml文件中:

<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object name="UserInfoService" type="CZBK.HeiMaOA.SpringNetDemo.UserInfoService, CZBK.HeiMaOA.SpringNetDemo"> <property name="Name" value="张三"/> <property name="Person" ref="Person"/> </object> <object name="Person" type="CZBK.HeiMaOA.SpringNetDemo.Person, CZBK.HeiMaOA.SpringNetDemo"> <property name="Age" value="20"/> </object> </objects>

2.修改app.confgi文件:

<?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 uri="file://objects.xml"/> <!--指定xml文件位置(这里程序会到bin\debug或release目录下找这个文件,须要修改下xml文件属性,改成始终复制到输出目录)--> </context>

    <objects xmlns="http://www.springframework.net"> <!--这个节点须要保留-->
    </objects>
</spring> </configuration>

3.修改objects.xml文件属性:

 

4.运行效果:

 

5.源码下载:点击下载>>

 

4、在MVC4中使用Spring.Net

1.Web工程添加dll引用: 点击下载须要的dll文件>>

2.在Web工程下新建Config文件夹和控制器xml文件:

 

3.控制器代码:

namespace WebApp.Controllers { public class UserInfoController : Controller { // // GET: /UserInfo/ //IUserInfoService userInfoService = new UserInfoService(); //修改为下面的: CZBK.HeiMaOA.IBLL.IUserInfoService userInfoService { get; set; } //这样就完成了Web层与BLL层的解耦 public ActionResult Index() { return View(); } }

 

4.controllers.xml文件配置内容:

<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object type="WebApp.Controllers.UserInfoController,WebApp" singleton="false" > <!--指定命名空间、程序集、是否单例--> <property name="userInfoService" ref="userInfoService" /> <!--配置UserInfoController中用到的复杂类型userInfoService--> </object>

  <object type="CZBK.HeiMaOA.BLL.UserInfoService,CZBK.HeiMaOA.BLL" singleton="false" >
  </object> </objects>

 

5.修改Web工程中的web.config文件,添加以下节点:

 <configSections>
    <!--Spring.Net配置-->
    <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/> <!--指定为MVC4--> </sectionGroup> </configSections> <!--Spring.Net配置--> <spring> <context> <resource uri="file://~/Config/controllers.xml"/> <!--指定控制器xml文件--> </context> </spring> <!--Spring.Net配置结束-->

 

6.源码下载:

点击下载源码>>

点击下载数据库文件>>

 

5、小结:

Spring.Net实质上底层就是用反射去读取配置文件,之后须要变更就不须要修改代码,直接修改配置文件就能够了,方便灵活。

 

6、Spring.Net中文帮助文档(联网版):

点击下载>>

 

7、扩推荐:

相似的框架还有微软的Unity,也很不错。推荐博文:

微软企业库Unity学习笔记(一)

相关文章
相关标签/搜索