91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot過濾器和攔截器以及監聽器的代碼怎么寫

發布時間:2021-10-20 15:39:36 來源:億速云 閱讀:139 作者:柒染 欄目:大數據

springboot過濾器和攔截器以及監聽器的代碼怎么寫,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

簡單的springboot 過濾器,攔截器,監聽器 使用

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>
   <!-- 導入Spirng Boot  web 所需的jar包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
  <!-- 這個插件,可以將應用打包成一個可執行的jar包;-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

過濾器,使用的servlet3 規范

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過濾器");
    }
}
監聽器 也是servlet 規范 

servletContextListener 監聽

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監聽器

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 啟動的類

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方法";
       }

}
 

看完上述內容,你們掌握springboot過濾器和攔截器以及監聽器的代碼怎么寫的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

大连市| 黄山市| 大城县| 南乐县| 通辽市| 景宁| 石河子市| 福建省| 柳林县| 巴青县| 宜宾县| 长春市| 惠水县| 瑞安市| 德格县| 娄烦县| 安徽省| 金阳县| 丁青县| 东辽县| 盐城市| 佛冈县| 台中市| 邳州市| 嘉荫县| 凤山县| 常德市| 沈丘县| 马山县| 潜江市| 安庆市| 鞍山市| 亚东县| 咸丰县| 满洲里市| 舟山市| 宜黄县| 长子县| 浪卡子县| 衡东县| 凉城县|