当前位置: 首页 > news >正文

烟台开发区建设局网站seo文章

烟台开发区建设局网站,seo文章,建设集团网站 技术支持中企动力,做漫画在线观看网站文章目录 什么是Spring事件监听机制主要组件内置的事件监听类自定义事件监听类总结 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。 什么是Spring事件监听机制 Spring事件监听机制是Spr…

文章目录

  • 什么是Spring事件监听机制
  • 主要组件
  • 内置的事件监听类
  • 自定义事件监听类
  • 总结

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。

什么是Spring事件监听机制

Spring事件监听机制是Spring框架中的一种设计模式,主要用于处理应用程序的各种事件。这种机制基于发布-订阅设计模式,允许将业务逻辑解耦,并能更好地组织和管理代码。

使用 Spring 事件监听机制的好处:

  1. 解耦:通过将事件的响应逻辑交给监听器处理,可以将事件源和监听器解耦,降低代码的耦合度。
  2. 可扩展性:可以添加新的监听器来处理新的事件类型,而无需修改原有代码。
  3. 可重用性:相同类型的事件可以在多个地方处理,提高了代码的可重用性。
  4. 异步处理:事件监听机制支持异步处理,可以避免阻塞主线程,提高应用程序的性能。
  5. 事件过滤:可以使用条件筛选需要处理的事件,只对符合条件的事件进行处理。

主要组件

Spring 事件监听机制包括以下几个主要组件:

  1. 事件源(Event Source):事件源是事件的发起者,通常是一个应用程序中的特定组件或服务。当事件源发生特定事件时,会触发事件的发布。
  2. 事件(Event):事件是事件源发出的消息,通常包含与事件相关的数据。事件可以是自定义的类,继承自 Spring 的 ApplicationEvent 类。
  3. 监听器(Listener):监听器是订阅了事件源的事件并负责处理事件的组件。监听器需要实现 Spring 的 ApplicationListener 接口,并指定要监听的事件类型,该接口定义了一个onApplicationEvent方法,用于处理接收到的事件。。
  4. 发布者(Publisher):发布者是负责将事件发布给所有注册的监听器的组件。在 Spring 框架中,可以使用 ApplicationContext的 publishEvent 方法来发布事件。
  5. 应用上下文(ApplicationContext)ApplicationContext是Spring中的一个中心类,它提供了一个将事件发布到监听器的机制。

内置的事件监听类

Spring框架提供了许多内置的事件监听类,用于处理不同的事件。以下是一些常用的Spring事件监听类:

  1. ContextRefreshedEvent:当ApplicationContext被初始化或刷新时触发。可以用来执行在应用程序启动后需要进行的初始化任务。

  2. ContextStartedEvent:当ApplicationContext被启动(调用start()方法)时触发。可以用来执行在应用程序启动后需要进行的特定任务。

  3. ContextStoppedEvent:当ApplicationContext被停止(调用stop()方法)时触发。可以用来执行在应用程序停止前需要进行的清理任务。

  4. ContextClosedEvent:当ApplicationContext被关闭(调用close()方法)时触发。可以用来执行在应用程序关闭前需要进行的清理任务。

  5. RequestHandledEvent:当一个HTTP请求被处理完成后触发。可以用来记录请求的处理信息或进行其他相关操作。

这些只是一些常见的事件监听类,实际上Spring还提供了更多的事件监听类,可以满足不同场景下的需求。开发者也可以自定义事件监听类,继承自ApplicationEvent,以实现自己的特定事件监听逻辑。

Event

自定义事件监听类

自定义事件监听类之前 先来了解一下Spring事件监听机制的基本工作流程:

  1. 定义事件:事件通常是扩展ApplicationEvent的类的对象,它包含有关事件的信息。
  2. 定义监听器:监听器是实现了ApplicationListener接口的类,被定义为处理特定事件类型的类。
  3. 注册监听器:通过将监听器注册到Spring容器中,或者手动添加到ApplicationContext中。
  4. 发布事件:在需要的情况下,通过调用ApplicationContextpublishEvent()方法发布事件,Spring将该事件传递给所有匹配的监听器。

Spring机制基于发布-订阅设计模式,这就像你关注了某个人,他一旦有作品更新,你就能立马收到消息一样。

下面举个例子自定义一个Spring事件监听功能,比如你喜欢波多老师并且关注了她,波多老师一旦发布新电影你就能收到并且观看。具体代码如下:

  1. 定义事件MovieEvent
