当前位置: 首页 > news >正文

Spring.NET学习笔记7——依赖对象的注入(基础篇) (转)

  一、属性注入

  上篇我们简单提到依赖注入的用途。回顾一下所讲内容,发现在object节点下使用了<property name="Tool" ref="computer"/>。而property 标签正是用来属性注入的。而ref是用来标识是关联到哪个object。而name属性是指属性名。如下:<object id="modernPerson" type="SpringNetIoC.ModernPerson, SpringNetIoC">
        
<property name="Tool" ref="computer"/>
</object>

值类型的注入是需要使用property 节点的value属性。如<property name="Name" value="Liu Dong"/>

作为内联类型可以使用如下:
<property name="Friend">
          <object type="SpringNetDi.Person, SpringNetDi"/>
</property>
同理,内联类型可以是循环引用的对象(见代码处)。

  二、构造函数注入

构造器注入使用constructor-arg标签作为标识。同样具有于属性注入相同的方式,使用namerefvalue作为构造器注入的属性,如下:
<constructor-arg name="argPerson" ref="person"/>
<constructor-arg name="intProp" value="1"/>


 

  程序的代码如下:

 

None.gif      public   class  Person
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public string Name dot.gifgetset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public int Age dot.gifgetset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Person Friend dot.gifgetset; }
ExpandedBlockEnd.gif    }

 

ContractedBlock.gif ExpandedBlockStart.gif PersonDao
    public class PersonDao
    {

        
private Person argPerson;
        
private int intProp;

        
public PersonDao(Person argPerson, int intProp)
        {
            
this.argPerson = argPerson;
            
this.intProp = intProp;
        }

        
public void Get()
        {
            
//构造函数注入的整型参数
            Console.WriteLine(string.Format("intProp:{0}", intProp));

            
//构造函数注入的Person
            Console.WriteLine(string.Format("argPerson Name:{0}", argPerson.Name));
            Console.WriteLine(
string.Format("argPerson Age:{0}", argPerson.Age));

            
//内联对象Friend
            Console.WriteLine(string.Format("argPerson Friend Name:{0}", argPerson.Friend.Name));
            Console.WriteLine(
string.Format("argPerson Friend Age:{0}", argPerson.Friend.Age));

            
//内联对象的循环引用
            Console.WriteLine(string.Format("argPerson Friend Friend Name:{0}", argPerson.Friend.Friend.Name));
            Console.WriteLine(
string.Format("argPerson Friend Friend Age:{0}", argPerson.Friend.Friend.Age));
        }
    }

 

ContractedBlock.gif ExpandedBlockStart.gif App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

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

  
<spring>

    
<context>
      
<resource uri="config://spring/objects" />
    
</context>

    
<objects xmlns="http://www.springframework.net">

      
<object id="person" type="SpringNetDi.Person, SpringNetDi">
        
<!--属性值类型注入-->
        
<property name="Name" value="Liu Dong"/>
        
<property name="Age" value="27"/>

        
<!--内联对象注入-->
        
<property name="Friend">
          
<object type="SpringNetDi.Person, SpringNetDi">
            
<property name="Name" value="Beggar"/>
            
<property name="Age" value="23"/>
            
<property name="Friend" ref="person"/>
          
</object>
        
</property>
        
      
</object>

      
<object id="personDao" type="SpringNetDi.PersonDao, SpringNetDi">
        
<!--构造器注入-->
        
<constructor-arg name="argPerson" ref="person"/>
        
<constructor-arg name="intProp" value="1"/>
        
      
</object>

    
</objects>

  
</spring>

</configuration>

 

 

ContractedBlock.gif ExpandedBlockStart.gif Program
None.gif    class Program
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IApplicationContext ctx 
= ContextRegistry.GetContext();
InBlock.gif
InBlock.gif            PersonDao dao 
= ctx.GetObject("personDao"as PersonDao;
InBlock.gif            dao.Get();
InBlock.gif
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

 

  输出效果如下:
2009-11-01.gif

 

 

 

 

 

 代码下载

 

 原文网址:http://www.cnblogs.com/GoodHelper/archive/2009/11/01/SpringNET_IoC1.html

转载于:https://www.cnblogs.com/lyh55/archive/2010/08/30/1816332.html

相关文章:

  • Forword(请求转发)与Redirect(重定向) 区别
  • [delphi]保证程序只运行一个实例
  • vue常用的指令
  • 商业计划书范本
  • 聊聊kafka0.8的topic的partition以及topicCountMap
  • FloodFill(漫水填充)算法
  • 升级BIOS解决DELL R730XD虚拟机死机问题
  • C# 委托,事件和Lambda表达式
  • ES6学习笔记四 default、rest、Multi-line Strings
  • SharePoint 2010 Excel Service 入门——在网页上显示Excel内容
  • New Concept English Two 33 94
  • 100%高度全屏自适应
  • Django REST框架--关系和超链接api
  • sql语句的字段转成Date
  • [转]java按指定编码写入和读取文件内容的类
  • Android优雅地处理按钮重复点击
  • JDK 6和JDK 7中的substring()方法
  • k8s 面向应用开发者的基础命令
  • mac修复ab及siege安装
  • npx命令介绍
  • PHP 7 修改了什么呢 -- 2
  • React+TypeScript入门
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • spring cloud gateway 源码解析(4)跨域问题处理
  • vue 个人积累(使用工具,组件)
  • 分布式熔断降级平台aegis
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 缓存与缓冲
  • 开发基于以太坊智能合约的DApp
  • 利用jquery编写加法运算验证码
  • 深入浏览器事件循环的本质
  • 手写双向链表LinkedList的几个常用功能
  • 双管齐下,VMware的容器新战略
  • 用Visual Studio开发以太坊智能合约
  • 怎么将电脑中的声音录制成WAV格式
  • 怎样选择前端框架
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • #{} 和 ${}区别
  • (1)Android开发优化---------UI优化
  • (超详细)语音信号处理之特征提取
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (亲测)设​置​m​y​e​c​l​i​p​s​e​打​开​默​认​工​作​空​间...
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (区间dp) (经典例题) 石子合并
  • (一)为什么要选择C++
  • (转)3D模板阴影原理
  • (转)Unity3DUnity3D在android下调试
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • (转)德国人的记事本
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .Net中的集合
  • @ResponseBody
  • [2023-年度总结]凡是过往,皆为序章