Spring.Net学习笔记(1)-容器的使用
一、下载地址:
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)-容器的使用的更多相关文章
- 【转】Spring.NET学习笔记——目录
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring.NET学习笔记——目录(原)
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...
- spring揭密学习笔记
spring揭密学习笔记 spring揭密学习笔记(1) --spring的由来 spring揭密学习笔记(2)-spring ioc容器:IOC的基本概念
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
- Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建
之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...
- Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建
Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...
随机推荐
- cogs——73. 找最佳通路
73. 找最佳通路 ★☆ 输入文件:city.in 输出文件:city.out 简单对比时间限制:1 s 内存限制:128 MB 问题描述有 n 个 城市,它们之间的交通情况已知.现在 ...
- Ubuntu 16.04安装网络流量监控工具Netspeed(附带10款最佳的指示器工具)
安装: sudo add-apt-repository ppa:ferramroberto/linuxfreedomlucid sudo apt-get update sudo apt-get ins ...
- 【.Net 学习系列】-- FileSystemWatcher 监控文件夹新生成文件,并在确认文件没有被其他程序占用后将其移动到指定文件夹
监控文件夹测试程序: using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...
- golang time.Duration()的问题解疑
原文: How to multiply duration by integer? 看到golang项目中的一段代码, ---------------------------------------- ...
- [转] Ubuntu/Linux Mint/Debian 安装 Java 8
本PPA由webupd8制作,支持Ubuntu .04以及对应的Linux Mint版本,Oracle Java 8包提供JDK8 和 JRE8. sudo add-apt-repository pp ...
- MySQL-子查询,派生表,通用表达式
MySQL-子查询 MySQL子查询是嵌套在另一个查询中的查询. MySQL子查询还可以嵌套在另一个子查询中. MySQL子查询称为内部查询,而包含子查询的查询称为外部查询. 查询返回在位于美国(US ...
- [git push] rejecteded 问题的解决方法
错误信息: hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart ...
- [办公自动化]如何在windows7中编辑hosts文件 (提示权限不够)
请按如下步骤尝试: 1.在开始菜单里,单击“所有程序”,找到“附件”,单击找到里面的“记事本”,右键,然后选择“以管理员身份运行”,如果有对话框,选择“是”.2.然后单击记事本窗口的“文件”菜单,选择 ...
- Eclipse Android环境配置
1.离线安装ADT插件,先将ZIP包下载 Help- Install New Software- Add 重启 2.WIndows -Preference设置SDK目录
- HDU 4445 数学-抛物运动
D - Crazy Tank ...