如何调用其他对象的成员

  接上文例子:

  现在Person有一个属性 name  静态属性 eye 域 gender

  public class Person
{
public string name { get; set; }
public static object eye { get; set; }
public string gender;
public Pet pet { get; set; }public object seefrisbee(object color)
{
return string.Format("{0}看到{1}追着{2}颜色的飞盘", name,pet.name,color);
}
~Person()
{
Console.WriteLine(name + "离开");
}
}

对属性赋值:

  

 <object id="person" type="SpringDemo.Person,SpringDemo" singleton="true" >
<property name="pet" ref="dog" ></property>
<property name="name" value="Yahue"></property>
<property name="gender" value="男"></property>
<property name="eye" value="忧郁的眼神"></property>
<listener event="frisbeefly" method="seefrisbee">
<ref object="dog"/>
</listener>
</object>

name

  <object id="personname" type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">
<property name="TargetObject" ref="person"></property>
<property name="TargetProperty" value="name"></property>
</object>

eye

 <!--静态属性的调用-->
<object id="personeye" type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">
<property name="StaticProperty" value="SpringDemo.Person.eye"></property>
</object>

gender

<object id="persongender" type="Spring.Objects.Factory.Config.FieldRetrievingFactoryObject, Spring.Core">
<property name="TargetObject" ref="person"/>
<property name="TargetField" value="gender"/>
</object>

执行:

        Console.WriteLine(ctx.GetObject("personname"));
Console.WriteLine(ctx.GetObject("persongender"));
Console.WriteLine(ctx.GetObject("personeye"));
Console.ReadLine();

输出结果:

  

Yahue

拥有迷离而又忧郁的眼神

调用其他对象的方法:

Person有了静态say的方法:

  

    public class Person
{
public string name { get; set; }
public static object eye { get; set; }
public string gender;
public Pet pet { get; set; }
public static string say(string word1,string word2,string word3)
{
return string.Format("说:{0}-{1}-{2}", word1, word2, word3);
} public object seefrisbee(object color)
{
return string.Format("{0}看到{1}追着{2}颜色的飞盘", name,pet.name,color);
}
~Person()
{
Console.WriteLine(name + "离开");
}
}

xml

<object id="personsay" type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
<property name="TargetType" value="SpringDemo.Person,SpringDemo"></property>
<property name="TargetMethod" value="say"></property>
<property name="Arguments">
<list>
<value>啊</value>
<value>大海啊</value>
<value>全是水</value>
</list>
</property> </object>
Console.WriteLine(ctx.GetObject("personsay"));

输出:

说:啊-大海啊-全是水

现在有个非静态的say2方法:

 public class Person
{
public string name { get; set; }
public static object eye { get; set; }
public string gender;
public Pet pet { get; set; }
public static string say(string word1,string word2,string word3)
{
return string.Format("说:{0}-{1}-{2}", word1, word2, word3);
}
public string say2(string word1, string word2, string word3)
{
return string.Format("说:{0}-{1}-{2}", word1, word2, word3);
}
public object seefrisbee(object color)
{
return string.Format("{0}看到{1}追着{2}颜色的飞盘", name,pet.name,color);
}
~Person()
{
Console.WriteLine(name + "离开");
}
}
  <object id="personsay2" type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
<property name="TargetObject" ref="person"></property>
<property name="TargetMethod" value="say2"></property>
<property name="NamedArguments">
<dictionary>
<entry key="word1" value="啊!"></entry>
<entry key="word2" value="大海啊!"></entry>
<entry key="word3" value="全是水!"></entry>
</dictionary>
</property>
</object>

注意ref的值是待调用对象的实例

调用:

 Console.WriteLine(ctx.GetObject("personsay2"));

执行结果:

说:啊!-大海啊!-全是水!

注意:方法参数的传递有两种方式:NamedArguments和Arguments 前者方法名和值是个键值,后者是个注入的参数顺序和方法的参数顺序对应的集合。

spring.net (3)依赖注入基础2的更多相关文章

  1. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转

    Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...

  2. 谈谈自己了解的spring.NET的依赖注入

         spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...

  3. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  4. Spring学习(一)——Spring中的依赖注入简介【转】

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  5. spring六种种依赖注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  6. spring.NET的依赖注入

    谈谈自己了解的spring.NET的依赖注入   spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection), ...

  7. spring 四种依赖注入方式以及注解注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  8. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  9. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  10. Spring学习(一)——Spring中的依赖注入简介

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

随机推荐

  1. 我的iOS开发系列博文

    之前目录性的总结了发表过的关于OC方面的文章,今天在目录性的总结一下有关iOS开发的文章.走过路过不要错过哦,今天的博文也全都是干货.写技术博客与大家交流一下思想也是不错的. 下面是我的技术博客中有关 ...

  2. Percona博客学习总结

    1. Upgrading to MySQL 5.7, focusing on temporal types 在MySQL 5.6.4中,对TIME, TIMESTAMP and DATETIME三种时 ...

  3. MyCAT实现MySQL的读写分离

    在MySQL中间件出现之前,对于MySQL主从集群,如果要实现其读写分离,一般是在程序端实现,这样就带来一个问题,即数据库和程序的耦合度太高,如果我数据库的地址发生改变了,那么我程序端也要进行相应的修 ...

  4. 你所不知道的15个Axure使用技巧

    你有用原型开发工具吗?如果有,那你用的是Axure还是别的? 从以前就喜欢使用Axure,主要是觉得它能清楚的表达设计的思路,还有交互的真实再现,能让看的人一目了然,昨天看了这篇博文,便更加确定Axu ...

  5. php使用js对表格进行排序

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-type" content=&quo ...

  6. [ES] 安装

    1.ElasticSearch安装的准备工作 Linux:CentOS6.4 Elasticsearc:elasticsearch-2.2.0 JDK:jdk-7u79-linux-x64 IK:1. ...

  7. 实验记录:Oracle redo logfile的resize过程

    实验记录:Oracle redo logfile的resize过程. 实验环境:RHEL 6.4 + Oracle 11.2.0.3 单实例 文件系统 实验目的:本实验是修改redo logfile的 ...

  8. 数据库基础及T-SQL语句

    字符类型: int 整型float 小数double 小数varchar(20) 字符串bit 布尔型数据datetime 日期时间类型text 长文本 (以下两种不经常使用) money 存货币im ...

  9. 使用Apache Server 的ab进行web请求压力测试

    参考:http://www.cnblogs.com/spring3mvc/archive/2010/11/23/2414741.html 自己写代码经常是顺着逻辑写下去,写完后run一下,ok就玩完事 ...

  10. Django 1.10 中文文档------3.2.2 查询操作making queries

    3.2.2 查询操作 6.15章节包含所有模型相关的API解释. 后面的内容基于如下的一个博客应用模型: from django.db import models class Blog(models. ...