SpringBoot中使用監(jiān)聽器的方法詳解
1.監(jiān)聽器
web監(jiān)聽器是一張Servlet中特殊的類,它們能幫助開發(fā)者監(jiān)聽web中特定的事件,比如ServletContext,HttpSession,ServletRequest的創(chuàng)建和銷毀;變量的創(chuàng)建、銷毀、和修改等。可以在某些動(dòng)作前后增加處理,實(shí)現(xiàn)監(jiān)控
2.SpringBoot中監(jiān)聽器的使用
web監(jiān)聽器的使用場(chǎng)景很多,比如監(jiān)聽servlet上下文用來初始化一些數(shù)據(jù)、監(jiān)聽http session用來獲取當(dāng)前在線的人數(shù)、監(jiān)聽客戶端請(qǐng)求的servletrequest對(duì)象來獲取用戶的訪問信息等。
2.1 監(jiān)聽Servlet上下文對(duì)象
監(jiān)聽Servlet上下文對(duì)象可以用來初始化數(shù)據(jù),用于緩存。舉個(gè)例子:比如用戶在點(diǎn)擊某個(gè)網(wǎng)站的首頁(yè)時(shí),一般都會(huì)展現(xiàn)出首頁(yè)的一些信息,而這些信息基本上或者大部分時(shí)間都保持不變的,但是這些信息都是來自數(shù)據(jù)庫(kù),如果用戶的每次點(diǎn)擊,都要從數(shù)據(jù)庫(kù)中去獲取數(shù)據(jù)的話,用戶量少還可以接受,如果用戶量非常大的話,這對(duì)數(shù)據(jù)庫(kù)也是一筆很大的開銷。
針對(duì)這種首頁(yè)數(shù)據(jù),大部分都不常更新的話,我們完全可以把它們緩存起來,每次用戶點(diǎn)擊的時(shí)候,我們都直接從緩存中拿,這樣既可以提高首頁(yè)的訪問速度,又可以降低服務(wù)器的壓力,如果做得更加靈活一點(diǎn),可以再加個(gè)定時(shí)器,定期的來更新這個(gè)首頁(yè)緩存,就類似于csdn個(gè)人博客首頁(yè)中排名的變化一樣
下面我們針對(duì)這個(gè)功能,來寫一個(gè)Service,模擬一下從數(shù)據(jù)庫(kù)查詢數(shù)據(jù):
package com.example.springdemo1.service; import com.example.springdemo1.pojo.User; import org.springframework.stereotype.Service; @Service public class UserService2 { public User getUser(){ //實(shí)際中會(huì)根據(jù)具體的業(yè)務(wù)場(chǎng)景,從數(shù)據(jù)庫(kù)中查詢對(duì)應(yīng)的信息 return new User(10,"lyh10","123456"); } }
然后寫一個(gè)監(jiān)聽器,實(shí)現(xiàn)ApplicationListener接口,重寫onApplicationEvent方法,將ContextRefreshedEvent對(duì)象傳進(jìn)去,如果我們想在加載或刷新應(yīng)用上下文時(shí),也重新刷新下我們預(yù)加載的資源,就可以通過監(jiān)聽ContextRefreshedEvent來做這樣的事情,如下:
package com.example.springdemo1.util; import com.example.springdemo1.pojo.User; import com.example.springdemo1.service.UserService2; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import javax.servlet.ServletContext; @Component public class MyServletContextListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { //先獲取到application上下文 ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext(); //獲取對(duì)應(yīng)的Service UserService2 userService2 = applicationContext.getBean(UserService2.class); User user = userService2.getUser(); //獲取application域?qū)ο?,將查到的信息放到application域中 ServletContext application = applicationContext.getBean(ServletContext.class); application.setAttribute("user",user); } }
首先通過contextRefreshedEvent來獲取application上下文,再通過application上下文來獲取UserService這個(gè)bean,然后再調(diào)用自己的業(yè)務(wù)代碼獲取相應(yīng)的數(shù)據(jù),最后存儲(chǔ)到application域中,這樣前端在請(qǐng)求相應(yīng)數(shù)據(jù)的時(shí)候,我們就可以直接從application域中獲取信息,減少數(shù)據(jù)庫(kù)的壓力,下面寫一個(gè)controller直接從application域中獲取user信息來測(cè)試一下
package com.example.springdemo1.controller; import com.example.springdemo1.pojo.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; @RestController @RequestMapping("/testListener") public class testController12 { @GetMapping("/user") public User getUser(HttpServletRequest request){ ServletContext application = request.getServletContext(); return (User)application.getAttribute("user"); } }
啟動(dòng)項(xiàng)目,在瀏覽器中輸http://localhost:8082/testListener/user
測(cè)試一下即可,如果正常返回user信息,那么說明數(shù)據(jù)已經(jīng)緩存成功,不過application這種事緩存在內(nèi)存中,對(duì)內(nèi)存會(huì)有消耗。
2.2 監(jiān)聽HTTP會(huì)話Session對(duì)象
監(jiān)聽器還有一個(gè)比較常用的地方就是用來監(jiān)聽session對(duì)象,來獲取在線用戶數(shù)量。
package com.example.springdemo1.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @Component public class MyHttpSessionListener implements HttpSessionListener { private static Logger logger = LoggerFactory.getLogger(MyHttpSessionListener.class); //記錄在線的用戶數(shù)量 public Integer count = 0; @Override public synchronized void sessionCreated(HttpSessionEvent httpSessionEvent){ logger.info("新用戶上線了"); count++; httpSessionEvent.getSession().getServletContext().setAttribute("count",count); } @Override public synchronized void sessionDestroyed(HttpSessionEvent httpSessionEvent){ logger.info("用戶下線了"); count--; httpSessionEvent.getSession().getServletContext().setAttribute("count",count); } }
可以看出,首先該監(jiān)聽器需要實(shí)現(xiàn)HttpSessionListener接口,然后重寫sessionCreated和sessionDestroyed方法,在sessionCreated方法中傳遞一個(gè)httpSessionEvent對(duì)象,然后將當(dāng)前session中的用戶數(shù)量加一,sessionDestroyed方法方法剛好相反,不再贅述,然后我們?cè)賹懸粋€(gè)controller來測(cè)試一下:
package com.example.springdemo1.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; @RestController @RequestMapping("/testMyListener") public class testController13 { /** * 獲取當(dāng)前在線人數(shù),該方法有bug * @param request * @return */ @GetMapping("/total") public String getTotalUser(HttpServletRequest request) { Integer count = (Integer) request.getSession().getServletContext().getAttribute("count"); return "當(dāng)前在線人數(shù):" + count; } }
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
JVM性能調(diào)優(yōu)之運(yùn)行時(shí)參數(shù)小結(jié)
jvm是java的運(yùn)行環(huán)境,在jvm中有很多的參數(shù)可以進(jìn)行設(shè)置,本文主要介紹了JVM性能調(diào)優(yōu)之運(yùn)行時(shí)參數(shù)小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04Java實(shí)現(xiàn)圖書館借閱系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖書館借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03JDK13.0.1安裝與環(huán)境變量的配置教程圖文詳解(Win10平臺(tái)為例)
這篇文章主要介紹了JDK13.0.1安裝與環(huán)境變量的配置教程圖文詳解(Win10平臺(tái)為例),本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01SpringCloud注冊(cè)中心部署Eureka流程詳解
Eureka是Netflix開發(fā)的服務(wù)發(fā)現(xiàn)框架,本身是一個(gè)基于REST的服務(wù),主要用于定位運(yùn)行在AWS域中的中間層服務(wù),以達(dá)到負(fù)載均衡和中間層服務(wù)故障轉(zhuǎn)移的目的2022-11-11Java在指定路徑上創(chuàng)建文件提示不存在解決方法
在本篇文章里小編給大家整理的是一篇關(guān)于Java在指定路徑上創(chuàng)建文件提示不存在解決方法,有需要的朋友們可以參考下。2020-02-02