使用Spring.Net对Web页面进行依赖注入
下面具体介绍如何使用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>
<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>
<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页面进行依赖注入。