被监听对象是SysUserDto
package com.richfit.richfit.bootConfig.ListenerEvent; import org.springframework.context.ApplicationEvent; /** * 此类作为监听对象,监听登录用户 与方法queryZhuce在一块 */ public class SysUserDto extends ApplicationEvent { private String userName; private String password; public SysUserDto(Object source, String userName, String password) { super(source); this.userName = userName; this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
利用applicationContext.publishEvent 发布消息
@GetMapping("/queryZhuce") public ListqueryZhuce(@RequestParam(name = "username",required = false)String username, @RequestParam(name = "pass",required = false)String pass, HttpServletRequest request){ //用于监听登录用户 applicationContext.publishEvent(new SysUserDto(this,username,pass)); return xqyService.queryZhuce(username,pass,request); }
用于接收发布的消息
package com.richfit.richfit.bootConfig.ListenerEvent; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.EventListener; /** * 接收被监听的对象 */ @Configuration public class CaiwuLIstenerEvent { @EventListener(SysUserDto.class) public void handle(SysUserDto sysUserDto){ System.out.println("监听到的登录用户是:"+ sysUserDto.getUserName()); } }
以此可以实现对登录名的监听,从而实现某些需求
还没有评论,来说两句吧...