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
相关推荐
oak隔壁找我2 小时前
JVM常用调优参数
java·后端
蝎子莱莱爱打怪6 小时前
OpenClaw 从零配置指南:接入飞书 + 常用命令 + 原理图解
java·后端·ai编程
狼爷7 小时前
Go 没有 override?别硬套继承!用接口+嵌入,写更清爽的“覆盖”逻辑
java·go
用户8307196840829 小时前
Spring Boot 集成 RabbitMQ :8 个最佳实践,杜绝消息丢失与队列阻塞
spring boot·后端·rabbitmq
小兔崽子去哪了10 小时前
Java 自动化部署
java·后端
ma_king10 小时前
入门 java 和 数据库
java·数据库·后端
后端AI实验室10 小时前
我用Cursor开发了3个月,整理出这套提效4倍的工作流
java·ai
Java水解10 小时前
Spring Boot 视图层与模板引擎
spring boot·后端
Java水解10 小时前
一文搞懂 Spring Boot 默认数据库连接池 HikariCP
spring boot·后端
码路飞14 小时前
GPT-5.3 Instant 终于学会好好说话了,顺手对比了下同天发布的 Gemini 3.1 Flash-Lite
java·javascript