The ApplicationContext event mechanism is an implementation of the observer design pattern, and through the Applicationevent class and the Applicationlistener interface, it is possible to implement ApplicationContext
Event handling. If there is a applicationlistener bean in the container, the Applicationlistener bean will automatically be applicationcontext whenever Applicationevent is released.
Trigger.
The Spring event framework has the following two important members:
- Applicationevent: Container event, must be released by ApplicationContext;
- Applicationlistener: Listener, which can be held by any listener bean in the container;
In fact, spring's event mechanisms are basically similar to all time mechanisms, and they all need to consist of event sources, events, and event listeners. Just the source of the event here is ApplicationContext,
And an event must be explicitly triggered by a Java program. The following program demonstrates the event mechanism of the spring container. The program first defines a Applicationevent class whose object is a spring container event.
The code is as follows:
public class EmailEvent extends applicationevent{private String address; private String text; Public EmailEvent (Object source, string address, string text) {super (source); this.address = address; This.text = text; } public EmailEvent (Object source) {super (source); }//......address and text setter, getter}
the EmailEvent class above inherits the Applicationevent class, except that it is an ordinary Java class.
The Listener class for the container event must implement the Applicationlistener interface, which must implement the following methods:
- onapplicationevent (applicationevent Event): This method will be triggered whenever any event occurs in the container;
The container Listener class code used in this example is as follows:
public class Emailnotifier implements applicationlistener{public void Onapplicationevent (Applicationevent event) { if (event instanceof emailevent) {emailevent emailevent = (emailevent) event; SYSTEM.OUT.PRINTLN ("Email address:" + emailevent.getaddress ()); System.our.println ("Mail content:" + emailevent.gettext ()); } else {System.our.println ("container itself event:" + event); } }}
Configure the listener in the spring container with the following code:
<bean class= "Com.abc.EmailNotifier"/>
Registering the listener for the spring container does not require code to be registered like AWT programming, just a simple configuration in the spring configuration file. When we are in
A bean,spring container that implements Applicationlistener in spring configures the bean as a listener for container events.
Container event listeners can monitor these events when the system creates a spring container, loads the spring container, and then automatically triggers the event. In addition, the program can also
Call ApplicationContext's Publishevent () method to actively trigger a container event, as an example:
public class Springtest {public static void main (String args[]) {ApplicationContext context = new Classpathxmlappli Cationcontext ("Bean.xml"); Create a Applicationevent object emailevent event = new EmailEvent ("Hello", "[email protected]", "This is a Test"); Actively triggering the event context.publishevent; }}
Note: If the Bean wants to publish the event, the bean must obtain a reference to its container. If you do not get a reference to a container directly in your program, you should let the bean implement
Applicationcontextaware or Beanfactoryaware interface, which allows you to obtain a reference to the container.
Spring provides several built-in events such as:
- post-processing bean is detected and activated, all singleton beans are pre-instantiated, the ApplicationContext container is ready for use
- Span style= "White-space:pre" >contextstartedevent: When using Configurableapplicationcontext ( The start () method of the ApplicationContext interface of the applicationcontext.
triggers the event when the container is started. The bean instance of the container management claims cycle will get a specified start signal, which is more common when restarting after a stop is often required
-
- contextstoppedevent: When using the Configurableapplicationcontext interface's Stop () method
Causes the event to be triggered when the ApplicationContext container is stopped. Stopping here means that the bean instance of the container management life cycle will get a specified stop signal, and the stopped
Spring container can call the start () method again to restart
- requesthandledevent:web related events, can only be applied to the Web using Dispatcherservlet should be
used. When you use spring as the front-end MVC controller, the event is automatically triggered when spring processes the user request at the end.
Original source: Chenssy
"Good text recommended" ApplicationContext event mechanism in spring