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、效果图

spring 属性注入的更多相关文章

  1. Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用

    Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...

  2. Spring 属性注入(二)BeanWrapper 结构

    Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...

  3. Spring 属性注入(三)AbstractNestablePropertyAccessor

    Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...

  4. Spring 属性注入(四)属性键值对 - PropertyValue

    Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...

  5. Spring属性注入、构造方法注入、工厂注入以及注入参数(转)

    Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...

  6. 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式

    Spring的属性注入: 构造方法的属性注入 set方法的属性注入

  7. spring属性注入

    1,set方法注入 (1)对于值类型的属性: 在对象中一定要有set方法 package com.songyan.demo1; import com.songyan.injection.Car; /* ...

  8. java spring属性注入

    一.创建对象时候,向类里面属性设置值:一般有三个方式 1) .有参构造, 2). set**** 3).接口注入 二. 在spring框架里面,支持前面两种方式: 1).有参构造方法  用constr ...

  9. spring属性注入DI

    spring setter方式注入: 注入对象属性: 前提: 在bean对应实体中有对应的setter方法. 基础代码: 在bean中有另一个bean属性的setter方法. package cn.i ...

随机推荐

  1. 文件编码、charset、sublime编辑器支持GBK等问题

    问题一:如何让sublime3支持GBK 首先打开package control ,然后键入install package进入,搜索ConvertToUTF8安装成功后 打开要查看的GBK文件,点击菜 ...

  2. PHP入门介绍与环境配置

    浏览目录: 一.PHP的背景和优势: 二.PHP原理简介: 三.PHP运行环境配置: 四.编写简单的PHP代码以及测试. 一.PHP的背景和优势 1.1   什么是PHP? PHP是能让你生成动态网页 ...

  3. python3 抓取网页资源的 N 种方法

    1. 最简单 import urllib.request response = urllib.request.urlopen('http://python.org/') html = response ...

  4. 随机数是骗人的,.Net、Java、C为我作证(转)

    几乎所有编程语言中都提供了"生成一个随机数"的方法,也就是调用这个方法会生成一个数,我们事先也不知道它生成什么数.比如在.Net中编写下面的代码: Random rand = ne ...

  5. 如何获取google地图、baidu百度地图的坐标

    google:打开google地图-->查找目的地-->右键:此位置居中-->地址栏键入javascript:void(prompt('',gApplication.getMap() ...

  6. Linux下安装Git

    Ubuntu12.04中默认没有安装Git.需要自行安装. 1. 安装Git 1.1 Ubuntu12.04下 可以使用apt-get方式安装,也可以下载源代码安装[1],我们这里使用apt-git安 ...

  7. 如何通过apk获得包名及Activiy 名称

    一.使用重签名工具Robotium

  8. CentOS7源码编译安装Postgresql9.5

    1.安装必要的基本软件 yum install -y gcc.x86_64 glibc.x86_64 glibc-devel.x86_64 vim-enhanced.x86_64 gcc-java a ...

  9. js中网页高度与宽度那些事

    网页可见区域宽:document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域宽:document.body.offsetW ...

  10. 【php】命名空间 和 自动加载的关系

    目的 本文的目的主要是说明 命名空间的 use 关键词 和 new ClassName 这两个步骤,哪个步骤才会执行自动加载,这是逻辑有点混乱的表现,这种想法也是很正常的,让我们来解密吧 命名空间(n ...