Filter:
只能拿到http请求的一些响应
import javax.servlet.Filter;
@Component
public class TimeFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("filter start");
chain.doFilter(request, response);
System.out.println("filter end");
}
}
第三方过滤器的引用方式
@Configuration
public class WebConfig {
@Bean
public FilterRegistrationBean<Filter> timeFilter() {
FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<Filter>();
TimeFilter timeFilter = new TimeFilter();
registrationBean.setFilter(timeFilter);
List<String> urls = new ArrayList<String>();
urls.add("/*");
registrationBean.setUrlPatterns(urls);
return registrationBean;
}
}
Interceptor
controller 以及方法,拿不到参数值
@Component
public class TimeInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println(((HandlerMethod)handler).getBean().getClass().getName());
System.out.println(((HandlerMethod)handler).getMethod().getName());
}
}
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private TimeInterceptor timeInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
registry.addInterceptor(timeInterceptor);
}
}
切片Aspect
https://docs.spring.io/spring/docs/5.2.2.RELEASE/spring-framework-reference/core.html#aop