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 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...
随机推荐
- [bzoj3712][PA2014]Fiolki_倍增LCA
Fiolki bzoj-3712 PA-2014 题目大意:题目链接. 注释:略. 想法: 神题! 我们建树:对于一次倾倒操作,我们弄一个新的大瓶子作为两个合并瓶子的父亲节点,与两个瓶子相连. 对于一 ...
- 子元素设置margin-top,父元素也受影响
这个问题困惑了非常久.尽管没有大碍早就摸出来怎么搞定它.但始终不明确原因出在哪里,假设仅仅是IE有问题我也不会太在意.可问题是全部上等浏览器都表现如此,这样叫我怎能安心?今天总算下狠心查出来怎么回事, ...
- Linux学习系列之LNMP
LNMP介绍 LNMP是什么 LNMP(Linux-Nginx-MySQL-PHP)网站架构是目前国际流行的Web架构; 这四种软件组合,可以成为一个免费.高效.扩展性强的Web架构; LNMP原理图 ...
- react 项目实战(一)创建项目 及 服务端搭建
1.安装 React社区提供了众多的脚手架,这里我们使用官方推荐的create-react-app. //安装脚手架 npm install -g create-react-app //生成并运行项目 ...
- MYSQL入库常用PHP函数
addslashes addslashes() 函数在指定的预定义字符前添加反斜杠.这些字符是单引号(').双引号(").反斜线(\)与NUL(NULL字符). stripslash ...
- ubuntu下apache+mysql+php+mysql等之webserver搭建
相信非常多人跟我一样,想搭建一个自己的webserver.网上资料非常多.可是因为版本号的区别,总是存在依照一个教程来做无法全然实现的问题.近期我也折腾了好几天,google不能用,仅仅能百度,真想说 ...
- 大数据R语言简析
R语言是用于统计分析.画图的语言和操作环境.R是属于GNU系统的一个自由.免费.源码开放的软件.它是一个用于统计计算和统计制图的优秀工具. R是统计领域广泛使用的诞生于1980年左右的S语言的一个分支 ...
- JTabbedPane的LookAndFeel--TabbedPaneUI
在定制JTabbedPane的时候是需要使用到LookAndFeel的,而使用LookAndFeel定制的时候,其实主要是继承BasicTabbedPaneUI. to be continue...
- 利用游标返回结果集的的例子(Oracle 存储过程)JAVA调用方法和.NET调用方法
在sqlplus中建立如下的内容: 1.程序包 SQL> create or replace package types 2 as 3 type cursorType is re ...
- 【Poj 1330】Nearest Common Ancestors
http://poj.org/problem?id=1330 题目意思就是T组树求两点LCA. 这个可以离线DFS(Tarjan)-----具体参考 O(Tn) 0ms 还有其他在线O(Tnlogn) ...