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 ...
随机推荐
- JFileChooser 中文API
javax.swing类 JFileChooser java.lang.Object java.awt.Component java.awt.Container javax.swing.JCompon ...
- Java这样学,Offer随便拿,学习方法和面试经验分享
Java这样学,Offer随便拿,学习方法和面试经验分享 学习中:https://mp.weixin.qq.com/s/iSutLzqCiPMWwm_Rm_2oPw
- 实战恢复2950交换机的IOS
本来想用两台交换机做实验的,可是通过console口进入其中一台交换机后却发现这个台交换器的IOS文件丢失了 本来正常进入交换机后应该是首先进入到用户模式的,而且提示符应该是">&qu ...
- oralce之复杂查询举例
表结构: S(SNO,SNAME) 代表 学号.学生姓名: C(CNO,CNAME,CTEACHER) 代表 课号,课程名称.授课老师 SC(SNO,CNO,SCGRADE) 代表 学号.课号.课程成 ...
- Jenkins 使用
Jenkins 安装 Jenkins是用Java语言开发的系统,首先要确定服务器上已经安装JDK或者JRE. 安装方式一 直接运行java –jar Jenkins.war,在浏览器中输入 http: ...
- C# 最基本的涉及模式(单例模式) C#种死锁:事务(进程 ID 112)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务,解决方案: C#关闭应用程序时如何关闭子线程 C#中 ThreadStart和ParameterizedThreadStart区别
C# 最基本的涉及模式(单例模式) //密封,保证不能继承 public sealed class Xiaohouye { //私有的构造函数,保证外部不能实例化 private ...
- MySQL测试代码
MySQL测试代码 # 注释内容 -- 注释内容 -- 创建maizi数据库 CREATE DATABASE IF NOT EXISTS `maizi` DEFAULT CHARACTER SET ' ...
- CCBPM工作流引擎的消息机制与设计
keyword:ccflowjflow 消息机制流程引擎 自己主动发送短信 发送邮件 发送消息 流程引擎微信连接 消息接口 关于ccbpm: 我们把ccflow jflow两个版本号的工作流引擎统称为 ...
- POJ3254 状压dp
Corn ...
- 修改版Putty,可保持密码
修改版Putty,可保持密码: http://files.cnblogs.com/findumars/putty_v6.0.rar 转自: http://unmi.cc/putty-auto-logi ...