Ⅳspring的点点滴滴--方法和事件
承接上文
方法和事件
.net篇(环境为vs2012+Spring.Core.dll v1.31)
public abstract class MethodDemo
{
protected abstract SingleMethod CreateMethodByAbstract();
public virtual SingleMethod CreateMethodByVireual() { return null; }
public virtual String add(int x,int y){return x+y+"";}
public virtual int add(int x, int y,int z) { return 0; }
public SingleMethod Process1()
{
return this.CreateMethodByAbstract();
}
}
public class MethodReplace :
Spring.Objects.Factory.Support.IMethodReplacer
{
public object Implement(object target,
System.Reflection.MethodInfo method, object[] arguments)
{
return "2";
}
}
public delegate string Handler(String arg);
public class HandlerDemo
{
public event Handler eventHandler;
public void fire()
{
//调用事件
if (eventHandler != null)
{
Console.WriteLine(eventHandler(null));
}
}
}
public class SingleMethod
{
public String Demo { get; set; }
public string HandlerTigger(String arg)
{
return arg+"触发";
}
}<object id="single" type="SpringBase.SingleMethod, SpringBase" singleton="false">
<property name="Demo" value="1"></property>
</object>
<object id="myObject" type="SpringBase.MethodDemo, SpringBase"
depends-on="methReplace,hadlerDemo" >
<lookup-method name="CreateMethodByAbstract" object="single"/>
<lookup-method name="CreateMethodByVireual" object="single"/>
<replaced-method name="add" replacer="methReplace">
<arg-type match="Int"/>
<arg-type match="Int"/>
</replaced-method>
</object>
<object id="methReplace" type="SpringBase.MethodReplace, SpringBase"></object>
<object id="hadlerDemo" type="SpringBase.HandlerDemo, SpringBase"></object>下面是事件的触发
IApplicationContext ctx = ContextRegistry.GetContext("test");
HandlerDemo dao = (HandlerDemo)ctx.GetObject("hadlerDemo");
ctx.PublishEvents(dao);
SingleMethod single = (SingleMethod)ctx.GetObject("single");
ctx.Subscribe(single);
dao.fire();
- lookup-method和replaced-method都必须是虚方法或者抽象方法
- replaced-method的类必须继承Spring.Objects.Factory.Support.IMethodReplacer,
实现Implement方法,替换方法返回int的时候会有问题- PublishEvents发布事件,Subscribe订阅事件
java篇(环境为Maven+Jdk1.7+IntelliJ IDEA 12.1.4)
package springdemo;
import org.springframework.beans.factory.support.MethodReplacer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import java.lang.reflect.Method;
public abstract class MethodDemo {
protected abstract SingleMethod CreateMethodByAbstract();
public SingleMethod CreateMethodByVireual() {
return null;
}
public Integer add(Integer x, Integer y) {
return x + y;
}
}
class SingleMethod {
private String demo;
public SingleMethod(String demo) {
this.demo = demo;
}
public String getDemo() {
return demo;
}
public void setDemo(String demo) {
this.demo = demo;
}
}
class MethodReplace implements MethodReplacer {
@Override
public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
System.out.println("MethodReplace: true");
return args.length;
}
}
class EventHandler extends ApplicationEvent {
public EventHandler(Object source) {
super(source);
}
}
class EventListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof EventHandler) {
EventHandler singleMethod = (EventHandler) event;
System.out.println("DemoEvent: " + singleMethod.toString());
}
}
}<bean class="springdemo.EventListener"/>
<bean id="singleMethod" class="springdemo.SingleMethod">
<constructor-arg index="0" value="test"/>
</bean>
<bean id="methodReplace" class="springdemo.MethodReplace"/>
<bean id="methodDemo" class="springdemo.MethodDemo">
<lookup-method name="CreateMethodByAbstract" bean="singleMethod" />
<lookup-method name="CreateMethodByVireual" bean="singleMethod" />
<replaced-method name="add" replacer="methodReplace" />
</bean>下面是事件的触发
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:bean.xml");
ctx.publishEvent(new EventHandler(ctx));
- replaced-method的类必须继承MethodReplacer,实现reimplement方法
- 事件监听必须继承ApplicationListener并且要在bean.xml文件里面配置,
否则无效,不过id可以忽略,因为是所有的事件都会进所以
最好instanceof判断下是不是自己要的事件,否则会报错,- 事件必须继承ApplicationEvent,构造函数的参数为发起者
- 通过ctx.publishEvent(new EventHandler(ctx))来触发事件
java和Csharp的共同点
- lookup-method为实现方法,replaced-method为替换方法,并且方法不能包含任何参数
- depends-on表示依赖那个对象用逗号分隔
- replaced-method的类继承接口后的方法第一个参数为要替换的对象,第二个为替换的方法
第三个为方法参数- replaced-method里面的arg-type是为了匹配参数类型重载的时候,
当只有一个方法的时候可以忽略
- 下一篇:Ⅴ.spring的点点滴滴--引用其他对象或类型的成员
- 上一篇:Ⅲ.spring的点点滴滴--赋值
- 本文链接地址:Ⅳspring的点点滴滴--方法和事件
Ⅳspring的点点滴滴--方法和事件的更多相关文章
- Ⅲ.spring的点点滴滴--赋值
承接上文 对象的赋值(调用方式都一样不再阐述) .net篇(环境为vs2012+Spring.Core.dll v1.31) public class PropertyDemo{ public Sys ...
- Ⅴ.spring的点点滴滴--引用其他对象或类型的成员
承接上文 引用其他对象或类型的成员 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class Person { public string Name { ...
- Ⅱ.spring的点点滴滴--对象
承接上文 对象的各种实例化 .net篇(环境为vs2012+Spring.Core.dll) 修改原来的PersonDao对象为 public class PersonDao : IPersonDao ...
- XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)
承接上文 IObjectFactoryPostProcessor(工厂后处理器)) 前提是实现接口的对象注册给当前容器 直接继承的对象调用这个postProcessBeanFactory方法,参数为工 ...
- Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)
承接上文 IObjectPostProcessor(对象后处理器) 前提是实现接口的对象注册给当前容器 C#: 通过继承AbstractObjectFactory对象的AddObjectPostPro ...
- Ⅷ.spring的点点滴滴--抽象对象和子对象
承接上文 抽象对象和子对象 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { public string Name { get; ...
- Ⅵ.spring的点点滴滴--自定义类型转换器
承接上文 自定义类型转换器 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class CustomeConverter : TypeConverter{ ...
- Ⅶ.spring的点点滴滴--自定义对象行为
承接上文 自定义对象行为 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class lifeCycle : Spring.Objects.Factory. ...
- Spring ApplicationContext(八)事件监听机制
Spring ApplicationContext(八)事件监听机制 本节则重点关注的是 Spring 的事件监听机制,主要是第 8 步:多播器注册:第 10 步:事件注册. public void ...
随机推荐
- NopCommerce架构分析之八------多语言
系统支持的语言是有类:Language表示: 多语言资源对应的类为:LocalizedProperty: 当先选择某种语言存储在类中:GenericAttribute: 多语言可以导出为XML文件,当 ...
- 教你用Java安全有效的实现两星期内自动登陆功能-Session
现在很多网站都有为用户保存登陆信息(即保存Cookie)的功能,当用户下一次进入网站时,可以帮助用户自动登陆,使网站显得更加友好.笔者通过研究ACEGI项目的自动登陆源码,编写了一个安全有效的实现两星 ...
- 【HTML】Advanced7:Embedded Content: Video, Audio, and Canvas
1.video <video src="kitties.mp4" poster="fluffy.jpg"(display before video is ...
- 浅谈Javascript中默认参数值的设置
第一种: 1: function test(a,b){ 2: var a = arguments[0] ? arguments[0] : 1;//设置参数a的默认值为1 3: var b = argu ...
- wuzhicms短信API 实例调用
1.接口调用 $sendsms = load_class('sms','sms'); echo $sendsms->send_sms('18911549611', '888888', 1); / ...
- 自己常用的js方法
$(function(){ tabview("#zcfw_list1"); tabview("#zcfw_list2"); tabview("#zcf ...
- Android实例-操作摄像头(XE8+小米2)
结果: 1.同样是照相,自己的程序设置为高质量时刷新慢,而小米手机的相机那真心反映快呀. 2.就算我设置为最高质量,可相片也没有小米手机的相片大.我最大是2000*1000,而小米可以做到3000*2 ...
- ASP.NET基础系列
一.HttpContext概述 1).如何获取对象: 在WebForm或类库(包括MVC)项目中,通过Current静态属性,就能够获得HttpContext的对象: HttpContext cont ...
- .NET常用工具类集锦
不错的地址: http://www.cnblogs.com/flashbar/archive/2013/01/23/helper.html https://github.com/chrisyanghu ...
- android获取mac地址方法
http://www.cnblogs.com/xioapingguo/p/4037513.html 网上找的,记录一下 public static String getMacAdress(){ Wif ...