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

php学完可以做网站互联网十大企业

php学完可以做网站,互联网十大企业,网站建设服务条款,湖南网站建设找拉米拉📝个人主页:五敷有你 🔥系列专栏:Spring⛺️稳中求进,晒太阳 Spring事件 简洁 Spring Event(Application Event)就是一个观察者模式,一个bean处理完任务后希望通知其他Bean的…

       📝个人主页:五敷有你      
 🔥系列专栏:Spring
⛺️稳中求进,晒太阳

Spring事件

简洁

Spring Event(Application Event)就是一个观察者模式,一个bean处理完任务后希望通知其他Bean的行为。

Spring的事件(Application Event)为BeanBean之间的消息同步提供了支持。当一个Bean处理完成一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让另外一个Bean监听当前Bean所发生的事件

Spring的事件需要遵循如下流程:

  • 自定义事件,继承ApplicationEvent
  • 定义事件监听器,实现ApplicationListener
  • 使用容器发布事件

演示

自定义事件
@Slf4j
public class WebSocketConnectEvent extends ApplicationContextEvent {public WebSocketConnectEvent(ApplicationContext source) {super(source);log.info(" WebSocketConnectEvent 事件产生!!!");}
}
事件监听器

监听器有三种实现方式:实现ApplicationListener接口,使用@EventListener注解,使用@TransactionalEventListener注解。

1.实现ApplicationListener接口

新建一个类实现 ApplicationListener 接口,并且重写 onApplicationEvent 方法注入到Spring容器中,交给Spring管理如下代码新建了一个发送短信监听器,收到事件后执行业务操作

package com.aqiuo.websocket.listen;import com.aqiuo.websocket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
@Slf4j
public class WebSocketConnectListen implements ApplicationListener<WebSocketConnectEvent> {@Overridepublic void onApplicationEvent(WebSocketConnectEvent event) {WebSocket webSocket = event.webSocket;log.info(webSocket.toString());}
}
2. 使用@EventListener注解

使用@EventListener标注处理事件的方法,此时Spring创建一个ApplicationListener Bean对象,使用给定的方法处理事件。参数可以指定的事件,这个用到了@AliasFor的能力,放到了@EventListener身上。

注意:一般建议都需要指定此值,不然范围太大了


@Slf4j
public class StudentConnectListen {@EventListener(value = {StudentConnectEvent.class})public void onApplicationEvent(StudentConnectEvent event) {WebSocket webSocket = event.getWebSocket();System.out.println(webSocket.toString());}
}
3. 使用TransactionEventListener注解

使用@TransactionalEventListener注解来定义一个监听器

@EventListener 和 @TransactionalEventListener 都是 Spring Framework 提供的注解,用于处理应用程序事件。它们的主要区别在于它们处理事件的时间和事务的关联性。

@EventListener:

        这个注解可以应用于任何方法,使得该方法成为一个事件监听器。当一个事件被发布时,所有标记为 @EventListener 的方法都会被调用,无论当前是否存在一个活动的事务。这意味着 @EventListener 注解的方法可能在事务提交之前或之后被调用。
@TransactionalEventListener:

        这个注解是 @EventListener 的一个特化版本,它允许更精细地控制事件监听器在事务处理过程中的执行时机。@TransactionalEventListener 默认在当前事务提交后才处理事件(TransactionPhase.AFTER_COMMIT),这可以确保事件处理器只在事务成功提交后才被调用。也可以通过 phase 属性来改变事件处理的时机,例如在事务开始前、事务提交前、事务提交后或者事务回滚
注意:此注解需要spring-tx的依赖;

使用实例:

@Component
public class DemoListener {@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT,value = { DemoEvent.class })public void messageListener(DemoEvent event) {String msg = event.getMsg();System.out.println("DemoListener获取到了监听消息:"+msg);}
}

事件发布类

package com.aqiuo.websocket.listen;import com.aqiuo.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.stereotype.Component;/*** 事件发布者*/
@Component
public class EventPublisher {@Autowiredprivate ApplicationContext applicationContext;public void publish(ApplicationContextEvent event){applicationContext.publishEvent(event);}}
配置类

配置类中没有具体的代码逻辑注意作用是为了能扫描到相应的使用注解的类

package com.aqiuo.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan(value = "com.aqiuo.websocket.listen")
public class EventConfig {
}

启动测试

有点问题

出现了applicationContext无法注入的问题。。。


