Ⅳ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 ...
随机推荐
- SharePoint 2010 获取列表中所有数据(包括文件夹内)的方法
转: http://blog.csdn.net/wangwenism/article/details/8751411 SharePoint的列表能存储文件以及文件夹,用户使用的时候,经常会建几个分类文 ...
- FZU 2105-Digits Count(线段树延时标记)
题意: 每次操作区间每个数进行一种(&或|.或^ )给定的一个数,到sum时统计给定区间的和. 分析: 这个题让我觉得我的思维很不活跃,对懒惰标记理解,还远远不够,通过这道题我对懒惰标记加深了 ...
- Java中线程顺序执行
现有线程threadone.threadtwo和threadthree,想要的运行顺序为threadone->threadtwo->threadthree,应该如何处理?这里需要用到一个简 ...
- 转:maven项目添加jar包.
很多新手都不知道如何在maven项目里添加jar包. 以前我还没接触maven的时候下载过一个demo,是maven项目. 我居然是照着他的pom.xml文件一个一个的写!!! 很多人认为理所当然的东 ...
- string 常用 方法
boost::array< char, 16 > header; string(header.begin(),header.end()) std::vector<uchar> ...
- Android UI -- 的基础知识。
在介绍基础知识之前先明确几个基本的概念 View 视图是所有可视组件的基类,所有的UI控件包括布局类都是从View派生出来的. ViewGroup ViewGroup是View的扩展,可以放置多个Vi ...
- NOIP2006 能量项链
洛谷1063 能量项链 本题地址:http://www.luogu.org/problem/show?pid=1063 题目描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N ...
- HW6.22
import java.util.Arrays; public class Solution { public static void main(String[] args) { int[][] ch ...
- phonegap Overview
PhoneGap 和 Cordova的关系阐述 是PhoneGap贡献给Apache后的开源项目,是从PhoneGap中抽出的核心代码,是驱动PhoneGap的核心引擎.你可以把他想象成类似于Webk ...
- Keil MDK 无法设置断点【worldsing】
要解决一个问题就怕不知道怎么搜索,或是别人没有遇到过: 同样碰到Keil MDK Debug调试无法设置断点问题,首先来问百度,GOOGLE,一下是我搜索到的结果: 1.keil 不能设置断点,每 ...