springboot:注册Servlet三大组件

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。

注册三大组件用以下方式

 1、注册自定义的servlet

  • 编写自定义的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().write("hello,springBoot!");
    }
}
  • 编写javaConfig配置类,返回ServletRegistrationBean
@Configuration
public class ServletConfig {
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(), "/servlet");
        registrationBean.setLoadOnStartup(1);//设置启动级别
        return registrationBean;
    }
}
  • 测试结果

SpringBoot帮我们自动SpringMVC的时候,自动的注册SpringMVC的前端控制器;DIspatcherServlet;

DispatcherServletAutoConfiguration:

        @Bean(
            name = {"dispatcherServletRegistration"}
        )
        @ConditionalOnBean(
            value = {DispatcherServlet.class},
            name = {"dispatcherServlet"}
        )
        public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet, WebMvcProperties webMvcProperties, ObjectProvider multipartConfig) {
            DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, webMvcProperties.getServlet().getPath());
            registration.setName("dispatcherServlet");
            registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
            multipartConfig.ifAvailable(registration::setMultipartConfig);
            return registration;
        }

DispatcherServletRegistrationBean是继承于ServletRegistrationBean, 通过DispatcherServletAutoConfiguration自动配置类注册了DispatcherServlet。

 2、注册自定义的Filter

  • 编写自定义的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.println("filter在过滤请求");
        //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
  • 编写javaConfig配置类,返回FilterRegistrationBean
.............................................................
    @Bean
    public FilterRegistrationBean myFilter(){
        //设置过滤的Servlet请求
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter(), myServlet());
        //这个也可以设置过滤的请求
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello"));
        return filterRegistrationBean;
    }
..............................................................

3、注册自定义的Listener

  •  编写自定义的MyListener
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("初始化");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("销毁");
    }
}
  • 编写javaConfig配置类,返回ServletListenerRegistrationBean
   @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new MyListener());
        return bean;
    }

4、使用Servlet API注解注入

  • 使用方法
  1. @ServletComponentScan(basePackages = "com.atguigu.admin") :指定原生Servlet组件都放在那里
  2. @WebServlet(urlPatterns = "/my"):效果:直接响应,不会经过拦截器,原因:多个Servlet都能处理到同一层路径时,会选择路径更精确的Servlet进行处理,所以不会经过DispatcherServlet的处理流程,因此不受拦截器的影响
  3. @WebFilter(urlPatterns={"/css/*","/images/*"})
  4. @WebListener
  • 使用步骤,如下面编写一个servlet,其它类似

编写servlet

@WebServlet(urlPatterns = {"/servlet"})
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("hello,servlet!!!");
    }

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

主配置类上添加@ServletComponentScan注解,并指定编写的servlet的所在包,使其能够扫描到

@SpringBootApplication
@ServletComponentScan(basePackages = "com.wust.springboot.servlet")
public class SpringbootApplication {

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

    }

}

 

 

你可能感兴趣的:(SpringBoot)