使用切片拦截方式

fifter

自定义bean

package com.imoke.web.Filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.FilterRegistration;
import java.beans.FeatureDescriptor;
import java.util.ArrayList;
import java.util.List;


@Configuration
public class WebFilterConfig {

    @Bean
    public FilterRegistrationBean timeFilter(){
        FilterRegistrationBean filterRegistration =  new FilterRegistrationBean();
        TimeFilter timeFilter = new TimeFilter();
        filterRegistration.setFilter(timeFilter);
        List<String> urls = new ArrayList<>();
        urls.add("/*");
        filterRegistration.setUrlPatterns(urls);
        return filterRegistration;
    }
}

 

package com.imoke.web.Filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;


public class TimeFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("time filter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        LocalDateTime start =LocalDateTime.now();
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("end time is"+ Duration.between(start,LocalDateTime.now()));
    }

    @Override
    public void destroy() {
        System.out.println("time filter dectory");
    }
}

interctor

ascep切片aop(类,1声明切入点在哪些方法起作用,啥时候起作用,/2.起作用是执行的业务逻辑)

1.@Aspect 2@Component

3@before被调用以前 @after成功返回以后

@AfterThrowing抛出异常 
@AfterReturning

@Around(ec)

 

  1. @Before: 标识一个前置增强方法,相当于BeforeAdvice的功能.
  2. @After: final增强,不管是抛出异常或者正常退出都会执行.
  3. @AfterReturning:  后置增强,似于AfterReturningAdvice, 方法正常退出时执行.
  4. @AfterThrowing:  异常抛出增强,相当于ThrowsAdvice.
  5. @Around: 环绕增强,相当于MethodInterceptor.
  6. https://my.oschina.net/u/3434392/blog/1625493 比较详细的介绍

dispatcherServlet分发请求

// Actually invoke the handler.真正数据是在这里完成的所有在prehandler不知道参数是啥
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值