SpringBoot(11) -- Web开发-- Web原生组件注入

SpringBoot2学习笔记

源码地址

四、Web开发

4.10)Web原生组件注入(Servlet、Filter、Listener)

4.10.1)使用Servlet API

4.10.1.1)原生Servlet组件

在工程SpringBootDemo4Admin中,新建MyServlet.java ,代码如下:

// 指定原生Servlet组件访问路径
@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("66666");
    }
}

4.10.1.2)原生Filter组件

新建MyFilter.java ,代码如下:

@Slf4j
// 指定原生Filter拦截路径
@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销毁");
    }
}

4.10.1.2)原生Listener组件

新建MySwervletContextListener.java ,代码如下:

@Slf4j
// 指定原生Listener监听器组件
@WebListener
public class MySwervletContextListener implements ServletContextListener {
​
​
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("MySwervletContextListener监听到项目初始化完成");
    }
​
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("MySwervletContextListener监听到项目销毁");
    }
}

测试:启动工程,控制台日志如下:

2022-06-11 15:35:52.436  INFO 4064 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1296 ms
2022-06-11 15:35:52.501  INFO 4064 --- [  restartedMain] c.s.a.servlet.MySwervletContextListener  : MySwervletContextListener监听到项目初始化完成
2022-06-11 15:35:52.504  INFO 4064 --- [  restartedMain] com.study.admin.servlet.MyFilter         : MyFilter初始化完成
2022-06-11 15:35:52.878  INFO 4064 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2022-06-11 15:35:52.929  INFO 4064 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-06-11 15:35:52.942  INFO 4064 --- [  restartedMain] c.s.a.SpringBootDemo4AdminApplication    : Started SpringBootDemo4AdminApplication in 2.59 seconds (JVM running for 4.15)

访问地址:http://localhost:8080/my ,页面如下:

停止工程,控制台日志如下:

2022-06-11 15:41:02.088  INFO 4064 --- [ionShutdownHook] com.study.admin.servlet.MyFilter         : MyFilter销毁
2022-06-11 15:41:02.088  INFO 4064 --- [ionShutdownHook] c.s.a.servlet.MySwervletContextListener  : MySwervletContextListener监听到项目销毁

4.10.2)DispatchServlet 如何注册到容器

  • 容器中自动配置了 DispatcherServlet 属性绑定到 WebMvcProperties;对应的配置文件配置项是 spring.mvc;

  • 通过 ServletRegistrationBean<DispatcherServlet> 把 DispatcherServlet 配置进来;

  • 默认映射的是 / 路径,可以在配置文件中修改,如下:

    # 修改servlet默认映射路径
    spring.mvc.servlet.path=/mvc/

 

4.10.3)使用RegistrationBean

删除上述MyServlet.java 、MyFilter.java 、MySwervletContextListener.java中的相关注解,新建 MyRegistConfig.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();
//        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);
    }
}

测试:启动工程,控制台日志如下:

2022-06-11 15:46:35.405  INFO 16200 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1437 ms
2022-06-11 15:46:35.468  INFO 16200 --- [  restartedMain] c.s.a.servlet.MySwervletContextListener  : MySwervletContextListener监听到项目初始化完成
2022-06-11 15:46:35.472  INFO 16200 --- [  restartedMain] com.study.admin.servlet.MyFilter         : MyFilter初始化完成
2022-06-11 15:46:36.130  INFO 16200 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2022-06-11 15:46:36.196  INFO 16200 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-06-11 15:46:36.209  INFO 16200 --- [  restartedMain] c.s.a.SpringBootDemo4AdminApplication    : Started SpringBootDemo4AdminApplication in 3.093 seconds (JVM running for 4.805)

访问地址:http://localhost:8080/my ,控制台日志如下:

2022-06-11 15:46:50.028  INFO 16200 --- [nio-8080-exec-1] com.study.admin.servlet.MyFilter         : MyFilter工作方法

停止工程,控制台日志如下:

2022-06-11 15:47:56.407  INFO 16200 --- [ionShutdownHook] com.study.admin.servlet.MyFilter         : MyFilter销毁
2022-06-11 15:47:56.408  INFO 16200 --- [ionShutdownHook] c.s.a.servlet.MySwervletContextListener  : MySwervletContextListener监听到项目销毁

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值