使用Spring.Net对Web页面进行依赖注入

今天看到这篇文章 Unity&WebForm(1): 自定义IHttpHandlerFactory使用Unity对ASP.NET Webform页面进行依赖注入,这是一个很好的思路,自定义IHttpHandlerFactory结合Unity来对web页面进行依赖注入,大家可以去看看。但是回复中有人提出了问题,对于web site类型的站点,在配置文件中无法指定要注入类型和所在的程序集,也就无法使用Unity。

  现在的Unity并不支持对web页面的依赖注入,上文的作者也是利用一定的技巧来解决的。 其实Spring.Net已经支持对Web页面的依赖注入,而且并不用修改代码,只需要在配置文件中配置就行。而且它还支持对自定义的httpHandler,httpModule的依赖注入,不能不说它的功能强大。

  下面具体介绍如何使用Spring.Net:

1. 添加Spring的SectionGroup

< sectionGroup  name ="spring" >
       < section  name ="context"  type ="Spring.Context.Support.WebContextHandler, Spring.Web" />
       < section  name ="objects"  type ="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</ sectionGroup >  

2. 添加HttpHandler配置

< add  verb ="*"  path ="*.aspx"  type ="Spring.Web.Support.PageHandlerFactory, Spring.Web" />

3. 添加HttpModule配置(如果不添加的话,会有异常)

< add  name ="Spring"  type ="Spring.Context.Support.WebSupportModule, Spring.Web" />

4. 最后配置如何依赖注入

< spring >
     < context >
       < resource  uri ="config://spring/objects" />
     </ context >
     < objects  xmlns ="http://www.springframework.net" >
       < object  type ="Default.aspx" >
         < property  name ="Service"  ref ="myService" />
       </ object >
       < object  id ="myService"  type ="Services.MyService, Services" >
       </ object >
     </ objects >
   </ spring >

object的type属性表明你要对哪个页面进行注入,property的name属性是该页面中被注入的Property的名字,ref就指向了你想注入的实际类型。

由此可见,使用Spring.Net可以很方便的对web页面进行依赖注入。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值