Spring.Net框架介绍及基本配置(学习笔记一)

  Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。  

  Spring作为实现IOC或者说是DI的一种框架,为我们更好的降低程序的耦合性提供了一种方便的处理方式。当然也有其他一些实现IOC的框架如微软的Unity。至于何谓IOC、DI我这里也就不介绍了。有兴趣可以参考http://student.csdn.net/space.php?uid=706796&do=blog&id=39000

  首先声明,我写这个系列并不是因为自己很精通。只是自己的一个学习过程。如果有什么不对的地方,往专家踊跃拍砖!

  本节要点:

  1、Spring整体框架介绍

  2、Spring基本配置

  3、Spring注入方式

  4、配置的使用

  首先:我使用的Spring的版本为:Spring1.3。开发工具为:VS2008

  学习Spring,首先得学会如何配置它。可以从官网下载最新的安装包www.springframework.net.

  1、Spring的整体框架以及说明

  

  

  Spring.Core作为整个框架的基础,实现了依赖注入的功能。Spring.NET的大部分模块都要依赖或扩展该模块。Spring.Core的基础是IObjectFactory接口,该接口用一个简单而优雅的方式实现了工厂模式,使我们可以无需自行编写singleton类型和众多的服务定位器,并允许将对象配置及其依赖关系与具体的程序逻辑解耦。该模块中的IApplicationContext接口是IObjectFactory的扩展,增加了诸多企业级功能,包括使用资源文件进行文本本地化、事件传播和资源装载等等。

  Spring.AOP为业务对象提供面向方面编程(AOP)的支持。AOP完善了IoC容器的功能,为创建企业应用和使用声明式服务奠定了坚实的基础。

  Spring.Web对ASP.NET进行了一系列功能扩展,包括对ASP.NET页面进行依赖注入、双向数据绑定、在ASP.NET 1.1中使用Master page、以及增强的本地化功能支持等。

  Spring.Services允许将任意的“普通”对象(意为没有继承任何指定基类型的对象)发布为企业服务(COM+)或远程对象。通过依赖注入和特性元数据覆盖等功能,该模块可使.NET的Web服务获得极大的灵活性。同时也支持Windows后台服务。

Spring.Data定义了一个抽象的数据访问层,可以跨越各种数据访问技术(从ADO.NET到各种ORM)进行数据访问。该模块包含一个ADO.NET的抽象层,减少了使用传统ADO.NET进行编码和事务管理时的工作量。

Spring.ORM为时下流行的ORM类库提供了一个整合层,其中包含声明式事务管理等诸多功能。

  2、Spring基本配置:

  Spring的配置比起NHibernate算是比较简单了(个人意见)。同NHibernate一样,它的配置文件也是用XML完成的。如果你想在手工书写Spring配置文件时,有同NHibernate配置的智能感应一样,那你需要将Spring安装目录下(Spring.NET 1.3.0\doc\schema)的XML结构文件拷贝到VS的安装目录的架构文件中如:C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas。

  Spring的配置中有几个是必须的,并且在不同的应用中都一样。如:

 

代码
1 < section name = " context " type = " Spring.Context.Support.ContextHandler, Spring.Core " />
2   < section name = " objects " type = " Spring.Context.Support.DefaultSectionHandler, Spring.Core " />
3
4   < spring >
5    < context >
6    < resource uri = " config://spring/objects " />
7 </ context >
8    < objects xmlns = " http://www.springframework.net " xmlns:aop = " http://www.springframework.net/aop " ></ objects >
9   </ spring >

        其中,name属性以及其类型都是固定配置,不可更改。context节点、objects也一样。

  其他的便是根据具体应用所进行的配置。

3、注入方式:

  1、属性注入

    <property name="baz" ref="baz" ></property>
             <property name="bar" ref="bar"></property>

  2、构造注入

             <constructor-arg name="_bar"  ref="bar"></constructor-arg>
            <constructor-arg name="_baz"  ref="baz"></constructor-arg>

以上两种配置根据类的有无属性、构造函数进行。不过在其他资料中,别人说属性注入的方式比较常用。

全部配置文件如下:

  3、方法注入。

  通过其他类的方法来构造对象。配置如下:

 

代码
1 < object id = " objFactory " type = " ConsoleAppSpring.InjectionFactory,ConsoleAppSpring " >
2 < lookup - method name = " GetObject " object = " target " />
3 </ object >
4
5 < object id = " target " type = " ConsoleAppSpring.MethodInjection,ConsoleAppSpring " singleton = " 1 " >
6 < property name = " Name " value = " TestName " ></ property >
7 < property name = " Age " value = " 38 " ></ property >
8 </ object >

注意:通过属性注入时:类必须有无参数构造函数,否则会发生如下异常。通过构造注入时:可以无无参数构造函数,但是,构造属性

