SpringBoot-Web开发之Web原生组件注入

使用Servlet API

  • Web三大原生组件:Servlet、Filter、Listener
  • 启动类上使用注解@ServletComponentScan(basePackages = "com.my.admin")
    • 使用注解的方式注入,需要开启扫描
  • @WebServlet()
    • 执行后会发现直接响应,没有经过Spring的拦截器
java 复制代码
//处理/my请求
@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
                throws ServletException, IOException {
        resp.getWriter().write("66666");
    }
}
  • @WebFilter
java 复制代码
@Slf4j
//拦截所有请求在servlet中是/*,spring中是/**
@WebFilter(urlPatterns={"/css/*","/images/*"}) 
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("MyFilter初始化完成");
    }

    @Override
    public void doFilter(ServletRequest request, 
                         ServletResponse response, 
                         FilterChain chain) throws IOException, ServletException {
        log.info("MyFilter工作");
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {
        log.info("MyFilter销毁");
    }
}
  • @WebListener
java 复制代码
@Slf4j
@WebListener
public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("MySwervletContextListener监听到项目初始化完成");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("MySwervletContextListener监听到项目销毁");
    }
}

RegistrationBean

  • 不使用注解注入,使用配置类
  • ServletRegistrationBean
  • FilterRegistrationBean
  • ServletListenerRegistrationBean
java 复制代码
@Configuration(proxyBeanMethods = true)
public class MyRegistConfig {

    @Bean
    public ServletRegistrationBean myServlet(){

        MyServlet myServlet = new MyServlet();
        return new ServletRegistrationBean(myServlet,"/my","/my02");
    }

    @Bean
    public FilterRegistrationBean myFilter(){

        MyFilter myFilter = new MyFilter();
		//myServlet()可以将servlet处理的请求进行过滤
        //return new FilterRegistrationBean(myFilter,myServlet());

        FilterRegistrationBean filterRegistrationBean = 
									new FilterRegistrationBean(myFilter);
		//自定义过滤的路径
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean myListener(){

        MySwervletContextListener mySwervletContextListener = 
									new MySwervletContextListener();
        return new ServletListenerRegistrationBean(mySwervletContextListener);
    }
}

DispatcherServlet注入原理

  • 容器中自动配置了 DispatcherServlet, 属性绑定到 WebMvcProperties;对应的配置文件配置项是 spring.mvc
  • 通过ServletRegistrationBean把 DispatcherServlet 配置进来。默认映射的是以/开头的路径
  • 多个Servlet都能处理到同一层路径,精确路径优先原则,故解释了上述自定义servlet直接响应,不经过spring
相关推荐
pshdhx_albert4 小时前
AI agent实现打字机效果
java·http·ai编程
沉鱼.444 小时前
第十二届题目
java·前端·算法
赫瑞5 小时前
数据结构中的排列组合 —— Java实现
java·开发语言·数据结构
周末也要写八哥6 小时前
多进程和多线程的特点和区别
java·开发语言·jvm
惜茶7 小时前
vue+SpringBoot(前后端交互)
java·vue.js·spring boot
杰克尼8 小时前
springCloud_day07(MQ高级)
java·spring·spring cloud
NHuan^_^9 小时前
SpringBoot3 整合 SpringAI 实现ai助手(记忆)
java·人工智能·spring boot
Mr_Xuhhh9 小时前
从ArrayList到LinkedList:理解链表,掌握Java集合的另一种选择
java·数据结构·链表
错把套路当深情10 小时前
Java 全方向开发技术栈指南
java·开发语言
han_hanker10 小时前
springboot 一个请求的顺序解释
java·spring boot·后端