Spring.Net快速入门
Spring.NET-2.0.0-M2以及中文文档
下载:
链接:https://pan.baidu.com/s/1TnMWP6wESllwlDE1Gdibqw
提取码:y0r5
Spring 框架本是 Java 平台上一个应用非常多的、开源的框架。虽然语言是固定的,但是好的方法应该是通用的,于是 Spring 框架 就被程序员从 Java 平台搬迁到了 .NET 平台。
通过Spring.NET,我们可以用统一且透明的方式来配置应用程序。Spring.NET 的重点是为中间层提供声明式事务管理,以及一个功能齐全的 ASP.NET 扩展框架。Spring.NET 是非侵入式的,代码对框架本身不会产生任何依赖。
Spring.NET 能够提供很多方面的功能,例如:控制反转(英文缩写为IoC)、依赖注入(DI)、面向方面编程(AOP)、数据访问抽象, 以及 ASP.NET 集成等。
Spring.NET 核心:
Spring.Core 库是框架的基础, 提供依赖注入功能。Spring NET中大多数类库依赖或扩展了Spring.Core的功能。IObjectFactory接口提供了一个简单而优雅的工厂模式,移除了对单例和一些服务定位stub的必要。允许你将真正的程序逻辑与配置解耦。作为对IObjectFactory 的扩展,IApplicationContext接口也在Spring.Core库中。
————————————————
引用自:https://blog.csdn.net/ruisenLi/article/details/83382267
代码入门
Spring.Net容器中
1.初始化容器对象
2.加载xml文件,放到内存里面
3.根据容器里面的xml配置创建对象
dll加载
Common.Logging.dll和Spring.Core.dll
在App.config中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"> <description>An example that demonstrates simple IoC features.</description> <object name="UserInfoDal" type="SpringNetDemo.UserInfoDal, SpringNetDemo"></object> </objects> </spring>
其中object SpringNetDemo.UserInfoDal表示类的全名称,SpringNetDemo是类所在的程序集
当有很多个类时候,每个类都要在App.config配置
使用Spring.Net
namespace SpringNetDemo { public interface IUserInfoDal { void Show(); string name { get; set; } } } namespace SpringNetDemo { public class UserInfoDal : IUserInfoDal { public UserInfoDal(string name) { this.name = name; } public string name { get;//=> throw new NotImplementedException(); set; //=> throw new NotImplementedException(); } public void Show() { Console.WriteLine("床前明月光"); } } }
//初始化容器 IApplicationContext ctx = ContextRegistry.GetContext(); //请求定义的对象 IUserInfoDal userInfoDal = ctx.GetObject("UserInfoDal") as UserInfoDal; userInfoDal.Show();
我们也可以引用xml文件
新建xml文件
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"> <object name="Test1" type="SpringNetTest.Test, SpringNetTest"></object> </objects>
设置xml文件 为 始终复制

在App.config中配置
<context> <resource uri="config://spring/objects"/> <resource uri="file://config.xml"/> </context>
我们也可以读取程序集里面的xml文件
需要将xml文件设置为嵌入的资源

在App.config中
<context> <resource uri="config://spring/objects"/> <resource uri="file://config.xml"/> <resource uri="assembly://SpringNetTest/SpringNetTest/config.xml"/> </context>
属性赋值
使用property设置类的属性值,这个属性可以直接使用
<object name="UserInfoDal" type="SpringNetDemo.UserInfoDal, SpringNetDemo"> <property name="name" value="静夜思"/> </object>
使用关联属性
namespace SpringNetDemo { public class UserInfoService { public IUserInfoDal UserInfoDal { get; set; } public void show() { Console.Write("UserInfoService:" + UserInfoDal.name); } } }
<object name="UserInfoService" type="SpringNetDemo.UserInfoService, SpringNetDemo"> <!--将上一个的UserInfoDal的实例引用到UserInfoService的UserInfoDal属性--> <property name="UserInfoDal" ref="UserInfoDal"/> </object>
问题

写Demo过程中发现configSections每个config文件中只允许存在一个,且必须是configuration节点下的第一个子节点

浙公网安备 33010602011771号