微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【ASP.NET】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

 

Ioc

Inversion of Control 控制反转 

 

DI

Dependency Injection 依赖注入

 

 

Spring.Net容器中

1.初始化容器对象

2.加载xml文件,放到内存里面

3.根据容器里面的xml配置创建对象

 

引入dll

Common.Logging.dll和Spring.Core.dll

 

新建项目,新建Test类,用于测试

public class Test
{
    public void Show()
    {
        Console.WriteLine("哈哈");
    }
}

 

在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="Test" type="SpringNetTest.Test, SpringNetTest"></object>
  </objects>
</spring>

其中object   SpringNetTest.Test表示类的全名称,SpringNetTest是类所在的程序集

正常情况下,不使用Spring.Net

Test test = new test();
test.Show();

使用Spring.Net

//初始化容器
IApplicationContext ctx = ContextRegistry.GetContext();
//请求定义的对象
Test test2 = (Test)ctx.Getobject("Test");
test2.Show();

 

当有很多个类时候,每个类都要在App.config配置

我们也可以引用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>

创建

Test test3 = (Test)ctx.Getobject("Test1");
test3.Show();

 

我们也可以读取程序集里面的xml文件

需要将xml文件设置为嵌入的资源

在App.config

<context>
  <resource uri="config://spring/objects"/>
  <resource uri="file://config.xml"/>
  <resource uri="assembly://SpringNetTest/SpringNetTest/config.xml"/>
</context>

创建

Test test4 = (Test)ctx.Getobject("Test1");
test4.Show();

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。