Spring.Net学习笔记(6)-方法注入
一、开发环境
系统: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)-方法注入的更多相关文章
- Spring揭秘 读书笔记 四----方法注入
我们知道,拥有prototype类型scope的bean,在请求方每次向容器请求该类型对象的时候,容器都会返回一个全新的该对象实例. 我们看下面的例子: public class MockNewsPe ...
- Spring学习笔记之方法注入
public abstract class ReplacedBean {protected static final Log log = LogFactory.getLog(ReplacedBean ...
- Spring.NET学习笔记6——依赖注入(应用篇)
1. 谈到高级语言编程,我们就会联想到设计模式:谈到设计模式,我们就会说道怎么样解耦合.而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny I ...
- Spring.Net学习笔记(5)-集合注入
一.开发环境 系统:Win10 编译器:VS2013 .net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Loggin.dll ...
- Spring.Net学习笔记(2)-依赖注入
一.开发环境 操作系统:Win10 编译器:VS2013 framework版本:.net 4.5 Spring版本:1.3.1 二.涉及程序集 Spring.Core.dll Common.Logg ...
- 【转】Spring.NET学习笔记——目录
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring.NET学习笔记——目录(原)
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring源码学习笔记9——构造器注入及其循环依赖
Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...
- SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...
随机推荐
- Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)
E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Bootstrap官网文档查询
Ctrl+F 在出现的小搜索框里面输入要查找的东西.回车即可!
- Google Protocol Buffer 的使用(二)
一.protobuf应用场景 protobuf 在Java中的应用场景可以是序列化和反序列化,流可以通过文件或者通过网络TCP/UDP等方式传输.新建一个.proto文件 syntax = " ...
- mysql建表语句key的含义
CREATE TABLE `admin_role` ( `adminSet_id` varchar(32) NOT NULL, `roleSet_id` varchar(32) NOT NULL, ...
- Java 代理模式和装饰者模式的区别
装饰模式:以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案:代理模式:给一个对象提供一个代理对象,并有代理对象来控制对原有对象的引用: 装饰模式应该为所装饰的对象增强功能:代理模式对代理的 ...
- 在ASP.NET Core 中使用Cookie中间件 (.net core 1.x适用)
在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...
- redhat and centos 系统经常使用命令
查看cpu信息: more /proc/cpuinfo |grep "model name" 通常是至强系统 查看内存信息: grep MemTotal / ...
- java STW stop the world 哈哈就是卡住了
java STW stop the world 哈哈就是卡住了 学习了:http://www.jb51.net/article/125400.htm
- 011 router backup
Router>en Router#config t Enter configuration commands, one per line. End with CNTL/Z. Router(co ...
- iOS开发项目实战——Swift实现图片轮播与浏览
近期開始开发一个新的iOS应用,自己决定使用Swift.进行了几天之后,发现了一个非常严峻的问题.那就是无论是书籍,还是网络资源,关于Swift的实在是太少了,随便一搜全都是OC实现某某某功能.就算是 ...