Spring.Net學習筆記(6)


一、開發環境

系統:win10

編譯器:VS2013

二、涉及程序集

Spring.Core.dll 1.3.1

Common.Logging.dll

三、開發過程

1.項目結構

image

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();
}
}
}

四、備注

今天又掉進坑了,具體什么坑看問題匯總


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com