springboot使用原生组件注入,源码分析

在springboot项目中创建一个自定义的Servlet 继承HttpServlet

加入这个@WebServlet注解,访问的就是my这个路径

注意my前面一定要加/,否则启动报错

@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("123456");
    }
}

http://localhost:8080/my

我们访问my 发现404

还需要在启动类加入@ServletComponentScan注解,扫描那个包

这次访问就有了

创建一个过滤器,使用@WebFilter注解,拦截所有请求

在项目启动的时候,调用初始化

@Slf4j
@WebFilter(urlPatterns = {"/*"})
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销毁");
    }
}

当我们访问http://localhost:8080/my的时候,进行过滤器处理

创建监听器,使用@WebListener注解


@Slf4j
@WebListener
public class MyServletContextListener implements ServletContextListener {


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

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

我们还有另一种注入的方式,先把那三个webxxx注解去掉

使用RegistrationBean的方式注入

@Configuration
public class TestConfig {


    //注入servlet
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        MyServlet myServlet=new MyServlet();
        return new ServletRegistrationBean(myServlet,"/my");
    }

    //注入过滤器
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        MyFilter myFilter=new MyFilter();
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
        filterRegistrationBean.setFilter(myFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return filterRegistrationBean;
    }

    //注入监听器
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
        ServletListenerRegistrationBean servletListenerRegistrationBean=new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new MyServletContextListener());
        return servletListenerRegistrationBean;
    }

}

我们进入DispatcherServletAutoConfiguration类

可以看到在这里注入的DispatcherServlet,和webmvc的属性进行了绑定

进入WebMvcProperties类,看到prefix = "spring.mvc"

我们在Yml文件对springmvc的一些属性的配置,都是来自这个类

我们在看下DispatcherServletRegistrationConfiguration类

注册DispatcherServlet配置

进入webMvcProperties.getServlet().getPath()

这个path就是servlet的路径,默认是/,我们可以在yml文件去修改

spring:
  mvc:
    servlet:
      path: /res

浏览器输入http://localhost:8080/res/

注意res后面还有个/,进入index.html界面

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值