您好,登錄后才能下訂單哦!
springboot過濾器和攔截器以及監(jiān)聽器的代碼怎么寫,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
簡單的springboot 過濾器,攔截器,監(jiān)聽器 使用
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sp</groupId>
<artifactId>spboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<!-- 導(dǎo)入Spirng Boot web 所需的jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 這個(gè)插件,可以將應(yīng)用打包成一個(gè)可執(zhí)行的jar包;-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
過濾器,使用的servlet3 規(guī)范
package com.soft.m;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@WebFilter(filterName="MyFilter",urlPatterns="/*")
public class MyFilter implements Filter {
private static Logger LOG = LoggerFactory.getLogger(MyFilter.class);
public void init(FilterConfig filterConfig) throws ServletException {
LOG.info("初始化過濾器");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String requestURI = req.getRequestURI();
LOG.info("過濾到的請求--->"+requestURI);
chain.doFilter(request, response);
}
public void destroy() {
LOG.info("destroy過濾器");
}
}
監(jiān)聽器 也是servlet 規(guī)范
servletContextListener 監(jiān)聽
package com.soft.m;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Component;
@Component
@WebListener("WebListener")
@ServletComponentScan("com.soft.m")
public class MyListener implements ServletContextListener {
private static Logger LOG = LoggerFactory.getLogger(MyListener.class);
public void contextInitialized(ServletContextEvent sce) {
;
LOG.info("init:"+sce.getSource().getClass().toString());
}
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
LOG.info("contextDestroyed");
}
}
session監(jiān)聽器
package com.soft.m;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebListener
public class MySessionListener implements HttpSessionListener {
private static Logger LOG = LoggerFactory.getLogger(MySessionListener.class);
public void sessionCreated(HttpSessionEvent se) {
LOG.info("sessionCreated:"+se.getSession().getId());
}
public void sessionDestroyed(HttpSessionEvent se) {
LOG.info("sessionDestroyed:"+se.getSession().getId());
}
}
servlet攔截器
package com.soft.m;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterceptor implements HandlerInterceptor {
private static Logger logger = LoggerFactory.getLogger(MyFilter.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HandlerMethod m=(HandlerMethod)handler;
logger.info("preHandle:"+ m.getBeanType().toString());
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
logger.info("postHandle");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
logger.info("afterCompletion");
}
}
攔截起配置
package com.soft.m;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/*");
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//registry.addResourceHandler("/*.ico");
}
}
springboot 啟動(dòng)的類
package com.soft.m;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MApplication {
public static void main(String[] args) {
SpringApplication.run(MApplication.class, args);
}
}
controler 類例子
package com.soft.m;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "HelloController中的hello方法";
}
@RequestMapping("/create")
public String create(HttpServletRequest request){
request.getSession().setAttribute("aaa", "234");
return "HelloController中的create方法";
}
@RequestMapping("/destory")
public String destory(HttpServletRequest request){
request.getSession().invalidate();
return "HelloController中的destory方法";
}
}
看完上述內(nèi)容,你們掌握springboot過濾器和攔截器以及監(jiān)聽器的代碼怎么寫的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。