RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
springboot省去web.xml配置的方法是什么

这篇文章主要讲解了“springboot省去web.xml配置的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springboot省去web.xml配置的方法是什么”吧!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网页空间、营销软件、网站建设、安福网站维护、网站推广。

概述

最开始使用原生的springmvc时,总是免不了有如下xml配置


    spring
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:spring-servlet.xml
    
    1
    spring
    /*
  
   
 org.springframework.web.context.ContextLoaderListener   
  
    contextConfigLocation
    classpath:config/applicationContext.xml

但是,切换到springboot之后,web.xml之类的繁琐的配置基本上都不见了。出于好奇研究了下springboot究竟帮我们做了什么,我们可以免于这样的繁琐配置。

Servlet3.0规范

首先研究的第一点,为什么web.xml不见了。刚开始使用原生servlet(不使用web框架),web.xml就是非常重要的一个配置,无论是servlet、filter、listener都需要在web.xml里面配置下。

但是在servlet3.0里,这个配置得到了简化。可以通过java配置(注解等)省去web.xml配置。

具体servlet3.0的规范这里就不讨论了,说下其中一个非常重要的类。javax.servlet.ServletContainerInitializer

这个类会在web容器启动阶段被回调,可以在onStartup方法里做一些servlet、filter、listener的注册等操作。

/**
Interface which allows a library/runtime to be notified of a web application's startup phase and perform any required programmatic registration of servlets, filters, and listeners in response to it.
*/public interface ServletContainerInitializer {
    public void onStartup(Set> c, ServletContext ctx)
        throws ServletException; }

springboot的实现

首先spring在META-INF/services下配置了这个类,让整个web容器启动后可以找到并启动这个类

springboot省去web.xml配置的方法是什么

SpringServletContainerInitializer
/**
* @HandlesTypes这个注解标明了该ServletContainerInitializer需要在启动时候处理哪些类,
然后服务器会把找到的这些类传到onStartup的第一个参数里
注意这里的类包括所配置类的子类,比如这里配置WebApplicationInitializer,
启动之后,就会把这个WebApplicationInitializer的子类都传进去
*/@HandlesTypes(WebApplicationInitializer.class)public class SpringServletContainerInitializer implements ServletContainerInitializer {
        
    @Override
    public void onStartup(Set> webAppInitializerClasses, ServletContext servletContext)
            throws ServletException {
        List initializers = new LinkedList();
        //.... 省略容错的一些代码
        initializers.add((WebApplicationInitializer) waiClass.newInstance());
        //.... 
    AnnotationAwareOrderComparator.sort(initializers);
        for (WebApplicationInitializer initializer : initializers) {
            initializer.onStartup(servletContext);
        }
    }}

startup的逻辑很简单,web容器启动后,调用所有WebApplicationInitializer的onStartup方法。

WebApplicationInitializer 的实现SpringBootServletInitializer
@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {
   //....
    WebApplicationContext rootAppContext = createRootApplicationContext(
            servletContext);
   //...}
protected WebApplicationContext createRootApplicationContext(
        ServletContext servletContext) {
    //...
    return run(application);}

一般使用Springboot的时候,都会继承一个类SpringBootServletInitializer,在这个类的onStartup方法中,启动了整个Spring容器。

本地启动springboot时,我们一般会写一个类似于这样的main方法。

springboot省去web.xml配置的方法是什么

上述分析也解释了为啥把springboot应用部署到机器上,tomcat能够找到springboot的入口,并启动它。

DispatcherServlet的配置

关于springboot如何加载类并启动的这里就不介绍了。
这里说明下究竟Springboot如何配置DispatcherServlet的

springboot省去web.xml配置的方法是什么

1)当类路径下存在DispatcherServlet时候,该配置生效。
2)这个配置会在DispatcherServletAutoConfiguration配置完之后再配置。

DispatcherServletAutoConfiguration配置

springboot省去web.xml配置的方法是什么

springboot省去web.xml配置的方法是什么

看到这里就是我们非常熟悉的springboot的使用了。springboot在DispatcherServletConfiguration这个类里对DispatcherServlet进行了配置以及注册。

感谢各位的阅读,以上就是“springboot省去web.xml配置的方法是什么”的内容了,经过本文的学习后,相信大家对springboot省去web.xml配置的方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


文章题目:springboot省去web.xml配置的方法是什么
网页URL:http://sczitong.cn/article/psshch.html