Spring文档苦读【ApplicationEvent】

简介

  首先,要说一下ApplicationEvent是干什么用的!

  个人理解,就是Spring管理的发布/订阅器,主要用来触发一系列Spring 内部事件。

  内部事件在Spring文档苦读【1】有介绍。

如何自己定义发布/订阅器

  下面我就写一个小例子,主要是用来发送邮件的。(该例子取自Spring官方文档)

  首先,要创建一个事件<ApplicationEvent>,交有Spring管理

 1 public class BlackListEvent extends ApplicationEvent {
 2     private final String address;
 3     private final String test;
 4     public BlackListEvent(Object source, String test, String address) {
 5         super(source);
 6         this.address = address;
 7         this.test = test;
 8     }
 9     public String getAddress() {
10         return address;
11     }
12 
13 
14     public String getTest() {
15         return test;
16     }
17 
18 
19     private static final long serialVersionUID = 1L;
20 
21 }
BlackListEvent

  其次创建一个通知监听器<ApplicationListener>.

public class BlackListNotifier implements ApplicationListener<BlackListEvent> {

    private String notificationAddress;

    public void setNotificationAddress(String notificationAddress) {
        this.notificationAddress = notificationAddress;
    }

    public void onApplicationEvent(BlackListEvent event) {
        System.out.println("onApplicationEvent +"+event.getAddress());
        System.out.println("to send email");
    }

}
BlackListNotifier

  最后,创建Service,要继承<ApplicationEventPublisherAware>,以便获取ApplicationEventPublisher

public class EmailService implements ApplicationEventPublisherAware {

    private List<String> blackList;
    private ApplicationEventPublisher publisher;

    public void setBlackList(List<String> blackList) {
        this.blackList = blackList;
    }

    public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    public void sendEmail(String address, String text) {
        if (blackList.contains(address)) {
            BlackListEvent event = new BlackListEvent(this, address, text);

            System.out.println("发布事件begin");
            publisher.publishEvent(event);
            System.out.println("发布事件end");
            return;
        }
    }
}
EmailService

  编写xml

<bean id="emailService" class="com.may.web.event.EmailService">
        <property name="blackList">
            <list>
                <value>known.spammer@example.org</value>
                <value>known.hacker@example.org</value>
                <value>john.doe@example.org</value>
            </list>
        </property>
    </bean>
    
    <bean id="blackListNotifier" class="com.may.web.event.BlackListNotifier">
        <property name="notificationAddress" value="blacklist@example.org"/>
    </bean>
beans.xml

  最后测试

@RunWith(SpringRunner.class)
@ContextConfiguration("/beans.xml")
public class XmlApplicationContextTests {
    @Autowired
    private ApplicationContext applicationContext;
    @Test
    public void testEvent() {
        EmailService service = applicationContext.getBean("emailService", EmailService.class);
        service.sendEmail("known.spammer@example.org", "111111");
    }
}
XmlApplicationContextTests

  输出

发布事件begin
onApplicationEvent +111111
to send email
发布事件end

 

  

posted on 2016-11-08 18:38  源码解析  阅读(349)  评论(0编辑  收藏  举报

导航