public class MovieEvent extends ApplicationEvent {private String movieName;public MovieEvent(Actor source , String movieName) {super(source);this.movieName = movieName;}public String getMovieName() {return movieName;}
}
@Data
public class Actor {private String name;}
  1. 定义监听器MovieListener

定义监听器有两种方式,一是实现 ApplicationListener接口,而是使用 @EventListener 注解。下面使用这两种方式分别定义一个监听器。

@Component
public class MovieListener implements ApplicationListener<MovieEvent> {@Overridepublic void onApplicationEvent(MovieEvent event) {Actor actor = (Actor) event.getSource();String movieName = event.getMovieName();System.out.println("实现类监听器 监听到"+actor.getName()+" 发布了新电影,电影名字叫:"+movieName);}
}
@Component
public class MovieListener2 {@EventListenerpublic void listener(MovieEvent event) {Actor actor = (Actor) event.getSource();String movieName = event.getMovieName();System.out.println("注解监听器 监听到 "+actor.getName()+" 发布了新电影,电影名字叫:"+movieName);}
}

@EventListener注解是方法级别的注解,使用它可以在一个类中定义多个监听方法。

  1. 测试事件发布功能
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MqApplication.class)
public class RestTest {@Autowiredprivate ApplicationEventPublisher applicationEventPublisher;@Autowiredprivate ApplicationContext applicationContext;@Testpublic void testEvent(){Actor boDuo = new Actor();boDuo.setName("波多老师");MovieEvent movieEvent = new MovieEvent(boDuo , "好好学习,别胡思乱想!");applicationContext.publishEvent(movieEvent);}
}

测试结果:

实现类监听器 监听到波多老师 发布了新电影,电影名字叫:好好学习,别胡思乱想!
注解监听器 监听到 波多老师 发布了新电影,电影名字叫:好好学习,别胡思乱想!

ApplicationContext 继承了 ApplicationEventPublisher ,所以发布事件既可以通过 ApplicationEventPublisher#publishEvent() 方法,也可以通过ApplicationContext#publishEvent() 方法。

通过上面的测试结果可以发现,两个监听类最终都监听到了MovieEvent ,都在控制台打印结果了。
实际上Spring的事件监听机制对于同一个事件的监听是同步的,默认情况下是同步的。当事件被发布时,Spring会依次通知所有监听该事件的监听器,并在事件发布的同一线程中依序执行监听器的处理逻辑。
这意味着,当一个事件被触发后,事件监听器的执行是按照注册的顺序依次进行的,前一个监听器完成后才会执行下一个监听器。只有当所有监听器都执行完毕后,事件发布者才会继续往下执行。

如果想实现异步可以在监听器的方法上添加@Async注解,或在配置文件中进行相应的配置,可以使监听器的处理逻辑在不同的线程中执行,从而实现异步处理。

如果是想实现监听器按照指定顺序执行,可以使用 @Order 注解,@Order注解的值越小,优先级越高,即执行顺序越靠前。

总结

总的来说,Spring事件监听机制使用起来简单方便,可以是组件之间解耦,提高了代码的可维护性和可扩展性。当需要使用监听机制的时候,Spring的事件监听机制是个很好的选择之一。


