spring-boot 如何为特定的 url 提供服务?
根据我之前的经验:
- 当使用纯
servlet
时,我们定义了 servlet,以便它服务于匹配特定 url 的请求. - 在使用
struts2
时,我们定义了一个过滤器,以便它可以为匹配特定 url 的请求提供服务. - 在传统的 xml 配置样式中使用
springMVC
时,我们定义了一个调度程序 servlet,以便它可以为匹配特定 url 的请求提供服务.
- 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.
但是使用 spring-boot
:
似乎没有明确定义 servlet 或过滤器.但它仍然可以提供特定的网址.
Seems no servlet or filter is defined explicitly. But it still could serve specific urls.
问题是:
- 还在使用servlet吗?如果是,它如何在不明确定义 servlet 或过滤器的情况下提供 url?
其他相关问题 (基于评论提示):
-
SpringBootServletInitializer
的实现似乎会在部署时调用,但谁来调用它?
- It seems the implementation of
SpringBootServletInitializer
will be invoked on deploy, but who is going to invoke it?
如您所见 此处 的详细信息,在启动,在初始化嵌入式服务器(默认为 Tomcat)时,Spring Boot 创建和注册 DispatcherServlet
作为 servlet.
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 然后像往常一样扫描您自己的类(包括您从中调用 SpringApplication.run()
的类)并为您的控制器设置相应的映射,如果您有任何.例如 /hello
的映射如下:
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);
}
}