一、开发环境

系统:win10

编译器:VS2013

二、涉及程序集

Spring.Core.dll 1.3.1

Common.Logging.dll

三、开发过程

1.项目结构

2.编写Mobile.cs

namespace SpringNetMethodDi
{
public abstract class Mobile
{
//可以是virtual
public abstract SendTool GetSendTool();
}
}

3.编写SendTool.cs

namespace SpringNetMethodDi
{
public class SendTool
{
public void WeChat()
{
Console.WriteLine("我是Kimisme,正在用微信发信息");
}
}
}

4.编写RealOp.cs

namespace SpringNetMethodDi
{
public class RealOp
{
public virtual string Buy(string goods)
{
return goods + "是昨天的剩下的";
}
}
}

5.编写SuperMarket.cs

namespace SpringNetMethodDi
{
public class SuperMarket : IMethodReplacer
{
public object Implement(object target, System.Reflection.MethodInfo method, object[] arguments)
{
string value = arguments[0].ToString();
return value + "是今天的面包";
}
}
}

6.编写Door.cs

namespace SpringNetMethodDi
{
public delegate string OpenHandler(string arg); public class Door
{
public event OpenHandler OpenTheDoor; public void OnOpen(string arg)
{ if (OpenTheDoor != null)
{
Console.WriteLine(OpenTheDoor(arg));
}
}
}
}

7.编写Men.cs

namespace SpringNetMethodDi
{
public class Men
{
public string OpenThisDoor(string arg)
{
return "参数是:" + arg;
}
}
}

8.编写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"></resource>
</context>
<objects xmlns="http://www.springframework.net">
<!--01抽象方法注入-->
<object name="sendTool" type="SpringNetMethodDi.SendTool,SpringNetMethodDi"></object>
<object name="mobile" type="SpringNetMethodDi.Mobile,SpringNetMethodDi">
<lookup-method name="GetSendTool" object="sendTool"/>
</object>
<!--02替换方法-->
<object name="replaceValue" type="SpringNetMethodDi.SuperMarket,SpringNetMethodDi"></object>
<object name="realOp" type="SpringNetMethodDi.RealOp,SpringNetMethodDi">
<replaced-method name="Buy" replacer="replaceValue">
<arg-type match="String"/>
</replaced-method>
</object>
<!--03事件注入-->
<object id="door" type="SpringNetMethodDi.Door,SpringNetMethodDi"></object>
<object id="men" type="SpringNetMethodDi.Men,SpringNetMethodDi">
<listener event="OpenTheDoor" method="OpenThisDoor">
<ref object="door"/>
</listener>
</object> </objects>
</spring>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

9.控制台代码

namespace SpringNetMethodDi
{
class Program
{
static void Main(string[] args)
{
IApplicationContext context = ContextRegistry.GetContext();
Mobile mobile = context.GetObject("mobile") as Mobile;
mobile.GetSendTool().WeChat(); RealOp op = context.GetObject("realOp") as RealOp;
Console.WriteLine(op.Buy("面包")); Door door = context.GetObject("door") as Door;
door.OnOpen("Opening");
Console.ReadKey();
}
}
}

四、备注

今天又掉进坑了,具体什么坑看问题汇总

Spring.Net学习笔记(6)-方法注入的更多相关文章

  1. Spring揭秘 读书笔记 四----方法注入

    我们知道,拥有prototype类型scope的bean,在请求方每次向容器请求该类型对象的时候,容器都会返回一个全新的该对象实例. 我们看下面的例子: public class MockNewsPe ...

  2. Spring学习笔记之方法注入

    public  abstract class ReplacedBean {protected static final Log log = LogFactory.getLog(ReplacedBean ...

  3. Spring.NET学习笔记6——依赖注入(应用篇)

    1. 谈到高级语言编程,我们就会联想到设计模式:谈到设计模式,我们就会说道怎么样解耦合.而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny I ...

  4. Spring.Net学习笔记(5)-集合注入

    一.开发环境 系统:Win10 编译器:VS2013 .net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Loggin.dll ...

  5. Spring.Net学习笔记(2)-依赖注入

    一.开发环境 操作系统:Win10 编译器:VS2013 framework版本:.net 4.5 Spring版本:1.3.1 二.涉及程序集 Spring.Core.dll Common.Logg ...

  6. 【转】Spring.NET学习笔记——目录

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  7. Spring.NET学习笔记——目录(原)

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  8. Spring源码学习笔记9——构造器注入及其循环依赖

    Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...

  9. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

随机推荐

  1. 权限框架之Shiro详解(非原创)

    文章大纲 一.权限框架介绍二.Shiro基础介绍三.Spring Boot整合Shiro代码实战四.项目源码与资料下载五.参考文章   一.权限框架介绍 1. 什么是权限管理   权限管理属于系统安全 ...

  2. DTRACE简介(1)

    https://blogs.oracle.com/swan/entry/dtrace%E7%AE%80%E4%BB%8B By samwan on 三月 20, 2007 记得几年前看过一部美国大片叫 ...

  3. 模拟用户点击弹出新页面不会被浏览器拦截_javascript技巧

    原文:http://www.html5cn.com.cn/article/zxzx/3195.html 相信用过window.open的小伙伴们都遇到过被浏览器拦截导致页面无法弹出的情况:我们换下思路 ...

  4. 关于Windows 8使用WMP播放音乐时WUDFHost跑CPU和硬盘的问题解决

    Windows 8使用Windows Media Player播放音乐的时候.事实上有一个这种情况,WMP和某个什么名字看起来非常屌的进程跑CPU非常高,这个跑非常高视你插入的SD卡内的文件数或者移动 ...

  5. MariaDB ----增删改

    1> 查看表的(创建)状态: show careate table students1; 注:       自增, (保护机制) 好马不吃回头草 2>查看 字段:( 查看表的结构) des ...

  6. MySQL 中间层 Atlas MySQL

    Atlas MySQL 详细介绍 Atlas是由 Qihoo 360,  Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0. ...

  7. __init__、__str__、__del__方法

    1.def__init__()方法: class Cat: """定义了一个Cat类""" #初始化对象 def __init__(self ...

  8. 图像处理之基础---彩色转灰度算法优化rgb to yuv

    File:      StudyRGB2Gray.txtName:      彩色转灰度算法彻底学习Author:    zyl910Version:   V1.0Updata:    2006-5- ...

  9. ubutu14.04无法使用sudo,也无法切换到root用户去解决问题怎么办?

    一不小心,修改了/etc/sudoers文件. 惨了. 无法使用sudo了,啥都干不成了. 最最关键的是,也无法用root登录. 本想着要重装系统了. 后来发现了神奇的ubuntu安全模式. 1.重启 ...

  10. html使用css让文字超出部分用省略号三个点显示的方法案例

    html使用css让文字超出部分用省略号三个点显示的方法: 我正确使用的就是下面的代码,li里面是a标记.在IE和google中使用是正常的,火狐不知道,我也不在意,等你来测 li{ display: ...