spring 属性注入
2024-10-19 08:59:21
Spring的核心技术室依赖注入,下面是依赖注入之属性注入的实现过程,牛刀小试,请看效果。
1、首先添加Spring.Web引用。本例中是使用分层思想来演示的,下面是项目的结构和UserModel类的内容,为了简单操作,只有一个Name属性
2、修改Web.Config文件,配置注入环境
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
<!-- 支持在 web.config 中定义对象 -->
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
添加Spring的SectionGroup
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpModules>
<!-- Spring 提供的 Module -->
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
</httpModules>
添加HttpModule配置
<httpHandlers>
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
<!-- 取消 Spring.NET 对于 Web 服务的处理 -->
<!--<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/>-->
<add verb="*" path="ContextMonitor.ashx" type="Spring.Web.Support.ContextMonitor, Spring.Web"/>
<add verb="*" path="*.ashx" type="Spring.Web.Support.DefaultHandlerFactory, Spring.Web"/>
</httpHandlers>
添加HttpHandler配置
</configSections>
<spring>
<context>
<!---外部文件注入-->
<!--<resource uri="Configration.xml"/>-->
<!--<resource uri="config://spring/objects"/>-->
</context> <objects>
<!--id为自定义的id和下面要注入的页面property节点下的framework要保持一致,type为要注入的属性的所在类 -->
<object id="framework" type="Spring.Model.UserModel,Spring.Model">
<!--name为注入的属性,value为属性的值-->
<property name="Name" value="Spring.NET"/>
</object> <!--页面注入-->
<object type="~/Default.aspx">
<property name="UserModel" ref="framework"></property>
</object>
</objects>
</spring>
依赖注入配置
3、实现注入
/// <summary>
/// 定义注入点
/// </summary>
public UserModel UserModel { set; get; } protected void Page_Load(object sender, EventArgs e)
{
if (this.UserModel != null)
{
Label1.Text = this.UserModel.Name;
}
}
实现注入
4、效果图
最新文章
- windows系统时间(SYSTEMTIME)
- node.js整理 04网络操作
- Asp.Net MVC4入门指南(3):添加一个视图
- 关于网站的UV分析
- 按字母顺序排序的 arcpy.mapping 类列表
- Vlc基础数据结构记录
- mfc删除标题和边框
- js架构设计模式——从angularJS看MVVM
- ArrayList源码解读
- 笔记:I/O流-内存映射文件
- 如何解决python升级后yum报错
- Jmeter之Non HTTP response code: java.net.SocketException/Non HTTP response message: Permission denied: connect
- npm 镜像源替换为淘宝镜像
- jQuery实现Ajax请求时,页面显示等待的效果,超过指定请求时间后,进行其他操作
- linux系统调用之网络管理1
- 【ARC063E】Integers on a tree
- python中sys.stdout、sys.stdin
- web项目中的执行流程参数传递详解
- 高级Bash脚本编程(一)
- C#基础之接口
热门文章
- 动态调用WebService(C#) (非常实用)
- poj1543-Perfect Cubes(暴力)
- cocos2d-x test学习[1]
- spring mvc异常统一处理(ControllerAdvice注解)
- finally类
- Mac&;iOS之多线程--转自http://geeklu.com/2012/02/thread/
- mysql, count函数容易曲解的地方
- PHP调用MYSQL存储过程实例
- 记一次程序排错与std::getline
- Ant和Maven的作用是什么?两者之间功能、特点有哪些区别?