javascript
【Spring框架】 ☞ 项目启动时执行特定处理及ApplicationListener源码分析
1、背景
在一些業(yè)務(wù)場(chǎng)景中,在容器啟動(dòng)完成后,需要處理一些諸如:kafka業(yè)務(wù)注冊(cè),數(shù)據(jù)處理,初始化緩存等的操作。
本文重點(diǎn)介紹如何在服務(wù)啟動(dòng)中,或啟動(dòng)完成時(shí)執(zhí)行相關(guān)處理。
2、針對(duì)上述場(chǎng)景,有如下實(shí)現(xiàn)方法
A:實(shí)現(xiàn)基于javaweb的ServletContextListener接口,重寫contextInitialized()接口
?
?
B:實(shí)現(xiàn)基于javaweb的javax.servlet包下的Filter接口,重寫init(FilterConfig filterConfig)接口
?
?
C:繼承基于javaweb的servlet包下的HttpServlet父類,重寫init()接口即可
?
?
D:實(shí)現(xiàn)基于Spring的ApplicationListener<ContextRefreshedEvent>接口,重寫onApplicationEvent(ContextRefreshedEvent event)接口
?
?
?總結(jié):基于javaweb實(shí)現(xiàn)的接口,均需要在web.xml注冊(cè)才可以啟動(dòng)時(shí)掃描到?;赟pring實(shí)現(xiàn)的,需要把對(duì)象注冊(cè)到容器中。
下面是在一個(gè)加載順序,可以看出 Listener和Filter是在Servlet之前執(zhí)行的。
?
3、ApplicationListener源碼分析
? 可以先看下?https://blog.csdn.net/u014263388/article/details/78996509
? 后續(xù)會(huì)上源碼分析
轉(zhuǎn)載于:https://www.cnblogs.com/liuxs13/p/9284445.html
總結(jié)
以上是生活随笔為你收集整理的【Spring框架】 ☞ 项目启动时执行特定处理及ApplicationListener源码分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。