SpringBoot之Servlet、Filter、Listener配置

1.介紹

經過以前的文章來看,SpringBoot涵蓋了不少配置,可是每每一些配置是採用原生的Servlet進行的,可是在SpringBoot中不須要配置web.xml的
由於有可能打包以後是一個jar包的形式,這種狀況下如何解決?SpringBoot 提供了兩種方案進行解決web

2.快速開始

2.1 方案一

方案一採用原生Servlet3.0的註解進行配置、@WebServlet 、@WebListener、@WebFilter是Servlet3.0 api中提供的註解
經過註解能夠徹底代替web.xml中的配置,下面是一個簡單的配置json

IndexServlet

@WebServlet(name = "IndexServlet",urlPatterns = "/hello")
    public class IndexServlet extends HttpServlet {
            @Override
            public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                    resp.getWriter().print("hello word");
                    resp.getWriter().flush();
                    resp.getWriter().close();
            }

            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                    this.doGet(req, resp);
            }
    }

IndexListener

@WebListener
    public class IndexListener implements ServletContextListener {
            private Log log = LogFactory.getLog(IndexListener.class);

            @Override
            public void contextInitialized(ServletContextEvent servletContextEvent) {
                    log.info("IndexListener contextInitialized");
            }

            @Override
            public void contextDestroyed(ServletContextEvent servletContextEvent) {

            }
    }

IndexFilter

@WebFilter(urlPatterns = "/*", filterName = "indexFilter")
    public class IndexFilter implements Filter {
        Log log = LogFactory.getLog(IndexFilter.class);

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("init IndexFilter");
        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            log.info("doFilter IndexFilter");
            filterChain.doFilter(servletRequest,servletResponse);

        }

        @Override
        public void destroy() {

        }
    }

上面配置完了,須要配置一個核心的註解@ServletComponentScan,具體配置項以下,能夠配置掃描的路徑api

@Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Import(ServletComponentScanRegistrar.class)
    public @interface ServletComponentScan {

        @AliasFor("basePackages")
        String[] value() default {};

        @AliasFor("value")
        String[] basePackages() default {};

        Class<?>[] basePackageClasses() default {};

    }

把註解加到入口處啓動便可app

@SpringBootApplication
    @ServletComponentScan
    public class AppApplication {

        public static void main(String[] args) throws Exception {
            SpringApplication.run(AppApplication.class, args);
        }

    }

2.2 方案二

方案二是採用本身SpringBoot 配置bean的方式進行配置的,SpringBoot提供了三種BeanFilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBean
分別對應配置原生的Filter、Servlet、Listener,下面提供的三個配置和方案一採用的方式可以達到統一的效果ide

@Bean
    public ServletRegistrationBean indexServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new IndexServlet());
        registration.addUrlMappings("/hello");
        return registration;
    }

    @Bean
    public FilterRegistrationBean indexFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new IndexFilter());
        registration.addUrlPatterns("/");
        return registration;
    }
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new IndexListener());
        return servletListenerRegistrationBean;
    }

3.總結

兩種方案在使用上有差異,可是在內部SpringBoot的實現上是無差異的,即便使用的是Servlet3.0註解,也是經過掃描註解
轉換成這三種bean的FilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBeanthis

4.擴展

你們在使用的時候有沒有發覺,其實SpringBoot在使用SpringMvc的時候不須要配置DispatcherServlet的,由於已經自動配置了,
可是若是想要加一些初始配置參數如何解決,方案以下:url

@Bean
    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
        registration.addUrlMappings("*.do");
        registration.addUrlMappings("*.json");
        return registration;
    }

能夠經過注入DispatcherServlet 而後用ServletRegistrationBean包裹一層 動態的加上一些初始參數spa

出處:https://blog.csdn.net/king_is_everyone/article/details/53116744.net

相關文章
相關標籤/搜索