Spring高级 事件监听器 (一)
事件监听器的两种方式
- 实现 ApplicationListener 接口
- 根据接口泛型确定事件类型
- @EventListener 标注监听方法
- 根据监听器方法参数确定事件类型
- 解析时机:在 SmartInitializingSingleton(所有单例初始化完成后),解析每个单例 bean
一、 实现 ApplicationListener 接口 作为监听器 (根据接口泛型确定事件类型)
1、事件发布器
package com.mangoubiubiu.event; import org.springframework.context.ApplicationEvent; public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } }
2、事件监听器
package com.mangoubiubiu.event.listener; import com.mangoubiubiu.event.MyEvent; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Slf4j @Component public class EmailListener implements ApplicationListener<MyEvent> { @Override public void onApplicationEvent(MyEvent event) { log.info("发送邮件"); } }
package com.mangoubiubiu.event.listener; import com.mangoubiubiu.event.MyEvent; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component @Slf4j public class SmsListener implements ApplicationListener<MyEvent> { @Override public void onApplicationEvent(MyEvent event) { log.info("发送短信"); } }
3、发布事件(注意:一个事件可以被多个监听器监听)
package com.mangoubiubiu.controller; import com.mangoubiubiu.event.MyEvent; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; @Api(tags = "首页模块") @RestController @RequiredArgsConstructor public class UserController { final ApplicationEventPublisher publisher; @ApiImplicitParam(name = "name",value = "姓名",required = true) @ApiOperation(value = "向客人问好") @GetMapping("/sayHi") public ResponseEntity<String> sayHi(@RequestParam(value = "name")String name){ publisher.publishEvent(new MyEvent("UserController.sayHi")); return ResponseEntity.ok("Hi:" + name); } }
二、@EventListener 标注监听方法
- 根据监听器方法参数确定事件类型
- 解析时机:在 SmartInitializingSingleton(所有单例初始化完成后),解析每个单例 bean
2、事件监听器
package com.mangoubiubiu.show.event.listener; import com.mangoubiubiu.show.event.MyEvent; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Slf4j @Component public class EmailListener implements ApplicationListener<MyEvent> { @Override public void onApplicationEvent(MyEvent event) { log.info("发送邮件"); } }
package com.mangoubiubiu.show.event.listener; import com.mangoubiubiu.show.event.MyEvent; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component @Slf4j public class SmsListener implements ApplicationListener<MyEvent> { @Override public void onApplicationEvent(MyEvent event) { log.info("发送短信"); } }
3、发布事件 后测试