一、下载地址:

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. zoj3988 Prime Set

    思路不难想到二分图求个最大匹配P,若P>=K,则2*K即可,否则应为P*2+min(K-P,未匹配且有度数不为0的顶点个数s).但坑点在于有1的情况,所以如果直接建二分图去跑最大匹配会因为1的影 ...

  2. CAS--CompareAndSwap原理

    1.CAS(Compare-and-Swap),即比较并替换,是一种实现并发算法时常用到的技术,Java并发包中的很多类都使用了CAS技术. CAS需要有3个操作数:内存地址V,旧的预期值A,即将要更 ...

  3. Ubuntu 16.04升级Linux内核为4.7.0最快的方法

    升级内容有很多好处,比如支持最新硬件驱动,使系统更安装等.但是升级内容也会带来一些问题,比如一些软件的兼容性问题,从而出现一些莫名其妙的问题等,所以升级时要慎重考虑. 升级方法: 下载脚本: http ...

  4. 【SQL Server 学习系列】-- 收缩数据库文件大小

    USE WebExam; GO ALTER DATABASE WebExam SET RECOVERY SIMPLE; GO -- 收缩文件到 1 MB. ); GO ALTER DATABASE W ...

  5. Django学习系列之模板

    什么是django模板 模板是一个文本,用于分离文档的表现形式和内容,模板定义了占位符以及各种用于规范文档该如何显示的各部分基本逻辑(模板标签) 模板通常用于产生HTML 如何使用模板 创建一个Tem ...

  6. 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

    题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...

  7. php删除数组中指定值的元素

    php删除数组中指定值的元素 /** * 删除数组中指定值的元素 * @author: ibrahim * @param array $arr 数组 * @param string $val 值 * ...

  8. HDU4930-Fighting the Landlords

    题意:斗地主,就是要自己出牌.使得对手在这一轮无法出牌,或者有出牌的可能.可是你的牌已经走完了.假设符合这些条件的话,输出Yes.否则输出No. 思路:先预处理能直接把牌走完的情况,假设不行的话就直接 ...

  9. java8-接口变化-默认方法-静态方法

    Java 8中允许接口中包含具有具体实现的方法,该方法称为 “默认方法”,默认方法使用 default 关键字修饰. default String getName(){ return "哈哈 ...

  10. POJ 1060:Modular multiplication of polynomials

    Modular multiplication of polynomials Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4 ...