浅析Spring.NET
一、Spring.NET 简单使用
最近用到了 Spring.NET ,不过在第一次使用的时候就遇到了一些问题,打算整理成系列博客,不断的总结和分享。
同时也非常感谢在学习过程中给予我非常多帮助的前辈们,具体太多可能不能一一列举,不过如果是看到前辈们的帮助后解决的问题,会在博客中挂上相应文章的链接。 Coding and Share!
1. 什么是 Spring.NET ?
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库中。
2.快速创建第一个使用 Spring.NET 的程序
本次开发环境: VS 2017 本次开发项目:.netframework控制台项目
(1). 使用 Nuget 安装 Spring.core 包
使用 Spring.NET 需要 Spring.Core 库的支持,同时在 Nuget 中安装 Spring.Core 包,会在项目上自动引入相关的引用。
(2).创建相关文件。
本次创建两个文件:IUserInfo 接口、UserInfo 实体类。使用 Spring 反射创建 UserInfo 类,使用 IUserInfo 接口调用反射创建的类。
//IUserInfo接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
public interface IUserInfo
{
string ShowMss();
}
}
//UserInfo实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
//实现 IUserInfo接口
public class UserInfo : IUserInfo
{
public string ShowMss()
{
return "Hello Spring.NET";
}
}
}
重要的是在配置节里面的配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--sectionGroup节点一定要在紧跟着configuration下面第一个添加-->
<sectionGroup name="spring">
<!--跟下面Spring.Net节点配置是一一对应关系-->
<section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/>
</sectionGroup>
</configSections>
<!--Spring.Net节点配置-->
<spring>
<!--容器配置-->
<context>
<resource uri="config://spring/objects"/>
</context>
<objects>
<!--objects里面放容器的所有节点-->
<description>An example that demonstrates simple Ioc features.</description><!--描述-->
<!--name 必须要唯一的,type = 类的全名称,所在的程序集-->
<object name="UserInfo" type="Demo.UserInfo,Demo"></object><!--咱们刚才创建的UserInfo实体类-->
</objects>
</spring>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>
获取并执行:
//Program 程序入口类
//引用 : Spring.Context 和 Spring.Context.Support 两个命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spring.Context;
using Spring.Context.Support;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
IUserInfo userInfo = (IUserInfo)ctx.GetObject("UserInfo");
Console.WriteLine(userInfo.ShowMsg());
Console.Read();
}
}
}
输出结果:
注意事项:
- 必须安装 Spring.core 包,否则缺少环境支持。
- 需要正确 配置 配置文件:
sectionGroup
节点一定要在紧跟着configuration下面第一个添加.objects
节点中的<object></object>
节点,name="value"
value 值一定要唯一,type ="value1 , value2"
中 value1 是需要反射创建出来的类的全名称,value2 是该类所在的程序集。- 反射创建。
打完收工,后续再补充。