一、下载地址:

http://www.springframework.net/download.html

二、相关程序集

Spring.Net容器定义在程序集Spring.Core.dll中,它依赖于Common.Logging.dll。该程序集位于

Spring.NET-1.3.1\Spring.NET\bin\net\4.0\release目录下

三、创建容器

1.编程方式的容器

使用Spring.Context.Support.StaticApplicationContxt直接创建容器

public class Person
{
public string Name { get; set; }
public override string ToString()
{
return "This is Person";
}
}
class Program
{
static void Main(string[] args)
{
//创建容器
Spring.Context.Support.StaticApplicationContext context = new StaticApplicationContext(); //注册Person类
context.RegisterPrototype("Person", typeof(Person), null); //从容器中获取Person对象
Person person = context.GetObject("Person") as Person; Console.WriteLine(person);
}
}

2.使用xml方式(注意xml的输出方式)

处理xml的配置处理器:Spring.Context.Support.XmlApplicationContext

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="Person" type="CPrj.Person,Cprj"></object>
</objects>
class Program
{
static void Main(string[] args)
{
Spring.Context.Support.XmlApplicationContext context = new XmlApplicationContext("objects.xml");
Person person = context.GetObject("Person") as Person;
Console.WriteLine(person);
}
}

注:<object>的父节点必须是<objects>,且objects的xmlns元素是必须的

3.使用配置文件+xml文件

处理器Spring.Context.Support.ContextHandler在启动程序时候加载配置信息

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="Person" type="CPrj.Person,Cprj"></object>
</objects>
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"></section>
</sectionGroup>
</configSections> <spring>
<context>
<resource uri="file://objects.xml"></resource>
</context>
</spring> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
class Program
{
static void Main(string[] args)
{
Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext();
Person person = context.GetObject("Person") as Person;
Console.WriteLine(person);
}
}

注:context节点的默认type就是 Spring.Context.Support.XmlApplicationContext,Spring.Core 。故可省去

<spring>
<context type="Spring.Context.Support.XmlApplicationContext,Spring.Core">
<resource uri="file://objects.xml"></resource>
</context>
</spring>

4.使用配置文件

xml对象定义也可以放在DotNet的标准应用程序配置文件中,此时必须为<objects>节点预先注册相应的节点处理器

需要使用一个新的配置处理器:Spring.Context.Support.DefaultSectionHandler

它可以帮助我们解析spring配置信息

<?xml version="1.0" encoding="utf-8" ?>
<configuration> <configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"></section>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/>
</sectionGroup>
</configSections> <spring>
<context>
<resource uri="config://spring/objects"></resource>
</context>
<objects xmlns="http://www.springframework.net">
<object id="Person" type="CPrj.Person,CPrj"></object>
</objects>
</spring> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
class Program
{
static void Main(string[] args)
{
Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext();
Person person = context.GetObject("Person") as Person;
Console.WriteLine(person);
Console.ReadKey();
}
}

注:spring和context节点名称不是任意的,必须是spring和context。Spring.Net本身将“spring/context”作为字符串常亮定义在AbstractApplicationContext类中以表示上下文的节点名称

5.混合使用外部配置文件和嵌入的配置

<?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="file://objects.xml"/>
<resource uri="config://spring/objects"/>
</context>
<objects>
<object id="Person" type="CPrj.Person,CPrj"></object>
</objects>
</spring> </configuration>

四、参考文章

http://www.cnblogs.com/haogj/archive/2011/06/10/2077540.html

Spring.Net学习笔记(1)-容器的使用的更多相关文章

  1. 【转】Spring.NET学习笔记——目录

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  2. Spring.NET学习笔记——目录(原)

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  3. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

  4. spring揭密学习笔记

    spring揭密学习笔记 spring揭密学习笔记(1) --spring的由来 spring揭密学习笔记(2)-spring ioc容器:IOC的基本概念

  5. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

  6. Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建

    之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...

  7. Spring MVC 学习笔记一 HelloWorld

    Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...

  8. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

  9. Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建

    Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...

随机推荐

  1. [bzoj2982]combination_卢卡斯

    Combination bzoj-2982 题目大意:求$C_n^m/%10007$. 注释:$1\le n,m\le 2\cdot 10^9$. 想法:裸卢卡斯定理. 先处理出$mod$数之内的阶乘 ...

  2. JSP的调试

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/debugging.html: 一.使用System.out.println() System.out.p ...

  3. no matching function transform?

    http://stackoverflow.com/questions/19876746/stdtolower-and-visual-studio-2013 http://forums.codeguru ...

  4. Windows Update 的工具

    Windows Update MiniTool 是一款管理 Windows Update 的工具,可以取得微軟 Windows 修補程式更新包,Windows Update 是我們用來升級系統的元件, ...

  5. [React] Preview and edit a component live with React Live

    In this lesson we'll use React Live to preview and edit a component directly in the browser. React L ...

  6. 网页瞬间转换成桌面应用级程序(IOS/Win/Linux)

    首先下载node,并且安装. 安装检测 检测完成后,执行下面这条命令 npm i -g nativefier 安装完成后 执行下面的命令+网址即可生成任意的桌面级程序 示例:nativefier &q ...

  7. vux picker

    1.通过实验证明: PopupPicker = TransferDom + Popup + PopupHeader + Picker 2.代码 Picker.vue <!-- Picker 组件 ...

  8. eclipse到Android Studio的项目迁移

    一直以来.公司开发都是用eclipse.可是随着我们应用不断成长.项目结构越来越庞大.项目间依赖关系变得非常复杂.用eclipse管理显得非常吃力,常常一个同事更改依赖项目之后,别人在更新.都会出现故 ...

  9. iOS xcode6最新提交app方法

    依照之前方式打包.打包成功后.直接submit提交AppStore.然后再选择build,假设上传成功,但在build选择上未出现,你能够耐心等待.有可能要等上一天,然后选择相应的build,直接提交 ...

  10. C#项目的生成事件及批处理文件

    一个C#项目,如果为同一个解决方案的其他项目所引用,则其编译后,会将DLL拷贝到引用项目中:但如果它并不被其他项目引用,但又想编译后能够自动将生成的东西拷贝过去,可以在项目的生成事件中,写上一些批处理 ...