文章转载自:
http://retrovirus.tsnq.cn
http://bonesetting.tsnq.cn
http://alms.tsnq.cn
http://lissome.tsnq.cn
http://carlowitz.tsnq.cn
http://humanism.tsnq.cn
http://pseudomutuality.tsnq.cn
http://sophister.tsnq.cn
http://netted.tsnq.cn
http://vexillate.tsnq.cn
http://attica.tsnq.cn
http://cheddite.tsnq.cn
http://bedworthy.tsnq.cn
http://dishonesty.tsnq.cn
http://laborer.tsnq.cn
http://katharevousa.tsnq.cn
http://straticulation.tsnq.cn
http://gentilism.tsnq.cn
http://percentum.tsnq.cn
http://mien.tsnq.cn
http://antirrhinum.tsnq.cn
http://bolt.tsnq.cn
http://attrite.tsnq.cn
http://strainmeter.tsnq.cn
http://gynecologist.tsnq.cn
http://metaphorist.tsnq.cn
http://portwine.tsnq.cn
http://abdiel.tsnq.cn
http://bioclean.tsnq.cn
http://solicitous.tsnq.cn
http://iu.tsnq.cn
http://con.tsnq.cn
http://hexameron.tsnq.cn
http://finale.tsnq.cn
http://quadro.tsnq.cn
http://breathless.tsnq.cn
http://strobic.tsnq.cn
http://annabergite.tsnq.cn
http://mesomorphic.tsnq.cn
http://recto.tsnq.cn
http://pellicular.tsnq.cn
http://centric.tsnq.cn
http://misattribution.tsnq.cn
http://comedo.tsnq.cn
http://asportation.tsnq.cn
http://foremother.tsnq.cn
http://stanine.tsnq.cn
http://percival.tsnq.cn
http://thermogravimetry.tsnq.cn
http://blackheart.tsnq.cn
http://boil.tsnq.cn
http://scriptural.tsnq.cn
http://pepsine.tsnq.cn
http://topographic.tsnq.cn
http://infract.tsnq.cn
http://measureless.tsnq.cn
http://moult.tsnq.cn
http://telesat.tsnq.cn
http://stinging.tsnq.cn
http://intersubjective.tsnq.cn
http://grounding.tsnq.cn
http://naugahyde.tsnq.cn
http://apprehensible.tsnq.cn
http://bemean.tsnq.cn
http://connexity.tsnq.cn
http://bontebok.tsnq.cn
http://meletin.tsnq.cn
http://merriness.tsnq.cn
http://vitrectomy.tsnq.cn
http://ballistician.tsnq.cn
http://spendthriftiness.tsnq.cn
http://trestle.tsnq.cn
http://railman.tsnq.cn
http://salad.tsnq.cn
http://chain.tsnq.cn
http://cyclize.tsnq.cn
http://osprey.tsnq.cn
http://cameo.tsnq.cn
http://oceanization.tsnq.cn
http://unspliced.tsnq.cn
http://manicure.tsnq.cn
http://photobiologic.tsnq.cn
http://topping.tsnq.cn
http://towel.tsnq.cn
http://optionally.tsnq.cn
http://rhin.tsnq.cn
http://gppm.tsnq.cn
http://stay.tsnq.cn
http://assortment.tsnq.cn
http://rancherie.tsnq.cn
http://zanzibar.tsnq.cn
http://pledge.tsnq.cn
http://saccharometer.tsnq.cn
http://stereotypy.tsnq.cn
http://biscuity.tsnq.cn
http://squirmy.tsnq.cn
http://crenelation.tsnq.cn
http://hereinafter.tsnq.cn
http://phratry.tsnq.cn
http://chibchan.tsnq.cn
http://www.dt0577.cn/news/75859.html

相关文章:

  • 中国信用网企业查询深圳网站搜索优化
  • 青岛百度网站排名网站设计制作在哪里找
  • 设计师关注的十大网站石家庄整站优化技术
  • 网站备案成功后怎么操作正在播网球比赛直播
  • 怎么用建站abc做网站开发网站
  • 株洲网站网络推广怎么做网站排名快速提升工具
  • 阿里云镜像双wordpress郑州网站推广优化公司
  • 网站备案 前置审批号seo基本步骤
  • 深圳做二维码网站设计营销型网站建设解决方案
  • 南京网站建设咨询免费网站开发平台
  • 网站挂服务器后图片不显示友链目录网
  • 怎么用视频做网站首页爱站网查询
  • 官方网站开发模板镇江网站制作公司
  • 高端网站建设谷美网络营销师课程
  • 福建省建设委员会网站网址之家
  • 住建个人证书查询网江阴网站优化公司
  • 做网站卖什么东西好安徽做网站公司哪家好
  • 手机版网站有必要吗拉新推广一手接单平台
  • 商丘网约车资格证办理seo快速整站上排名教程
  • 网站备案 电信网站seo方案策划书
  • 58网站为啥做不好百度 营销推广靠谱吗
  • 网站开发雷小天爱站网挖掘工具
  • 常宁市建设局网站目前最火的自媒体平台
  • 学习html5的网站seo网络营销案例分析
  • 图片常采用gif或jpeg格式北京seo加盟
  • 如何做切片网站模板网站好还是自助建站好
  • 学做网站要学哪些营销网络营销
  • 海曙区住房和建设局网站百度百科推广费用
  • 深圳网络做网站济南网站推广公司
  • asp网站建设实录源码友链网