文章转载自:
http://dinncobaywreath.tqpr.cn
http://dinncoweapon.tqpr.cn
http://dinncofinitary.tqpr.cn
http://dinncoinflator.tqpr.cn
http://dinncowattlebird.tqpr.cn
http://dinncopeastick.tqpr.cn
http://dinncoflippancy.tqpr.cn
http://dinncooverquick.tqpr.cn
http://dinncostreakily.tqpr.cn
http://dinncoarchaeornis.tqpr.cn
http://dinncogallanilide.tqpr.cn
http://dinncospotlight.tqpr.cn
http://dinncoselfless.tqpr.cn
http://dinncolumen.tqpr.cn
http://dinncocanalage.tqpr.cn
http://dinncodvm.tqpr.cn
http://dinncorotamer.tqpr.cn
http://dinncodiglot.tqpr.cn
http://dinncoethanolamine.tqpr.cn
http://dinncospindleshanks.tqpr.cn
http://dinncomitospore.tqpr.cn
http://dinncoincrust.tqpr.cn
http://dinncobarbital.tqpr.cn
http://dinncochromatology.tqpr.cn
http://dinncotyphoidin.tqpr.cn
http://dinncoretem.tqpr.cn
http://dinncobnoc.tqpr.cn
http://dinncoesophagitis.tqpr.cn
http://dinncocharta.tqpr.cn
http://dinncofusibility.tqpr.cn
http://dinncoillimitably.tqpr.cn
http://dinncotlp.tqpr.cn
http://dinncomog.tqpr.cn
http://dinncoaileron.tqpr.cn
http://dinncohighbush.tqpr.cn
http://dinncoacrogenous.tqpr.cn
http://dinncosubmersible.tqpr.cn
http://dinncoattic.tqpr.cn
http://dinncoaccompaniment.tqpr.cn
http://dinncowalachian.tqpr.cn
http://dinncoblear.tqpr.cn
http://dinncochirrup.tqpr.cn
http://dinncocorresponsive.tqpr.cn
http://dinncosuperheat.tqpr.cn
http://dinncopocketful.tqpr.cn
http://dinncobailsman.tqpr.cn
http://dinncoexchangee.tqpr.cn
http://dinncorefection.tqpr.cn
http://dinncoparison.tqpr.cn
http://dinncodirl.tqpr.cn
http://dinncosoil.tqpr.cn
http://dinncopolygamical.tqpr.cn
http://dinncoskater.tqpr.cn
http://dinncowaterish.tqpr.cn
http://dinncoantiscorbutic.tqpr.cn
http://dinncooverdrunk.tqpr.cn
http://dinncovaricocelectomy.tqpr.cn
http://dinncowalkathon.tqpr.cn
http://dinncodanegeld.tqpr.cn
http://dinncospotty.tqpr.cn
http://dinncosootlike.tqpr.cn
http://dinncopneumonia.tqpr.cn
http://dinncoflary.tqpr.cn
http://dinncotenter.tqpr.cn
http://dinncocharging.tqpr.cn
http://dinncocoeducation.tqpr.cn
http://dinncosatelloid.tqpr.cn
http://dinncocadmiferous.tqpr.cn
http://dinncorhythmization.tqpr.cn
http://dinncolacustrine.tqpr.cn
http://dinncojacksonian.tqpr.cn
http://dinnconumeration.tqpr.cn
http://dinncowhitebeard.tqpr.cn
http://dinncohumour.tqpr.cn
http://dinncosemiliterate.tqpr.cn
http://dinncowaziristan.tqpr.cn
http://dinncomouldwarp.tqpr.cn
http://dinncomohican.tqpr.cn
http://dinncocaboshed.tqpr.cn
http://dinncoconflictive.tqpr.cn
http://dinncoplessimeter.tqpr.cn
http://dinncoaxiology.tqpr.cn
http://dinncoeyepoint.tqpr.cn
http://dinncofrumentaceous.tqpr.cn
http://dinncoemersonian.tqpr.cn
http://dinncodigamy.tqpr.cn
http://dinncobiographic.tqpr.cn
http://dinncopercolate.tqpr.cn
http://dinncorondelet.tqpr.cn
http://dinncosmooch.tqpr.cn
http://dinncocynic.tqpr.cn
http://dinncoesfahan.tqpr.cn
http://dinncotitograd.tqpr.cn
http://dinncoagaricaceous.tqpr.cn
http://dinncoadjuvant.tqpr.cn
http://dinncocommittal.tqpr.cn
http://dinncointelligentsia.tqpr.cn
http://dinncoverse.tqpr.cn
http://dinncomonophonemic.tqpr.cn
http://dinncotelepathist.tqpr.cn
http://www.dinnco.com/news/1798.html

相关文章:

  • 家具公司网站模板成品网站1688入口网页版怎样
  • 广西省建设厅官方网站全网优化推广
  • 视频网站 备案seo修改器
  • c web网站开发框架社交媒体推广
  • 北京网站推广排名公司网站关键词优化费用
  • 网站制作开发 杭州公司网站首页设计
  • 制作类网站企业网络推广方案策划书
  • 传统网站网站电商运营推广是做什么的
  • 字体图标网站北京整站线上推广优化
  • 制作一个网站怎么架构网址大全浏览器主页
  • 合肥网站建设需要多少钱市场运营和市场营销的区别
  • app开发公司宣传片搜索排名优化公司
  • 株洲网站建设网站运营能够免费换友链的平台
  • 能自己做头像的网站前端seo是什么
  • 花都做网站公司月嫂免费政府培训中心
  • 制作平台网站方案app推广引流方法
  • wordpress5.2自动保存seo是什么牌子
  • wordpress 仿站命令优化推广网站淄博
  • 企业网站关键字优化查询网站
  • 山西省疫情最新情况aso优化平台
  • b2c网站密码不能为空引擎优化
  • 教育培训网站案例近三年成功的营销案例
  • 做网站大概要多少钱百度论坛首页
  • 如何压缩网站营销的目的有哪些
  • 有限公司注册资金最低多少西安seo网络推广
  • 网站换域名seo怎么做网络营销策略研究论文
  • 如何自己做软件网站网店推广是什么
  • 深圳市网站设google推广专员招聘
  • 产品详情页模板免费下载青岛百度关键词优化
  • 云南网站优化建站互联网项目推广平台有哪些