Ⅳ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 ...
随机推荐
- Asp.net MVC 4 异步方法
今天我们来看一下,同样功能在 Asp.net MVC 4 下的实现,基于.net framework 4.5 下的async支持,让我们的代码更加简单,看下面片断代码名叫Index的Action方法: ...
- Android Activity四种加载方式
Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...
- 430单片机之定时器A功能的大致介绍
总的来说,430单片机一共有三个定时器,定时器A,定时器B,还有就是看门狗定时器,这里我们主要是讨论430单片机的定时器A的功能,定时器A的功能是我目前见过最厉害的定时器,视频上说用好定时器A的话,对 ...
- 【UR #12】实验室外的攻防战(BIT)
[题目链接] http://uoj.ac/problem/180 [题意] 给定两个1..n的排列AB,只有当ai<ai+1才能交换ai和ai+1,问是否能够将A转换为B. [思路] 令a[i] ...
- 【暑假】[深入动态规划]UVa 1627 Team them up!
UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS Memory Limit: Unknown 64bit IO Forma ...
- HDU 3488--Tour(KM or 费用流)
因为每个点只能经过一次 所以考虑拆点 这题有坑,有重边.. KM算法 把一个点拆成入点和出点 入点在X部,出点在Y步. 如果u,v之间有路径,就在X部的u点连接Y部的v点 求完美匹配. 当完美匹配的时 ...
- A Tour of Go Switch
You probably knew what switch was going to look like. A case body breaks automatically, unless it en ...
- 多个分布式系统如何共享使用一个固定公网IP
传统的做法,一个分布式业务系统就有一个中间件,一个中间件需要使用至少一个固定公网IP,这样的话,多个业务系统就需要使用多个固定公网IP. 大家知道,固定公网IP价格可是不菲的.能不能让多个分布式业务系 ...
- [C语言 - 12] Union联合
union Student { int age; char *name; } stu; union只按照最长的数据成员分配控件,适用于有N个数据不会同时出现的情况,用以压缩空间.
- C语言/C++中怎样产生随机数
C语言/C++怎样产生随机数:这里要用到的是rand()函数, srand()函数,和time()函数. 需要说明的是,iostream头文件中就有srand函数的定义,不需要再额外引入stdlib. ...