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
相关推荐
麦麦鸡腿堡29 分钟前
Java绘图技术
java·开发语言
西西学代码3 小时前
Flutter---Stream
java·服务器·flutter
Blossom.1185 小时前
移动端部署噩梦终结者:动态稀疏视觉Transformer的量化实战
java·人工智能·python·深度学习·算法·机器学习·transformer
静若繁花_jingjing5 小时前
IDEA下载
java·ide·intellij-idea
代码丰6 小时前
函数式接口+default接口+springAi 中的ducumentReader去理解为什么存在default接口的形式
java
果汁华7 小时前
java学习连续打卡30天(1)
java
q***92517 小时前
Spring Cloud Data Flow 简介
后端·spring·spring cloud
武子康7 小时前
Java-171 Neo4j 备份与恢复 + 预热与执行计划实战
java·开发语言·数据库·性能优化·系统架构·nosql·neo4j
m0_639817158 小时前
基于springboot火锅店管理系统【带源码和文档】
java·spring boot·后端
会编程的林俊杰9 小时前
SpringBoot项目启动时的依赖处理
java·spring boot·后端