spring.net事件的注入
1.c#代码 TestObject source = new TestObject(); TestEventHandler eventListener1 = new TestEventHandler(); TestEventHandler eventListener2 = new TestEventHandler(); source.Click += eventListener1.HandleEvent; // Adding the first event handler method to the event source.Click += eventListener2.HandleEvent; // Adding a second event handler method to the event source.OnClick(); // First eventListener1.HandleEvent is invoked, then eventListener2.HandleEvent 2.触发事件,激发委托 When OnClick() is invoked, the event is fired. public void OnClick() { if (Click != null) { Click(this, EventArgs.Empty); // Fire the event off to the registered handler methods } The IoC container Spring Framework (Version 1.3.2) 40 } 3.spring.net xml配置 <object id="eventListener1" type="SpringdotNETEventsExample.TestEventHandler, SpringdotNETEventsExample"> <!-- wired up to an event exposed on an instance --> <listener event="Click" method="HandleEvent"> <ref object="source"/> </listener> </object> <object id="eventListener2" type="SpringdotNETEventsExample.TestEventHandler, SpringdotNETEventsExample"> <!-- wired up to an event exposed on an instance --> <listener event="Click" method="HandleEvent"> <ref object="source"/> </listener> </object> 4. Regular expressions can be employed to wire up more than one handler method to an object that contains one or more events. <object id="eventListener" type="SpringdotNETEventsExample.TestEventHandler, SpringdotNETEventsExample"> <listener method="Handle.+"> <ref object="source"/> </listener> </object> Spring Framework (Version 1.3.2) 41 <object id="eventListener" type="SpringdotNETEventsExample.TestEventHandler, SpringdotNETEventsExample"> <!-- For the Click event, the HandleClick handler method will be invoked. --> <listener method="Handle${event}"> <ref object="source"/> </listener> </object> 5.3.3.4. Registering a handler method against an event name that contains a regular expression Finally, you can register an object's handler methods against a selection of events, filtering based on their name using a regular expression. <object id="eventListener" type="SpringdotNETEventsExample.TestEventHandler, SpringdotNETEventsExample"> <listener method="HandleEvent" event="Cl.+"> <ref object="source"/> </listener> </object> In this example the eventListener's HandleEvent handler method will be invoked for any event that begins with 'Cl'