一、下载DLL文件

去Spring的官方网站下载并解压,然后直接添加dll文件的引用就可以了。在上一篇文章中,已经介绍过Spring.Net框架中需要使用到的dll文件。这些程序集文件位于Spring.NET-1.3.1\Spring.NET\bin\net\4.0\debug或Spring.NET-1.3.1\Spring.NET\bin\net\4.0\release中。

二、编程方式的容器

在Spring.Net中,对于通过编程方式使用容器的环境,提供了Spring.Context.Support.StaticApplicationContext,我们可以直接创建这个容器,并加入一些配置。在下面的例子中,我们定义了一个接口 IAnimal,然后定义了两个类Dog和Cat,分别实现IAnimal接口:

IAnimal接口:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SpringDemo
{
/// <summary>
/// 动物接口
/// </summary>
public interface IAnimal
{
/// <summary>
/// 吃的方法
/// </summary>
void Eat();
}
}

Dog类:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SpringDemo
{
/// <summary>
/// 定义Dog类实现IAnimal接口
/// </summary>
public class Dog:IAnimal
{
/// <summary>
/// 实现吃的方法
/// </summary>
public void Eat()
{
Console.WriteLine("狗在吃饭");
}
}
}

Cat类:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SpringDemo
{
/// <summary>
/// 定义Cat类实现IAnimal接口
/// </summary>
public class Cat:IAnimal
{
/// <summary>
/// 实现吃的方法
/// </summary>
public void Eat()
{
Console.WriteLine("猫在吃饭!");
}
}
}

主程序中调用:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SpringDemo
{
class Program
{
static void Main(string[] args)
{
// 创建容器
Spring.Context.Support.StaticApplicationContext context = new Spring.Context.Support.StaticApplicationContext();
// 注册狗类
context.RegisterPrototype("IAnimal", typeof(Dog), null);
IAnimal animal = context.GetObject("IAnimal") as IAnimal;
animal.Eat();
Console.ReadKey();
}
}
}

结果:

如果想调用Cat类的Eat()方法,只需要修改注册的代码即可:

// 注册Cat类
context.RegisterPrototype("IAnimal", typeof(Cat), null);

三、XML方式容器

在开发中,我们通常通过XML配置文件来完成配置。Spring.Net提供了Spring.Context.Support.XmlApplicationContext,此时,对象的配置信息写在一个XML的配置文件中,这个XML的配置文件有特定的格式,这些规定以XML Schema的形式保存在Spring.NET-1.3.1\Spring.NET\doc\schema文件夹的spring-objects-1.3.xsd中。

对于上面的例子,我们可以编写如下的配置文件ObjectSchema.xml:

 <?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="bll" type="SpringDemo.Dog,SpringDemo"></object>
</objects>

然后在代码中就可以直接使用容器了:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SpringDemo
{
class Program
{
static void Main(string[] args)
{
// 直接使用容器:路径使用的相对路径
Spring.Context.Support.XmlApplicationContext context = new Spring.Context.Support.XmlApplicationContext("ObjectSchema.xml");
IAnimal animal = context.GetObject("bll") as IAnimal;
animal.Eat();
Console.ReadKey();
}
}
}

如果想使用Cat类,直接修改ObjectSchema.xml文件就可以,把type修改为:type="SpringDemo.Cat,SpringDemo"。

注意:

上面的代码中加载XML文件使用的是相对路径,要修改XML的属性,把复制到输出目录改成“始终复制”,否则加载XML文件的时候会提示找不到文件的错误。或者使用XML文件的绝对路径。

四、通过应用程序配置文件来自动加载Spring.Net配置

Spring.Net提供了Spring.Context.Support.ContextHandler帮助我们直接在启动程序的时候加载配置信息。实际的配置文件通过spring节点中context节点下的resource节点的uri属性的值指定,文件的话使用file://协议描述,还可以使用其他的协议。例如嵌入在程序集中的配置文件可以使用assembly://,直接写在配置文件中则为config://。

配置文件:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<!--定义上下文切面-->
<section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/>
</sectionGroup>
</configSections>
<spring>
<context>
<!--使用ObjectSchema.xml文件里面的配置信息-->
<resource uri="file://ObjectSchema.xml"/>
</context>
</spring>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
</configuration>

程序中直接使用:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SpringDemo
{
class Program
{ static void Main(string[] args)
{ Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext();
IAnimal animal = context.GetObject("bll") as IAnimal;
animal.Eat();
Console.ReadKey();
}
}
}

如果想使用其他实现类,直接修改ObjectSchema.xml文件即可。

五、将所有的配置信息都保存在应用程序配置文件中
还可以不再使用另外的Spring配置文件(即ObjectSchema.xml),而是将所有的配置信息都保存在应用程序配置文件中。
这需要使用一个新的配置处理器Spring.Context.Support.DefaultSectionHandler,它可以帮助我们解析spring配置信息。
此时的配置文件改成如下的形式,注意:现在的resource中使用config://表示使用配置文件中的信息。

