springboot---監聽ApplicationListener和ApplicationEvent簡單使用
阿新 • • 發佈:2018-11-27
監聽ApplicationListener和ApplicationEvent簡單使用
本來想實現一個功能就是,在登入成功之後,將一些使用者的資訊,放入到session裡面,本來想,在登入介面成功回撥之後,在寫,但是想做一個監聽的動作,監聽著登入成功的介面,之後走到監聽函式裡面,進行業務處理。
1、首先寫一個監聽類,之後實現ApplicationListener介面,實現其中的public void onApplicationEvent(ApplicationEvent event)方法,其實ApplicationListener介面可以使用泛型,但是我為了防止以後還有監聽,所以我沒有寫泛型,而是在onApplicationEvent
package cn.springboot.yzpt.listener; import cn.springboot.yzpt.common.SuccessfulAuthenticationEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; public class SuccessLoginListener implements ApplicationListener { private static Logger logger = LoggerFactory.getLogger(SuccessLoginListener.class); @Override public void onApplicationEvent(ApplicationEvent event) { if(event instanceof SuccessfulAuthenticationEvent){ SuccessfulAuthenticationEvent successfulAuthenticationEvent = (SuccessfulAuthenticationEvent)event; logger.info(successfulAuthenticationEvent.toString()); } } }
2、緊接著需要建立一個監聽事件,需要繼承ApplicationEvent這個類,這裡面所需要的屬性,自己去定義,我定義HttpServletRequest httpServletRequest; HttpServletResponse httpServletResponse; String sessionId,這三個屬性,用來滿足我的業務需求。
package cn.springboot.yzpt.common; import org.springframework.context.ApplicationEvent; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SuccessfulAuthenticationEvent extends ApplicationEvent { private static final long serialVersionUID = 3039313222160544111L; private HttpServletRequest httpServletRequest; private HttpServletResponse httpServletResponse; private String sessionId; public SuccessfulAuthenticationEvent(Object source,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) { super(source); this.httpServletRequest = httpServletRequest; this.httpServletResponse = httpServletResponse; } public String getSessionId() { return sessionId; } public void setSessionId(String sessionId) { this.sessionId = sessionId; } public HttpServletResponse getHttpServletResponse() { return httpServletResponse; } public void setHttpServletResponse(HttpServletResponse httpServletResponse) { this.httpServletResponse = httpServletResponse; } public HttpServletRequest getHttpServletRequest() { return httpServletRequest; } public void setHttpServletRequest(HttpServletRequest httpServletRequest) { this.httpServletRequest = httpServletRequest; } }
3、接著需要在springboot的啟動類裡面配置一下監聽。
package cn.springboot.yzpt;
import cn.springboot.yzpt.listener.SuccessLoginListener;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@MapperScan("cn.springboot.yzpt.mapper")
public class YzptApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(YzptApplication.class, args);
//裝載監聽
context.addApplicationListener(new SuccessLoginListener());
}
}
4、這個時候需要設定一個觸發這個事件的方法。
package cn.springboot.yzpt.common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class EventPublisher {
@Autowired
private ApplicationContext applicationContext;
public void pushlish(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
applicationContext.publishEvent(new SuccessfulAuthenticationEvent(this,httpServletRequest,httpServletResponse));
}
}
5、最後就是在自己的業務邏輯裡面呼叫這個觸發的方法了,在登入成功裡面寫這個觸發的事件,用來處理這個介面之後的業務邏輯。
package cn.springboot.yzpt.controller.loginUser;
import cn.springboot.yzpt.common.CodeEnums;
import cn.springboot.yzpt.common.EventPublisher;
import cn.springboot.yzpt.common.HttpRequestEntity;
import cn.springboot.yzpt.exception.YzptException;
import cn.springboot.yzpt.server.loginUser.LoginUserServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
@RestController
@RequestMapping("/loginUser")
public class LoginUserController {
private static Logger logger = LoggerFactory.getLogger(LoginUserController.class);
@Autowired
LoginUserServer loginUserServer;
@Autowired
EventPublisher eventPublisher;
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public HttpRequestEntity login(@RequestBody Map<String, String> userInfo, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
HttpRequestEntity httpRequestEntity = new HttpRequestEntity();
try {
boolean loginType = loginUserServer.login(userInfo.get("user"), userInfo.get("password"));
httpServletRequest.getSession().setAttribute("id", "12314434231");
if (loginType) {
httpRequestEntity.setMessage("登陸成功");
eventPublisher.pushlish(httpServletRequest,httpServletResponse);
logger.info(userInfo.get("user") + "登陸成功");
}
} catch (YzptException e) {
httpRequestEntity.setCode(e.getCode());
httpRequestEntity.setMessage(e.getErrMsg());
} catch (Exception e) {
logger.error("/loginUser/login:出現異常", e);
httpRequestEntity.setCodeEnums(CodeEnums.SYSTEM_ERR);
}
return httpRequestEntity;
}
}