惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 紫色阴影

Android ellipsize的小问题 腾讯招聘贴 有意向的人发邮件或者msn Asp.net MVC tips -- 也做为 要写漂亮的代码 (2) - 紫色阴影 要写漂亮的代码 (1) 在团队中如何推行一项新的实践 - 紫色阴影 事件驱动的javascript 验收测试的自动化 - 紫色阴影 项目中的技术风险 欢迎需求变动,拥抱变化 成为MVP 使用动态数据库访问对象 如何把握设计的尺度 初窥Managed Extensibility Framework - 紫色阴影 Bug驱动开发简介 Unity的属性注入 持续集成简介 简单介绍使用WCF的Web编程模型开发REST风格的Web Service 尝试使用WatiN进行TDD - 紫色阴影 关于站立式会议
使用Spring.Net对Web页面进行依赖注入
紫色阴影 · 2008-06-11 · via 博客园 - 紫色阴影

  今天看到这篇文章 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页面进行依赖注入。