Spring Boot 編寫Servlet、Filter、Listener、Interceptor的方法
前言
在編寫過濾器、監(jiān)聽器、攔截器之前我們需要在spring-boot啟動(dòng)的類上加上注解@ServletComponentScan:
@SpringBootApplication @ServletComponentScan public class MySpringbootApplication { public static void main(String[] args) { SpringApplication.run(MySpringbootApplication.class, args); } }
Servlet
spring-boot編寫過濾器和spring中差不多,直接看代碼:
@WebServlet(urlPatterns = "/serv") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { System.out.println("------------doget-------------"); doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { System.out.println("------------dopost-------------"); } }
其實(shí)也就是注解的不同而已:
@WebServlet(urlPatterns = "/serv")
過濾器(Filter)
在spring-boot里編寫過濾器我們只需要實(shí)現(xiàn)javax.servlet.Filter
@WebFilter(filterName = "myFilter", urlPatterns = "/*") public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("初始化過濾器"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("執(zhí)行過濾器"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { System.out.println("銷毀過濾器!"); } }
然后添加一個(gè)注解:
@WebFilter(filterName = "myFilter", urlPatterns = "/*")
監(jiān)聽器 (Listener)
在上面,看了下過濾器的使用。其實(shí)監(jiān)聽器和攔截器就差不多了,直接上代碼:
@WebListener public class MyHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { System.out.println("session 被創(chuàng)建"); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { System.out.println("session 被摧毀"); } }
我們發(fā)現(xiàn)只是注解發(fā)生了變化:
@WebListener
攔截器(Interceptor)
攔截器大致和上面差不多,不過有一點(diǎn)點(diǎn)不同。我們知道在web開發(fā)中,可以使用過濾器和攔截器來過濾外部的web請(qǐng)求。但是攔截器提供了更加細(xì)致的控制功能。主要有:請(qǐng)求之前、請(qǐng)求之后渲染之前、渲染之后、請(qǐng)求全部結(jié)束之后這四個(gè)步驟的攔截。
這里面使用攔截器主要有三個(gè)步驟
自定義攔截器,實(shí)現(xiàn)org.springframework.web.servlet.HandlerInterceptor
自定義WebAppConfigurer,繼承WebMvcConfigurerAdapter
在自定義的WebAppConfigurer覆蓋父類方法addInterceptors(InterceptorRegistry registry),并在方法中添加自己定義的攔截器
public class MyInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println(MyInterceptor.class.getName()+" : 在請(qǐng)求之前調(diào)用"); return true; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println(MyInterceptor.class.getName()+" :請(qǐng)求處理之后視圖渲染之前使用"); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println(MyInterceptor.class.getName()+" :請(qǐng)視圖渲染之后使用"); } } @Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { // 多個(gè)攔截器組成一個(gè)攔截器鏈 // addPathPatterns 用于添加攔截規(guī)則 // excludePathPatterns 用戶排除攔截 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**"); super.addInterceptors(registry); } }
以上就是關(guān)于在spring-boot中如何定義過濾器、監(jiān)聽器和攔截器。關(guān)于他們的原理以及一些細(xì)節(jié)問題(如攔截器的攔截順序),就不詳述。有興趣的可以去網(wǎng)上搜索。
相關(guān)文章
SpringBoot自動(dòng)重啟、熱啟動(dòng)方式
這篇文章主要介紹了SpringBoot自動(dòng)重啟、熱啟動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Idea自定義方法注釋模板的教程詳解(去param括號(hào)、return全類名)
這篇文章主要介紹了Idea自定義方法注釋模板(去param括號(hào)、return全類名),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08使用java.nio.file?庫(kù)優(yōu)雅的操作文件詳解
這篇文章主要介紹了使用java.nio.file?庫(kù)優(yōu)雅的操作文件詳解,需要的朋友可以參考下2023-05-05Java?Swing實(shí)現(xiàn)畫板的簡(jiǎn)單操作
這篇文章主要介紹了Java?Swing實(shí)現(xiàn)畫板的簡(jiǎn)單操作,修改顏色,更改圖形,清除,任務(wù)欄按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06