问题
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