您好,登錄后才能下訂單哦!
這篇文章主要介紹“基于SpringBoot怎么應(yīng)用ApplicationEvent”,在日常操作中,相信很多人在基于SpringBoot怎么應(yīng)用ApplicationEvent問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”基于SpringBoot怎么應(yīng)用ApplicationEvent”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
1.發(fā)起restful請(qǐng)求,根據(jù)請(qǐng)求參數(shù)發(fā)布不同的事件。
2.事件監(jiān)聽(tīng)者,監(jiān)聽(tīng)到事件后,做預(yù)定操作。
3.本例是事件同步處理機(jī)制,即發(fā)布事件后,會(huì)同步監(jiān)聽(tīng)事件。
org.springframework.context.ApplicationEvent,spring的事件對(duì)象。
org.springframework.context.ApplicationListener,事件監(jiān)聽(tīng)者接口。
org.springframework.context.ApplicationEventPublisher,事件發(fā)布者接口。
事件對(duì)象實(shí)現(xiàn)ApplicationEvent。
ExampleApplicationEvent,一個(gè)抽象類。繼承ApplicationEvent,自定義拓展微服務(wù)中需求的一些屬性。
public abstract class ExampleApplicationEvent extends ApplicationEvent { private static final String eventSource = "Example"; private String eventType = null; private Object eventData = null; public ExampleApplicationEvent(Object eventData) { super(eventSource); this.eventData = eventData; } public ExampleApplicationEvent(Object eventData, String eventType) { super(eventSource); this.eventData = eventData; this.eventType = eventType; } public String getEventType() { return eventType; } public Object getEventData() { return eventData; } }
ExampleLocalApplicationEvent,是抽象類ExampleApplicationEvent的實(shí)現(xiàn)類,在此處按需拓展屬性。
public class ExampleLocalApplicationEvent extends ExampleApplicationEvent { public ExampleLocalApplicationEvent(Object eventData) { super(eventData); } public ExampleLocalApplicationEvent(Object eventData, String eventType) { super(eventData, eventType); } }
EventTypeEnum,自定義事件類型枚舉,按需擴(kuò)展。
public enum EventTypeEnum { CHANGE("change", "變更事件"), ADD("add", "新增事件"); private String id; private String name; public String getId() { return id; } public String getName() { return name; } EventTypeEnum(String id, String name) { this.id = id; this.name = name; } public static EventTypeEnum getEventTypeEnum(String id) { for (EventTypeEnum var : EventTypeEnum.values()) { if (var.getId().equalsIgnoreCase(id)) { return var; } } return null; } }
事件監(jiān)聽(tīng)者包括接口和抽象類。
IEventListener,一個(gè)接口,繼承ApplicationListener接口。
@SuppressWarnings("rawtypes") public interface IEventListener extends ApplicationListener { }
AbstractEventListener,一個(gè)抽象類,實(shí)現(xiàn)IEventListener接口。并提供抽象方法便于實(shí)現(xiàn)類擴(kuò)展和代碼解耦。
public abstract class AbstractEventListener implements IEventListener { @Override public void onApplicationEvent(ApplicationEvent event){ if(!(event instanceof ExampleApplicationEvent)){ return; } ExampleApplicationEvent exEvent = (ExampleApplicationEvent) event; try{ onExampleApplicationEvent(exEvent); }catch (Exception e){ e.printStackTrace(); } } protected abstract void onExampleApplicationEvent(ExampleApplicationEvent event); }
OrderEventListener,實(shí)現(xiàn)類AbstractEventListener抽象類。監(jiān)聽(tīng)事件,并對(duì)事件做處理,是一個(gè)業(yè)務(wù)類。
@Slf4j @Component public class OrderEventListener extends AbstractEventListener { @Override protected void onExampleApplicationEvent(ExampleApplicationEvent event) { log.info("OrderEventListener->onSpApplicationEvent,監(jiān)聽(tīng)事件."); Object eventData = event.getEventData(); log.info("事件類型: " + EventTypeEnum.getEventTypeEnum(event.getEventType())); log.info("事件數(shù)據(jù): " + eventData.toString()); } }
事件監(jiān)聽(tīng)者包括接口和實(shí)現(xiàn)類。
IEventPublisher,自定義事件發(fā)布接口,方便擴(kuò)展功能和屬性。
public interface IEventPublisher { boolean publish(ExampleApplicationEvent event); }
LocalEventPublisher,事件發(fā)布實(shí)現(xiàn)類,此類使用@Component,spring的IOC容器會(huì)加載此類。此類調(diào)用ApplicationEventPublisher的publishEvent發(fā)布事件。
@Slf4j @Component("localEventPublisher") public class LocalEventPublisher implements IEventPublisher { @Override public boolean publish(ExampleApplicationEvent event) { try{ log.info("LocalEventPublisher->publish,發(fā)布事件."); log.info("事件類型: " + EventTypeEnum.getEventTypeEnum(event.getEventType())); log.info("事件數(shù)據(jù): " + event.getEventData().toString()); SpringUtil.getApplicationContext().publishEvent(event); }catch (Exception e){ log.info("事件發(fā)布異常."); e.printStackTrace(); return false; } return true; } }
使用Restful請(qǐng)求觸發(fā)事件發(fā)生。
EventController,接收Restful請(qǐng)求。
@Slf4j @RestController @RequestMapping("/event") public class EventController { @Autowired private LocalEventPublisher eventPublisher; @PostMapping("/f1") public Object f1(@RequestBody Object obj) { log.info("EventController->f1,接收參數(shù),obj = " + obj.toString()); Map objMap = (Map) obj; OrderInfo orderInfo = new OrderInfo(); orderInfo.setUserName((String) objMap.get("userName")); orderInfo.setTradeName((String) objMap.get("tradeName")); orderInfo.setReceiveTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")); String flag = (String) objMap.get("flag"); if (StringUtils.equals("change", flag)) { eventPublisher.publish(new ExampleLocalApplicationEvent(orderInfo, EventTypeEnum.CHANGE.getId())); } else if (StringUtils.equals("add", flag)) { eventPublisher.publish(new ExampleLocalApplicationEvent(orderInfo, EventTypeEnum.ADD.getId())); } else { eventPublisher.publish(new ExampleLocalApplicationEvent(orderInfo)); } log.info("EventController->f1,返回."); return ResultObj.builder().code("200").message("成功").build(); } }
OrderInfo,數(shù)據(jù)對(duì)象,放入事件對(duì)象中傳遞。
@Data @NoArgsConstructor public class OrderInfo { private String userName; private String tradeName; private String receiveTime; }
ResultObj,restful返回通用對(duì)象。
@Data @NoArgsConstructor @AllArgsConstructor @Builder public class ResultObj { private String code; private String message; }
URL請(qǐng)求: http://127.0.0.1:8080/server/event/f1
入?yún)ⅲ?/p>
{ "userName": "HangZhou", "tradeName": "Vue進(jìn)階教程", "flag": "add" }
返回值:
{ "code": "200", "message": "成功" }
輸出日志:
到此,關(guān)于“基于SpringBoot怎么應(yīng)用ApplicationEvent”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。