constructor-arg 的name属性值应为对应构造函数的参数

 

      以上注入方式配置文件如下:

代码
1 <? xml version = " 1.0 " encoding = " utf-8 " ?>
2   < configuration >
3 < configSections >
4 < sectionGroup name = " spring " >
5 < section name = " context " type = " Spring.Context.Support.ContextHandler,Spring.Core " />
6 < section name = " objects " type = " Spring.Context.Support.DefaultSectionHandler,Spring.Core " />
7 </ sectionGroup >
8 </ configSections >
9
10
11 < spring >
12 < context >
13 < resource uri = " config://spring/objects " />
14 </ context >
15
16 < objects xmlns = " http://www.springframework.net " xmlns:aop = " http://www.springframework.net/aop " >
17 < object id = " foo " type = " ConsoleAppSpring.Foo,ConsoleAppSpring " >
18 < property name = " baz " ref = " baz " ></ property >
19 < property name = " bar " ref = " bar " ></ property >
20 <!--< constructor - arg name = " _bar " ref = " bar " ></ constructor - arg >
21 < constructor - arg name = " _baz " ref = " baz " ></ constructor - arg >-->
22 </ object >
23
24 < object id = " bar " type = " ConsoleAppSpring.Bar,ConsoleAppSpring " >
25 < property name = " Heigth " value = " 27 " ></ property >
26 </ object >
27 < object id = " baz " type = " ConsoleAppSpring.Baz,ConsoleAppSpring " >
28 < property name = " Name " >
29 < value > tyb </ value >
30 </ property >
31 </ object >
32
33 < object id = " objFactory " type = " ConsoleAppSpring.InjectionFactory,ConsoleAppSpring " >
34 < lookup - method name = " GetObject " object = " target " />
35 </ object >
36
37 < object id = " target " type = " ConsoleAppSpring.MethodInjection,ConsoleAppSpring " singleton = " 1 " >
38 < property name = " Name " value = " TestName " ></ property >
39 < property name = " Age " value = " 38 " ></ property >
40 </ object >
41
42
43 </ objects >
44 </ spring >
45   </ configuration >

   在Objects节点的子节点Object中,ID代表类的标识,type代表类的具体类型。具体Foo、Bar、Baz三个类的代码一起给出:

 

代码
namespace ConsoleAppSpring
{
public class Foo
{
public Bar bar { get ; set ; }
public Baz baz { get ; set ; }

public Foo()
{ }

public Foo(Bar _bar, Baz _baz)
{
bar
= _bar;
baz
= _baz;


}
}
}

namespace ConsoleAppSpring
{
public class Bar
{
public string Heigth { get ; set ; }
}
}

namespace ConsoleAppSpring
{
public class Baz
{
// public List<string> Name { get; set; }
public string Name { get ; set ; }
}
}

namespace ConsoleAppSpring
{
public class MethodInjection
{
public string Name { get ; set ; }
public int Age { get ; set ; }

public MethodInjection()
{

}
public MethodInjection( string name, int age)
{
Name
= name;
Age
= age;
}
}
}

namespace ConsoleAppSpring
{
public class InjectionFactory
{
public string name = " method injection " ;
public int age = 32 ;

public virtual object GetObject()
{
return new MethodInjection(name, age);
}
}
}

 4、使用以上Spring的配置

  

代码
1 static void Main( string [] args)
2 {
3 IApplicationContext context = ContextRegistry.GetContext();
4 Foo foo = (Foo)context.GetObject( " foo " );
5 Console.WriteLine(foo.bar.Heigth);
6 Console.WriteLine(foo.baz.Name);
7
8 InjectionFactory factory = (InjectionFactory)context.GetObject( " objFactory " );
9 Console.WriteLine( " factory name is " + factory.name + " factory age is " + factory.age);
10 MethodInjection injection = (MethodInjection)factory.GetObject();
11 Console.WriteLine( " methodinjection name is {0}, age is {1}: " , injection.Name, injection.Age);
12 Console.ReadLine();
13
14 }

  

输出如下:

 

由上图可以看出MethodInjection的属性还是配置文件给出的值,不会因为InjectionFactory而改变

说明:IApplicationContext 、ContextRegistry需要引入 Spring.Context、 Spring.Context.Support命名空间。安装完Spring后,在安装的目录中能找到相应的程序集。IObjectFactory接口提供了配置框架和基本功能,IApplicationContext接口又在其基础上扩展了许多企业级特性。可以说IApplicationContext是IObjectFactory的一个超集,具备IObjectFactory所有的功能与行为。ContextRegistry类既可用来初始化应用程序上下文,也可用来以服务定位器风格对容器中的对象进行访问

参考:Spring.Net框架参考文档
     http://www.cnblogs.com/goodHelper

附:源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值