过滤器
过滤器是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码、做一些业务逻辑判断等。继承自Filter,需要实现init(),doFilter()和destroy()三个方法,其中init和destroy只会在容器启动和结束的时候才调用一次,所以主要逻辑卸载doFilter中
step1:新建过滤器类
tip:也可以用@WebFilter注解
@Slf4j
@Component //别忘了装载
public class TestFilter2 implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse)servletResponse;
//这里可以操作session,cookie,过滤文件上传请求...可以自行查阅
filterChain.doFilter(servletRequest, servletResponse);//别忘了释放请求
}
@Override
public void destroy() {
}
}
step2:注册过滤器
需要在配置类中注入过滤器
@Bean
public FilterRegistrationBean filterRegistrationBeanA(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new TestFilter());
filterRegistrationBean.addUrlPatterns("/blogs"); //url前不要忘记加"/",否则会报错
filterRegistrationBean.setName("sessionFilterA");
filterRegistrationBean.setOrder(10);
return filterRegistrationBean;
}
setFilter()设置过滤器
addUrlPatterns()添加过滤url
setName()设置拦过滤名字,同一容器下如果过滤器重名会报错
setOrder()设置过滤器排序,小的在前面。
拦截器
step1:新建拦截器类
@Slf4j
public class MyInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//在请求处理之前进行调用(Controller方法调用之前)
return true; //只有返回true才放行,否则拦截请求
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
log.info("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
log.info("afterCompletion");
}
}
step2:创建一个配置类实现WebMvcConfigurer接口,并重写 addInterceptors 方法
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
//增加拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//实例化我们自定义的拦截器,然后将对像手动添加到拦截器链中
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/blogs");
registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/blogs2");
}
}
监听器
spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利。
//支持的事件类型四种
ApplicationStartedEvent //spring boot启动开始时执行的事件
ApplicationEnvironmentPreparedEvent //spring boot 对应Enviroment已经准备完毕,但此时上下文context还没有创建。
ApplicationPreparedEvent //spring boot上下文context创建完成,但此时spring中的bean是没有完全加载完成的。
ApplicationFailedEvent //spring boot启动异常时执行事件
这里以监听ApplicationStartedEvent事件为例
**step1:监听类实现ApplicationListener接口 **
public class MyApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
private Logger logger = LoggerFactory.getLogger(MyApplicationStartedEventListener.class);
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
SpringApplication app = event.getSpringApplication();
app.setShowBanner(false);// 不显示banner信息
logger.info("==MyApplicationStartedEventListener==");
}
}
在该事件中可以获取到SpringApplication对象,可做一些执行前的设置.
step2:将监听类添加到主类实例
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.addListeners(new MyApplicationStartedEventListener());
app.run(args);
}
}
网友评论