在基于XML的工厂中,这些对象定义表现为一个或多个<object>子节点,它们的父节点必须是<objects>(按:objects节点的xmlns元素是必需的,必须根据不同的应用添加不同的命名空间,以便有IDE的智能提示。

 <?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>
<!--使用ObjectSchema.xml文件里面的配置信息-->
<resource uri="config://spring/objects"/>
</context>
<objects>
<object id="bll" type="SpringDemo.Cat,SpringDemo" />
</objects>
</spring>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
</configuration>

主程序中调用:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SpringDemo
{
class Program
{
static void Main(string[] args)
{ Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext();
IAnimal animal = context.GetObject("bll") as IAnimal;
animal.Eat();
Console.ReadKey();
}
}
}

示例代码下载地址:http://files.cnblogs.com/files/dotnet261010/SpringDemo.rar

Spring.Net框架二:配置Spring.Net框架环境的更多相关文章

  1. Spring Boot 2 (二):Spring Boot 2 动态 Banner

    Spring Boot 2 (二):Spring Boot 2 动态 Banner Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner. 一.配置依赖 使用 Sp ...

  2. Spring Boot(十二):spring boot如何测试打包部署

    Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...

  3. Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC

    内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...

  4. SSM框架的配置Spring+Springmvc +Mybatis

    ssm框架是由spring mvc +spring+mybatis组成 快速阅读 通过spring的配置文件spring.xml,在servlet中指定spring mvc的配置文件spring-mv ...

  5. Spring知识点总结(二)之Spring IOC

    1.创建bean类,并在spring中进行配置交由spring来管理1. IOC(DI) - 控制反转(依赖注入)    所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管 ...

  6. Spring入门(十二):Spring MVC使用讲解

    1. Spring MVC介绍 提到MVC,参与过Web应用程序开发的同学都很熟悉,它是展现层(也可以理解成直接展现给用户的那一层)开发的一种架构模式,M全称是Model,指的是数据模型,V全称是Vi ...

  7. Spring实战(二)Spring容器和bean的生命周期

    引入问题: 在XML配置文件中配置bean后,这些文件又是如何被加载的?它们被加载到哪里去了? Spring容器——框架核心 1.什么是Spring容器?它的功能是什么? 在基于Spring的应用中, ...

  8. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  9. Spring学习(二)——Spring中的AOP的初步理解

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  10. 【spring教程之二】spring注射剂xml构造方法参数

    1.上述续,假设你想注入bean当文件,传递给构造函数的参数.主要的变化是需要spring.xml配置文件来配置. <?xml version="1.0" encoding= ...

随机推荐

  1. Ubuntu 中/etc/resolv.conf 文件修改丢失的解决方案

    方法一 1.需要创建一个文件/etc/resolvconf/resolv.conf.d/tail sudo vi /etc/resolvconf/resolv.conf.d/tail 2.在该文件中写 ...

  2. haproxy综合

    常见问题:haproxy启动报错Starting proxy : cannot bind socket?答:查看haproxy.conf配置文件发现其监听80跟apache冲突,而apache没在使用 ...

  3. android 开源组件合集-UI篇(2013-11-07更新)

    其实也算不上合集,只是将我经常用到的部分整理一下,如果您有好东西,也可以留言补充 1.actionbar http://actionbarsherlock.com/ https://github.co ...

  4. UDP Sockets in C#

    UDP provides an end-to-end service different from that of TCP. In fact, UDP performs only two functi ...

  5. tomcat服务器开启gzip功能的方法

    http://blog.csdn.net/wang_159369/article/details/8107163 tomcat5.0以后的版本是支持对输出内容进行gzip格式的压缩的.该功能默认是关闭 ...

  6. 【转发】jQuery1.9.1至最高版本针对checkbox的调整

    在jquery 1.8.x中的版本,我们对于checkbox的选中与不选中操作如下: 判断是否选中 $('#checkbox').prop('checked') 设置选中与不选中状态: $('#che ...

  7. 〖Linux〗ltib的使用帮助

    scue@Link:/home/work/ltib$ ./ltib --help This script is used to manage the building of BSPs with com ...

  8. Ubuntu下开启root登陆--并开启SSH登录验证

    Ubuntu下开启root登陆亲手安装过Ubuntu的童鞋都知道,默认安装只会添加一个普通用户名和密码,而超级用户权限则是利用sudo命令来执行.在Ubuntu下使用root登陆或者在shell中用s ...

  9. 推荐算法——非负矩阵分解(NMF)

    一.矩阵分解回想 在博文推荐算法--基于矩阵分解的推荐算法中,提到了将用户-商品矩阵进行分解.从而实现对未打分项进行打分. 矩阵分解是指将一个矩阵分解成两个或者多个矩阵的乘积.对于上述的用户-商品矩阵 ...

  10. QQ在通信与传输的一些知识

    http://www.nowamagic.net/librarys/veda/detail/2028 一.登录 不管UDP还是TCP,最终登陆成功之后,QQ都会有一个TCP连接来保持在线状态.这个TC ...