Spring's event publishing mechanism

Source: Internet
Author: User

One: Spring's event release

ApplicationContext provides event propagation capabilities for beans, the protagonist of which is the Publishevent () method, which enables you to notify the listener within the system (to implement the Applicationlistener interface).

ApplicationContext this interface, is the spring context, usually get the bean need this interface, this interface is not directly inherit from Beanfactory, the most famous is directly inherit the Applicationpublisher interface, This interface to see the source code can be found: there is only one way, that is the protagonist void Publishevent (Applicationevent event);

Spring provides a aware-based interface with Applicationcontextaware,resourceloaderaware,servletcontextaware (note: STRUTS2 also has this interface, note the distinction), The most common are these three, and spring's event publishing mechanism needs to use the Applicationcontextaware interface.

Implements the Applicationcontextaware bean, which will be injected into the ApplicationContext instance when the bean is initialized (because there is a set (Applictationcontext ctx) method in this interface)

Second: With the above basics, look at the sample code:

1. First create the event class Tradeevent

Package Net.wang.test;import org.springframework.context.applicationevent;/** * Event * @author Liuruowang */public Class Tradeevent extends Applicationevent{public tradeevent (Object source) {super (source); System.out.println ("Event: Tradeevent event!!");}} Event must inherit applicationevent abstract class provided by spring
2. Then write the publisher of the event HelloWorld:
package Net.wang.test;import Org.springframework.context.applicationeventpublisher;import org.springframework.context.applicationeventpublisheraware;/** * Event Publisher * @author Liuruowang */public class HelloWorld Implements Applicationeventpublisheraware{private string Word;public void Setword (string word) {This.word = Word;} Private Applicationeventpublisher tradeeventpublisher;public void Setapplicationeventpublisher ( Applicationeventpublisher applicationeventpublisher) {this.tradeeventpublisher=applicationeventpublisher;} public void Say () {System.out.println ("say:" +this.word); Tradeevent tradeevent = new Tradeevent (New String ("helloworld!")); This.tradeEventPublisher.publishEvent (tradeevent);}} 
Where the event was published in the Say () method
3. The recipient of the last writing event Eventreceiver:
Package Net.wang.test;import org.springframework.context.applicationlistener;/** * Event Recipient * @author Liuruowang */ public class Eventreceiver implements Applicationlistener<tradeevent>{public void Onapplicationevent ( Tradeevent event) {System.out.println ("Listen to Events:" +event.getsource ());}}
The receiver of the event is actually a listener that must implement the Applicationlistener and note that the event tradeevent is written directly into the generic
4.applicationcontext.xml: 
<?xml version= "1.0" encoding= "GBK"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= "http// Www.springframework.org/schema/aop "xmlns:tx=" http://www.springframework.org/schema/tx "xsi:schemalocation=" http ://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp:// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp:// Www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "><bean name=" Hellowrold "class=" Net.wang.test.HelloWorld "><property name=" word "value=" word! " /></bean><bean name= "Eventreceiver" class= "Net.wang.test.EventReceiver"/></beans>

Note that the recipient of the event is written to the configuration file

5. Testing Test:

Package Net.wang.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test {public static void main (String [] args) {ApplicationContext ctx=new classpathxmlapplicationcontext ("Applicationcontext.xml"); HelloWorld h = (HelloWorld) ctx.getbean ("Hellowrold"); H.say ();}}

6. The results show:

The results have shown that the heard events indicate success.

Spring's event publishing mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.