承接上文

方法和事件


.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();
  1. lookup-methodreplaced-method都必须是虚方法或者抽象方法
  2. replaced-method的类必须继承Spring.Objects.Factory.Support.IMethodReplacer,
    实现Implement方法,替换方法返回int的时候会有问题
  3. 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));
  1. replaced-method的类必须继承MethodReplacer,实现reimplement方法
  2. 事件监听必须继承ApplicationListener并且要在bean.xml文件里面配置,
    否则无效,不过id可以忽略,因为是所有的事件都会进所以
    最好instanceof判断下是不是自己要的事件,否则会报错,
  3. 事件必须继承ApplicationEvent,构造函数的参数为发起者
  4. 通过ctx.publishEvent(new EventHandler(ctx))来触发事件

javaCsharp的共同点

  1. lookup-method为实现方法,replaced-method为替换方法,并且方法不能包含任何参数
  2. depends-on表示依赖那个对象用逗号分隔
  3. replaced-method的类继承接口后的方法第一个参数为要替换的对象,第二个为替换的方法
    第三个为方法参数
  4. replaced-method里面的arg-type是为了匹配参数类型重载的时候,
    当只有一个方法的时候可以忽略

Ⅳspring的点点滴滴--方法和事件的更多相关文章

  1. Ⅲ.spring的点点滴滴--赋值

    承接上文 对象的赋值(调用方式都一样不再阐述) .net篇(环境为vs2012+Spring.Core.dll v1.31) public class PropertyDemo{ public Sys ...

  2. Ⅴ.spring的点点滴滴--引用其他对象或类型的成员

    承接上文 引用其他对象或类型的成员 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class Person { public string Name { ...

  3. Ⅱ.spring的点点滴滴--对象

    承接上文 对象的各种实例化 .net篇(环境为vs2012+Spring.Core.dll) 修改原来的PersonDao对象为 public class PersonDao : IPersonDao ...

  4. XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)

    承接上文 IObjectFactoryPostProcessor(工厂后处理器)) 前提是实现接口的对象注册给当前容器 直接继承的对象调用这个postProcessBeanFactory方法,参数为工 ...

  5. Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)

    承接上文 IObjectPostProcessor(对象后处理器) 前提是实现接口的对象注册给当前容器 C#: 通过继承AbstractObjectFactory对象的AddObjectPostPro ...

  6. Ⅷ.spring的点点滴滴--抽象对象和子对象

    承接上文 抽象对象和子对象 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { public string Name { get; ...

  7. Ⅵ.spring的点点滴滴--自定义类型转换器

    承接上文 自定义类型转换器 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class CustomeConverter : TypeConverter{ ...

  8. Ⅶ.spring的点点滴滴--自定义对象行为

    承接上文 自定义对象行为 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class lifeCycle : Spring.Objects.Factory. ...

  9. Spring ApplicationContext(八)事件监听机制

    Spring ApplicationContext(八)事件监听机制 本节则重点关注的是 Spring 的事件监听机制,主要是第 8 步:多播器注册:第 10 步:事件注册. public void ...

随机推荐

  1. vtiger 支持 物业收费功能 微信收费

    谁要?需要什么功能? 直接在下面留言,博主会整理大家的需求,形成产品,发出来.

  2. 我的WCF之旅(3):在WCF中实现双工通信

    双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...

  3. 【转】经典SQL语句大全

    原博客地址:http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html (超级纳闷为啥没有转载的功能) 一.基础 1.说明:创建数 ...

  4. The type or namespace name '****' could not be found (are you missing a using directive or an assembly reference

    错误的提升内容:

  5. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]PrI.6.1

    Given a basis $U=(u_1,\cdots,u_n)$ not necessarily orthonormal, in $\scrH$, how would you compute th ...

  6. 《深入Java虚拟机学习笔记》- 第8章 连接模型

    Java虚拟机学习笔记(八)连接模型

  7. C++ static_cast dynamic_cast reinterpret_cast const_cast转换

    static_cast <type-id> ( expression ) 和C风格的类型转换相似,可以转换一个指针到基类,或者派生类.不做Run-time类型检查,这样转换并不总是安全的. ...

  8. CentOS 7.0 安装 python3.X 脚本

    #!/bin/sh #第一个Linux下的脚本,太多不明白的地方,只是依着网上的例子照葫芦画瓢,能正常运行即可 #运行环境 CentOS 7.0 版本 #首行指定程序的路径,以#号开头的行是注释行 # ...

  9. SSH权威指南(转载)

    本书是一本介绍通信安全的书籍,如果你想保障你的通信安全,本书能给你一个很好的解决方案.本书从ssh协议介绍起,到具体的开源实现和商业实现.但本书同时介绍开源实现和商业实现,给人感觉比较乱.注意:由于o ...

  10. EXCEL 建立工作薄与工作表

    //1.引用单元 uses ComObj; //2.建立工作薄与工作表 procedure TForm1.Button1Click(Sender: TObject); Var ExcelApp,She ...