码迷,mamicode.com
首页 > 移动开发 > 详细

applicationContext.publishEvent

时间:2016-07-30 18:23:37      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

一个事件:

package com;

import org.springframework.context.ApplicationEvent;

public class AddEvent extends ApplicationEvent {
    public AddEvent(Object source) {
        super(source);
    }
}

一个监听:

package com;

import org.springframework.context.ApplicationListener;

public interface AddListener extends ApplicationListener<AddEvent> {
}

 

订阅者1

package com;

import org.springframework.stereotype.Component;

@Component
public class AddWorker implements AddListener {
    @Override
    public void onApplicationEvent(AddEvent addEvent) {
        System.out.println(AddWorker.class.getSimpleName() + "收到消息:" + addEvent.toString());
    }
}

订阅2

package com;

import org.springframework.stereotype.Component;

@Component
public class AddWorker2 implements AddListener {
    @Override
    public void onApplicationEvent(AddEvent addEvent) {
        System.out.println(AddWorker2.class.getSimpleName() + "2收到消息:" + addEvent.toString());
    }
}

 

订阅3

package com;

import org.springframework.stereotype.Component;

@Component
public class AddWorker3 implements AddListener {
    @Override
    public void onApplicationEvent(AddEvent addEvent) {
        System.out.println(AddWorker3.class.getSimpleName() + "3收到消息:" + addEvent.toString());
    }
}

主题及事件发布者是ApplicatonContext:

applicationContext.publishEvent(new AddEvent(msg));

 

applicationContext.publishEvent

标签:

原文地址:http://www.cnblogs.com/softidea/p/5721347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!