How does spring-boot able to serve specific url?

一世执手 提交于 2019-12-08 02:05:53

问题


In my previous experience:

  • When using pure servlet, we define servlets so that it will serve requests that match specific urls.
  • When using struts2, we define a filter so that it will serve requests that match specific urls.
  • When using springMVC in a traditional xml configuration style, we define a dispatcher servlet so that it will serve requests that match specific urls.

But with spring-boot:

Seems no servlet or filter is defined explicitly. But it still could serve specific urls.

The questions is:

  • Is it still using servlet? If yes, how it get to serve urls without defining servlet or filter explicitly?

Additional related questions (base on tips from comments):

  • It seems the implementation of SpringBootServletInitializer will be invoked on deploy, but who is going to invoke it?

回答1:


As you can see here in details, on startup, while initializing an embedded server (Tomcat by default), Spring Boot creates and registers DispatcherServlet as a servlet.

Spring then, as usual, scans your own classes (including the one you invoke SpringApplication.run() from) and sets corresponding mapping for your controllers, if you have any. For example mapping for /hello here:

@RestController
@EnableAutoConfiguration
public class TestSpring {

    @RequestMapping("/hello")
    String hello() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TestSpring.class, args);
    }

}


来源:https://stackoverflow.com/questions/39192943/how-does-spring-boot-able-to-serve-specific-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!