1. 程式人生 > >SpringBoot學習記錄(五)——Servlet、Filter、Listener配置

SpringBoot學習記錄(五)——Servlet、Filter、Listener配置

SpringBoot中沒有了web.xml檔案,但是有時候需要使用Servlet,Listener,Filter,則SpringBoot中有2種方式:

         1、Servlet3中的註解@WebServlet 、@WebListener、@WebFilter來配置

         2、SpringBoot配置bean的方式進行配置

第一種方式:

使用Servlet3中的註解@WebServlet 、@WebListener、@WebFilter來配置,下面進行配置:

TestServlet

/**
 * @author Tang Haorong
 * @create 2018-12-22 21:36
 * @name
 */
@WebServlet(name = "TestServlet",urlPatterns = "/hello")
public class TestServlet extends HttpServlet {

    @Override
    protected 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);
    }
}

TestListener

/**
 * @author Tang Haorong
 * @create 2018-12-22 21:41
 * @name
 */
@WebListener
public class TestListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("TestListener contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

TestFilter

/**
 * @author Tang Haorong
 * @create 2018-12-22 21:41
 * @name
 */
@WebFilter(urlPatterns = "/*",filterName = "TestFilter")
public class TestFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init TestFilter");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("doFilter TestFilter");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

然後還要在SpringBoot的啟動類上面新增一個重要的註解@ServletComponentScan,這樣才能掃描到對應的servlet。

@SpringBootApplication
@ServletComponentScan
public class SpringBootServletApplication {

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

}

第二種方式:

SpringBoot配置bean的方式進行配置,SpringBoot提供了三種ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean,分別對應配置原生的Filter、Servlet、Listener,下面配置和能夠達到一樣的效果。

/**
 * @author Tang Haorong
 * @create 2018-12-22 21:57
 * @name
 */
/**
 * @configuration:指明當前類是一個配置類,可以表示為一個spring的xml配置檔案
 */
@Configuration
public class ServletConfig {

    /**
     * @Bena可以表示如下
     * 將發放的返回值新增到容器中;容器中的這個元件預設id就是方法名
     * <bean id="testServletRegistration" class="org.springframework.boot.web.servlet.testServletRegistration">
     * </bean>
     * @return
     */
    @Bean
    public ServletRegistrationBean testServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new TestServlet());
        registration.addUrlMappings("/hello");
        return registration;
    }

    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new TestFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }
    @Bean
    public ServletListenerRegistrationBean testServletListenerRegistrationBean(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new TestListener());
        return servletListenerRegistrationBean;
    }
}