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>下面是事件的觸發 html
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訂閱事件
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>下面是事件的觸發 java
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的共同點 spring
- lookup-method爲實現方法,replaced-method爲替換方法,而且方法不能包含任何參數
- depends-on表示依賴那個對象用逗號分隔
- replaced-method的類繼承接口後的方法第一個參數爲要替換的對象,第二個爲替換的方法 第三個爲方法參數
- replaced-method裏面的arg-type是爲了匹配參數類型重載的時候, 當只有一個方法的時候能夠忽略