Five of people subscribed to newspapers at the newspaper. Once the newspaper had a new newspaper, they were sent to the five people. In this case, the newspaper is the event, the newspaper is the broadcaster, and five subscribers are the listeners. The broadcaster receives the event, passes the event to the listener, and the listener does something about the event. Such an example belongs to the Observer pattern.
- Observer Pattern : Class A is responsible for receiving new information, B, C, and d have always been concerned with a. Once a class has new information, it is sent to classes B, C, D, B, C, D to receive information to make different operations. This is the Observer pattern. Specific to the code, a variable message is set in a (this is information), and when the message is changed, the Performmessage (message) method of B, C, D is called (this sends the message to the B, C, D classes and lets the B, C, D classes begin processing. Here, the B, C, and D classes are listeners (the equivalent of a listener), and A is a publisher (equivalent to a broadcaster), and a message is an event.
- The Observer pattern for Java : Java.util.Observer belongs to the listener interface, and writing its own listener only needs to implement this interface. Java.util.Observable is the publisher, and writing your own publisher needs to extend this class.
- Event System : There are several elements:
- Event: Like a new newspaper, something happened, and this thing is an event.
- Event Source: The initiator of the thing.
- Event broadcaster: Notifies event listeners of events. (similar to a newspaper).
- Event Listener: Receive events and do some work on the events. (Subscribers have different responses after reading the newspaper separately).
- Event Listener registry: The framework's event listeners are stored in the registration table.
- event System role diagram : Event source generates events, passes events to event broadcasters, The event broadcaster then passes the event to the event listener in the event listener registry.
- How does spring use event broadcasters to publish events ?
- To publish an event, you must have several elements: an event class, a broadcaster, and a listener.
- Java.util.EventObject is the Java event class, and spring's applicationcontextevent extends the EventObject class. The inheritance relationship is:, where Applicationevent inherits the EventObject. We write our own Spring event class and can extend the applicationevent.
- Java.util.EventListener is the event listener class for Java, and Spring's Applicationlistener expands the EventListener interface. You can extend the Applicationlistener to write your own listener (configured in the configuration file after writing it).
- Spring's event broadcaster Inheritance relationship: The event listener Registry is provided by the event broadcaster. You can implement applicationeventmulticaster to write your own broadcaster, and if you do not have your own broadcaster, spring will use the default Simpleapplicationeventmulticaster broadcaster.
event class, Listener class, broadcast class all have, and then write an event source class (manufacturing event, must implement Applicationcontextaware interface, overwrite Setapplicationcontext method, To get the ApplicationContext instance. And then configure it in the configuration file, so that the entire event process works. Specific Operating Procedures:
- The event broadcaster is initialized when the spring container starts, and the event broadcaster provides the listener registry.
- Spring takes all the listeners from the configuration file and puts them in the listener registry.
- When the spring container starts, it runs the event source class, manufactures the event, and passes the event to the broadcaster, which then passes the event to the listener.
- an instance:
- Write event class:
public class Mailsendevent extends applicationcontextevent { private String to; public Mailsendevent ( ApplicationContext source, String to) { super (source); public String Getto () {return this .TO; }}
- To write the event listener class:
Public class Implements Applicationlistener<mailsendevent>{ publicvoid onapplicationevent ( Mailsendevent event) { = (mailsendevent) event; System.out.println ("Mailsendlistener: to" + mse.getto () + "send out a message");} }
- Write the event source class (the spring container starts and loads the class to drive the entire event delivery process) (in the following code, within Publishevent (MSE), spring delegates the Applicationeventmulticaster broadcast event to the listener):
Public classMailSenderImplementsApplicationcontextaware {PrivateApplicationContext CTX; Public voidSetapplicationcontext (ApplicationContext ctx)throwsbeansexception { This. CTX =CTX; } Public voidSendMail (String to) {System.out.println ("MailSender: Simulate sending mail ..."); Mailsendevent MSE=NewMailsendevent ( This. ctx,to); Ctx.publishevent (MSE); }}
- To configure the listener and event source classes in XML:
<class= "Com.baobaotao.event.MailSendListener"/> < id = "MailSender" class= "Com.baobaotao.event.MailSender" />
- The event source is called in the main function:
Public Static void Main (string[] args) { = "Com/baobaotao/event/beans.xml"; New Classpathxmlapplicationcontext (resourcefile); = Ctx.getbean (MailSender. Class); Mailsender.sendmail ("test mail.") ); System.out.println ("done." ); }
(spring-15th Back "IOC Basics") container events