SpringBoot中的三大组件的简单应用

三大组件的顶级父类为RegistrationBean
SpringBoot中的三大组件的简单应用_第1张图片
SpringBoot中的三大组件的简单应用_第2张图片

ServletRegistrationBean

  1. 自定义一个Servlet类实现HttpServlet抽象类,重写service方法(有兴趣的可以深追一下Servlet接口、GenericServlet抽象类、HttpServlet抽象类service方法的变化)

SpringBoot中的三大组件的简单应用_第3张图片

@Component
public class MyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.write("hello");
        writer.flush();
    }
}
  1. 在配置类中使用@Bean注解获取ServletRegistrationBean对象注册进容器中
@Configuration
public class MyConfig_threeComponents {

    @Autowired
    private MyServlet servlet;
    @Bean
    public ServletRegistrationBean<MyServlet> registerMyServlet(){
        ServletRegistrationBean<MyServlet> register=new ServletRegistrationBean<>();
        register.setServlet(servlet);
        register.setUrlMappings(Arrays.asList("/myServlet"));
        return  register;
    }

}

测试:
SpringBoot中的三大组件的简单应用_第4张图片

FilterRegistrationBean(过滤器)

  1. 创建自定义过滤器实现Filter接口

@Component
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        String uri=request.getRequestURI();
        System.out.println("filter==uri="+uri);

        //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
  1. 在配置类中使用@Bean注解获取FilterRegistrationBean对象注册进容器中
@Configuration
public class MyConfig_threeComponents {
	@Autowired
    private MyFilter myFilter;
    @Bean
    public FilterRegistrationBean<MyFilter> registrationFilter(){
        FilterRegistrationBean<MyFilter> register = new FilterRegistrationBean<>();
        register.setFilter(myFilter);
        register.setUrlPatterns(Arrays.asList("/*"));//定义过滤路径
        return register;
    }
 }   

测试:
在这里插入图片描述

ServletListenerRegistrationBean(监听器)

  1. 创建自定义监听器实现ServletContextListener接口
    SpringBoot中的三大组件的简单应用_第5张图片

@Component
public class MyContextListen implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {

        System.out.println("MyContextListen--项目启动");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("MyContextListen--项目停止");
    }
}

  1. 在配置类中使用@Bean注解获取ServletListenerRegistrationBean对象注册进容器中
@Configuration
public class MyConfig_threeComponents {
    @Autowired
    private MyContextListen myContextListen;

    @Bean
    ServletListenerRegistrationBean<MyContextListen> registrationListen(){
        ServletListenerRegistrationBean<MyContextListen> register = new ServletListenerRegistrationBean<>();
        register.setListener(myContextListen);
        return register;
    }
 }   

测试:
SpringBoot中的三大组件的简单应用_第6张图片

你可能感兴趣的:(SpringBoot,spring,boot)