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)
- @Before: 标识一个前置增强方法,相当于BeforeAdvice的功能.
- @After: final增强,不管是抛出异常或者正常退出都会执行.
- @AfterReturning: 后置增强,似于AfterReturningAdvice, 方法正常退出时执行.
- @AfterThrowing: 异常抛出增强,相当于ThrowsAdvice.
- @Around: 环绕增强,相当于MethodInterceptor.
- https://my.oschina.net/u/3434392/blog/1625493 比较详细的介绍
dispatcherServlet分发请求
// Actually invoke the handler.真正数据是在这里完成的所有在prehandler不知道参数是啥
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());