# spring-boot-event **Repository Path**: i-upon/spring-boot-event ## Basic Information - **Project Name**: spring-boot-event - **Description**: spring boot 事件监听,解耦业务,通过ApplicationEventPublisher 发布事件 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2019-04-07 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ##说明 spring boot 事件监听,解耦业务,通过ApplicationEventPublisher 发布事件 通过`ApplicationEventPublisher`发布事件,在Controller里使用`@EventListener`监听事件,如果想实现异步解耦业务, 需要全局启用异步任务注解`@EnableAsync`,然后在事件监听里面加上`@Async`,也可以通过`@Order()`对不同的业务实现按顺序 加载 ###测试用例代码 ```java @Slf4j @Component public class ApplicationReadyEventListener implements ApplicationListener { @Autowired PlayerOnLoginService playerOnLoginService; @Override public void onApplicationEvent(ApplicationReadyEvent event) { log.info("......ApplicationReadyEvent......"); log.info("开始测试"); RequestVO requestVO = new RequestVO(); requestVO.setPid("xiaowugege"); playerOnLoginService.initPlayerInfo(requestVO); log.info("结束测试"); } } ``` ```java @Slf4j @Service public class PlayerOnLoginService { @Autowired ApplicationEventPublisher publisher; @Async public void initPlayerInfo(RequestVO requestVO) { log.info("异步推送玩家初始化数据信息.."); final PlayerOnLoginEvent playerOnLoginEvent = new PlayerOnLoginEvent<>(requestVO); publisher.publishEvent(playerOnLoginEvent); } } ``` ```java @Component @Slf4j public class BaseInfoController { @EventListener @Order(0) public void initPlayerInfo(PlayerOnLoginEvent playerOnLoginEvent){ log.info("初始化基础信息:{}",playerOnLoginEvent.getRequestVO()); } } ```