Spring 事件通知

Spring事件通知

1.编写自定义的事件载体对象

  1. /**
  2. * @Author:qmfang
  3. * @Description: 继承自事件对象,表明将其作为一个事件通知对象
  4. * @Date:Created in 10:35 2018/4/8
  5. * @Modified By:
  6. */
  7. @Data
  8. public class BlackListEvent extends ApplicationEvent {
  9. private final String address;
  10. private final String test;
  11. public BlackListEvent(Object source, String address, String test) {
  12. super(source);
  13. this.address = address;
  14. this.test = test;
  15. }
  16. }

2.编写事件触发对象

  1. /**
  2. * @Author:qmfang
  3. * @Description: 实现以aware结尾的接口之后在bean被实例化之后会Spring会注入相应的资源
  4. * @Date:Created in 10:37 2018/4/8
  5. * @Modified By:
  6. */
  7. public class EmailService implements ApplicationEventPublisherAware {
  8. private List<String> blackList;
  9. private ApplicationEventPublisher publisher;
  10. public void setBlackList(List<String> blackList) {
  11. this.blackList = blackList;
  12. }
  13. @Override
  14. public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
  15. /**
  16. * 此处也就是注册进入应用事件发布对象
  17. */
  18. this.publisher = applicationEventPublisher;
  19. }
  20. public void sendEmail(String address, String test) {
  21. if (blackList.contains(address)) {
  22. /**
  23. * 如果是包含的地址将发布该消息
  24. */
  25. BlackListEvent event = new BlackListEvent(this, address, test);
  26. publisher.publishEvent(event);
  27. return;
  28. }
  29. }
  30. }

3.编写相应的事件监听对象

  1. /**
  2. * @Author:qmfang
  3. * @Description: 实现ApplicationListener的接口将监听相应的事件,当有事件时,将触发onApplicationEvent方法
  4. * @Date:Created in 10:44 2018/4/8
  5. * @Modified By:
  6. */
  7. public class BlackListNotifier implements ApplicationListener<BlackListEvent> {
  8. @Override
  9. public void onApplicationEvent(BlackListEvent event) {
  10. System.out.println("应用事件:" + event);
  11. }
  12. }

4.将相应的对象注入到容器

  1. /**
  2. * @Author:qmfang
  3. * @Description:
  4. * @Date:Created in 9:58 2018/4/8
  5. * @Modified By:
  6. */
  7. @Configuration
  8. public class MvcConfig {
  9. @Bean
  10. public EmailService emailService() {
  11. EmailService emailService = new EmailService();
  12. List list = Arrays.asList("known.spammer@example.org", "known.hacker@example.org", "john.deo@example.org");
  13. emailService.setBlackList(list);
  14. return emailService;
  15. }
  16. @Bean
  17. public BlackListNotifier blackListNotifier() {
  18. BlackListNotifier blackListNotifier = new BlackListNotifier();
  19. return blackListNotifier;
  20. }
  21. }

4.编写测试

  1. @RestController
  2. public class TestController {
  3. @Autowired
  4. private EmailService emailService;
  5. /**
  6. * 127.0.0.1:8886/test
  7. *
  8. * @return
  9. */
  10. @GetMapping("/test")
  11. public Object get() {
  12. emailService.sendEmail("known.spammer@example.org", "test@test");
  13. return "hello";
  14. }
  15. }

基于注解的事件处理

基于注解的可以将BlackListNotifier修改为如下形式

  1. /**
  2. * @Author:qmfang
  3. * @Description: 实现ApplicationListener的接口将监听相应的事件,当有事件时,将触发onApplicationEvent方法
  4. * @Date:Created in 10:44 2018/4/8
  5. * @Modified By:
  6. */
  7. public class BlackListNotifier {
  8. /**
  9. * 也可以基于注解的形式 参数表名了需要监听的事件类型
  10. *
  11. * @param event
  12. */
  13. @EventListener
  14. public void onApplicationEvent(BlackListEvent event) {
  15. System.out.println("应用事件:" + event);
  16. }
  17. /**
  18. * 如果方法需要监听好几个事件,或者如果定义的方法没有任何参数,也可以在注解自身上指定事件类型
  19. */
  20. @EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})
  21. public void handleContextStart() {
  22. System.out.println("监听到事件");
  23. }
  24. /**
  25. * 也可以通过定义SpEL表达式的注解condition属性添加额外的运行时过滤,
  26. * 他们应该匹配以实际调用特定事件的方法,例如如果事件的test属性等于foo
  27. * 才回调该方法
  28. * <p>
  29. * SpEL可用的元数据
  30. * 名字 位置 描述 例子
  31. * Event事件 根对象 实际应用事件 #root.event
  32. * Arguments array参数数组 根对象 用于目标数组 #root.args[0]
  33. * Argumentname参数名称 评估上下文 任意方法参数的名字
  34. */
  35. @EventListener(condition = "#event.test=='foo'")
  36. public void onApplicationEvent2(BlackListEvent event) {
  37. System.out.println("应用事件2:" + event);
  38. }
  39. /**
  40. * 如果需要发布一个事件作为另一个事件的结果,只需要更改方法签名返回应该发布的事件即可
  41. * 注意点是:异步事件不支持此功能
  42. */
  43. @EventListener
  44. public OtherEvent handlerBlackListEvent(BlackListEvent event) {
  45. }
  46. }
转载出处:https://blog.youkuaiyun.com/fang_qiming/article/details/79849394
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值