ddsspring中的事件机制使用到设计模式中的观察者模式 ,观察者模式有两个概念,1.观察者、被观察者。2.被观察者做出相应得动作,观察者能接收到。不分析设计模式,学习下spring中的事件机制实际开发如何使用 及使用场景 。

spring中的事件机制涉及到者几个类文件 :ApplicationEvent(事件类型)、ApplicationListener(事件监听类)、ApplicationEventPublisher(事件发布类)。先看看这几个类的继承、实现关系:

Application类继承jdk Utill包中的 EventObject,下面实现了很多子类:

ApplicationListener :接口继承了utill 包中的EventListener接口,泛型参数E必须继承ApplicationEvent类

ApplicationEventPublisher: 事件发布接口 ,实现类很多,其中子类ApplicationContext,发布事件就用ApplicationContext类去发布 

使用方法:

  1.声明事件类型

public class DemoEvent extends ApplicationEvent{
private static final long serialVersionUID = 1L; public DemoEvent(Object source) {
super(source);
} }

2.事件监听类

@Component  // 注意这里要把类注册到spring容器中
public class DemoListener implements ApplicationListener<DemoEvent> {//1
@Override
public void onApplicationEvent(DemoEvent event) {//2
Object o = event.getSource();
System.out.println("我(bean-demoListener)接收到了bean-demoPublisher发布的消息:"+ o); } }

3.配置类

@Configuration
@ComponentScan("springboot.springboot_learn.event") //扫描加载bean文件
public class EventConfig { }

4.事件发布 ,使用AnnotationConfigApplicationContext 类,这个为ApplicationContext的子类

public class Client {
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(EventConfig.class); context.publishEvent(new DemoEvent("我要发财")); }
}

实际开发中使用场景 :提交一个订单order,成功后给用户发送短信,大多时候我们会写类似一下代码

public String submitOrder(OrderInfo orderinfo){
... // 省略代码
String rsMassage = dao.add(orderinfo);//插入信息
Boolean flag = sendSms(rsMassage); //发送短信
return flag ; //返回是否成功
}

这个代码一看是没有毛病。但是如果后续老板叫你,发送成功后还要给用户发个微信,还要给用户发个QQ...?,那么就要往submitOrder方法继续的添加代码,根据代码的开闭原则,这并不是最好的做法。那么可以做到,不修改代码,做到添加业务逻辑吗?利用spring的事件机制改造代码设计模式来实现。

1.申明事件类型

public class OrderEvent extends ApplicationEvent{
private static final long serialVersionUID = 1L; public OrderEvent(Object source) {
super(source);
} }

2.申明事件监听

@Component
class WeiChatListener implements ApplicationListener<OrderEvent>{ @Override
public void onApplicationEvent(OrderEvent event) {
System.out.println("微信发送短信:" + event.getSource());
} }

3.发布事件

 @Autowired
ApplicationContext applicationContext; //1
public String submitOrder(OrderInfo orderinfo){
... // 省略代码
String rsMassage = dao.add(orderinfo);//插入信息
applicationContext.publishEvent(new OrderEvent("我要发财"));
return "" ; //返回是否成功
}

后来老板叫你还要加个普通的短信?现在怎么办 ?修改代码吗 ?No 直接加个普通短信事件监听,注入实例到spring容器就ok了 ;

@Component
class massageListener implements ApplicationListener<OrderEvent>{ @Override
public void onApplicationEvent(OrderEvent event) {
System.out.println("发送不通短信:" + event.getSource());
} }

还要加别的xxx短信 ?? 。ok继续加个Listener,是不是这样优秀多了 0-0

最新文章

  1. asp.net分页控件
  2. SSL介绍与Java实例
  3. tcp连接listen的backlog剖析
  4. OpenGL官方教程——着色器语言概述
  5. 遍历List过程中删除元素的正确做法(转)
  6. Python 特殊语法:filter、map、reduce、lambda
  7. ASP.NET MVC Filter
  8. EBS多OU和多帐套客户化总结
  9. Nginx 499错误的原因及解决方法
  10. js闭包简要分析
  11. android4.0 禁止横竖屏切换使用 android:configChanges=&quot;orientation|keyboardHidden&quot;无效的解决方法
  12. java总结,错误集
  13. Let&#39;s Encrypt: 为CentOS/RHEL 7下的nginx安装https支持-具体案例
  14. springmvc跨域+token验证(app后台框架搭建二)
  15. Spring入门看这一篇就够了
  16. Codeforces Round #475 Div. 1
  17. Jmeter 批量执行脚本之-----------Ant
  18. 【sqli-labs】Less7
  19. 网络编程并发 多进程 进程池,互斥锁,信号量,IO模型
  20. 关于hdfs 和hive的数据迁移

热门文章

  1. 常用的 21 条 Linux 命令,生产力必备
  2. .net core api 请求实现接口幂等性
  3. ubuntu install redis
  4. Intellij Idea显示回退和前进按钮的方法
  5. super和this
  6. 我罗斯方块第二次作业(Player类)
  7. SpringCloud升级之路2020.0.x版-37. 实现异步的客户端封装配置管理的意义与设计
  8. C/C++ Qt 给ListWidget增加右键菜单
  9. R语言与医学统计图形-【27】ggplot2图形组合、字体、保存
  10. 关于AnnotationHub的一些应用