Ⅳ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();
  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是为了匹配参数类型重载的时候, 当只有一个方法的时候可以忽略

原文地址:https://www.cnblogs.com/cnlj/p/3461226.html