美文网首页
Spring boot 中使用 @WebListener @We

Spring boot 中使用 @WebListener @We

作者: 黑曼巴yk | 来源:发表于2020-07-25 16:20 被阅读0次

前言

一般用户通过浏览器发起Request请求进入服务器后处理处理顺序如下:

Client > Listener > ServletContainer > Filter > Servlet > Interceptor

注册 Listener/Filter等

Configuration/Bean 注册

ServletContextConfig

@Configuration
public class ServletContextConfig {
    @Bean
    public ServletListenerRegistrationBean registrationBean() {
        ServletListenerRegistrationBean registrationBean = new ServletListenerRegistrationBean();
        registrationBean.setListener(new MyServletContextListener());
        return registrationBean;
    }
}

MyServletContextListener

public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext application = sce.getServletContext();
        String userName = application.getInitParameter("userName");
        System.out.println("启动web应用的用户名字为:" + userName);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext application = sce.getServletContext();
        String userName = application.getInitParameter("userName");
        System.out.println("关闭web应用的用户名字为:" + userName);
    }
}

通过注解的形式注册

CharacterEncodingFilter

@WebFilter(filterName = "characterFilter", urlPatterns = "/*")
public class CharacterEncodingFilter extends OncePerRequestFilter {
    private static final boolean responseSetCharacterEncodingAvailable = ClassUtils.hasMethod(
            HttpServletResponse.class, "setCharacterEncoding", new Class[]{String.class});

    // 需要设置的编码方式,为了支持可配置,Spring把编码方式设置成了一个变量
    private String encoding;
    // 是否强制使用统一编码,也是为了支持可配置
    private boolean forceEncoding;

    // 构造器,在这里,Spring把forceEncoding的值默认设置成了false
    public CharacterEncodingFilter() {
        this.forceEncoding = false;
    }

    // encoding/forceEncoding的setter方法
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public void setForceEncoding(boolean forceEncoding) {
        this.forceEncoding = forceEncoding;
    }

    // Spring通过GenericFilterBean抽象类,对Filter接口进行了整合,
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        System.out.println("----filter encoding----");
        if ((this.encoding != null) && (((this.forceEncoding) || (request.getCharacterEncoding() == null)))) {
            request.setCharacterEncoding(this.encoding);
            if ((this.forceEncoding) && (responseSetCharacterEncodingAvailable)) {
                response.setCharacterEncoding(this.encoding);
            }
        }
        filterChain.doFilter(request, response);
    }
}

MianApplication

@SpringBootApplication
@ServletComponentScan
public class MianApplication {
    public static void main(String[] args) {
        SpringApplication.run(MpApplication.class, args);
    }

相关文章

网友评论

      本文标题:Spring boot 中使用 @WebListener @We

      本文链接:https://www.haomeiwen.com/subject/mocmlktx.html