SpringBoot——注册Servlet三大组件

之前我们是在web.xml中注册这三大组件,由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器,以此来启动SpringBoot的web应用,并没有web.xml文件,所以在SpringBoot中我们需要使用ServletRegistrationBean 、FilterRegistrationBean和ServletListenerRegistrationBean来分别注册Servlet、Filter和Listener

一、注册Servlet

1、编写一个Servlet:

public class MyServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print("MyServlet");
    }
}

2、使用ServletRegistrationBean注册Servlet:注册时写明具体Servelt和该Servlet的拦截路径,注意一定要在配置类中将返回的ServletRegistrationBean放在容器中

@Configuration
public class MyServerConfig {

    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        return servletRegistrationBean;
    }
}

访问结果:

SpringBoot——注册Servlet三大组件

SpringBoot为我们提供的SpringMvc的DispatcherServlet的注册也是通过此方式注册的,在DispatcherServletAutoConfiguration中有一段注册代码:拦截的请求是/(包含静态资源但不包含jsp请求,/*则会拦截所有请求包含jsp请求)

@Bean(name = {"dispatcherServletRegistration"})
@ConditionalOnBean(value = {DispatcherServlet.class},name = {"dispatcherServlet"})
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
	DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.serverProperties.getServlet().getPath());
	registration.setName("dispatcherServlet");
	registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
	if (this.multipartConfig != null) {
		registration.setMultipartConfig(this.multipartConfig);
	}
	return registration;
}

二、注册Filter

1、编写Filter

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

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

    @Override
    public void destroy() {

    }
}

2、注册Filter

@Configuration
public class MyServerConfig {

    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return filterRegistrationBean;
    }
}

注册Filter时可以指定拦截哪些请求,也可以指定拦截哪些Servlet,可以通过构造器直接设置,也可以跟案例一样分步设置

三、注册Listener

1、编写Listner:此处监听的是ServletContext

public class MyListner implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.print("contextInitialized...项目启动");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.print("contextInitialized...项目销毁");
    }
}

2、注册Listener

@Configuration
public class MyServerConfig {

    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean<MyListner>(new MyListner());
        return servletListenerRegistrationBean;
    }
}

其他的一些设置,比如启动顺序等都可以通过相应的XxxRegistrationBean对象进行设置

 

;