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 ...
随机推荐
- Ubuntu 16.04安装TortoiseSVN(基于CrossOver)
基于CrossOver的TortoiseSVN有如下缺点: 1.不能像Windows下实现右键菜单提交,但是可以通过TortoiseSVN实现迁出代码. 可以解决的问题: 1.可以通过Tortoise ...
- Ionic3 填坑记录 - java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
1 错误:Unable to merge dex 执行打包命令时 ionic cordova build android --prod 报如下错误 2 原因 重复引用了同一个包 如上图所示, com ...
- PyMySQL - Python3 MySQL数据库连接 - 基本操作
一.Python连接MySQL数据库 1.导入模块 #导入模块 import pymysql 2.打开数据库连接 #打开数据库连接 #注意:这里已经假定存在数据库testdb,db指定了连接的 ...
- centos下性能分析工具perf的安装和简单使用
1.安装: cat /etc/redhat-releaseCentOS release 6.6 (Final) sudo yum install perf 2.
- ubuntu VSFTPD搭建FTP服务器 提示530错误
配置完 vsftpd ,发现不能登录,提示 530 错误.解决方法如下: sudo rm /etc/pam.d/vsftpd 注:因为 ubuntu 启用了 PAM,所在用到 vsftp 时需要用到 ...
- [AngularJS] ocLazyLoad -- Lazy loaded module should contain all the dependencies code
Recentlly works with AngularJS + ocLazyLoad, our project have break down into multi small modules. F ...
- 《编程导论(Java)·3.1.2 方法》之 副作用
4. 副作用 在一些语言如Pascal中,子程序被分成两种:函数和过程.尽管Java没有强制性地要求将方法区分为命令和函数.然而这样的差别对于良好地设计程序有非常大的帮助[1]. 首先说明一个概念:副 ...
- vux 全局使用 loading / toast / alert
1.入口文件 main.js import { LoadingPlugin, ToastPlugin, AlertPlugin } from 'vux' Vue.use(LoadingPlugin); ...
- td里面嵌套img标签后如何消除图片间隔
td里面嵌套image标签后如何消除图片间隔 CreateTime--2018年3月7日16:18:12 Author:Marydon 情景还原: <body> <div sty ...
- 【剑指Offer学习】【面试题65:滑动窗体的最大值】
题目:给定一个数组和滑动窗体的大小,请找出全部滑动窗体里的最大值. 举例说明 比如,假设输入数组{2,3,4,2,6,2,5,1}及滑动窗体的大小.那么一共存在6个滑动窗体,它们的最大值分别为{4,4 ...