Spring Boot中的DispatcherServlet和web.xml
- java
- Spring Boot
1.概述
DispatcherServlet
是Spring Web應用程序中的前端控制器。它用於在Spring MVC中創建Web應用程序和REST服務。在傳統的Spring Web應用程序中,此servlet是在web.xml
文件中定義的。
在本教程中,我們將在Spring Boot應用程序web.xml
文件DispatcherServlet
另外,我們還將把來自web.xml
Filter
, Servlet
和Listener
類映射到Spring Boot應用程序。
2. Maven依賴
首先,我們必須將spring-boot-starter-web
Maven依賴項添加到我們的pom.xml
文件中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3. DispatcherServlet
DispatcherServlet接收所有HTTP請求並將它們委託給控制器類。
**在Servlet 3.x規範之前, DispatcherServlet
將在Spring MVC應用程序web.xml
**從Servlet 3.x規範開始,我們可以使用ServletContainerInitializer
編程方式註冊servlet。
讓我們在web.xml文件中查看DispatcherServlet示例配置:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Spring Boot提供了spring-boot-starter-web
庫,用於使用Spring MVC開發Web應用程序。 Spring Boot的主要功能之一是自動配置。 Spring Boot自動配置會自動註冊並配置DispatcherServlet
。因此,我們不需要手動DispatcherServlet
默認情況下, spring-boot-starter-web
starter將DispatcherServlet
配置為URL模式“ /”。因此,我們不需要在web.xml文件中為上述DispatcherServlet示例完成任何其他配置。但是,我們可以使用server.servlet.
*在application.properties
文件中:
server.servlet.context-path=/demo
spring.mvc.servlet.path=/baeldung
通過這些定制,將DispatcherServlet
配置為處理URL模式/baeldung
,並且根contextPath
將為/demo
。因此, DispatcherServlet
監聽http://localhost:8080/demo/baeldung/.
4.應用程序配置
Spring MVC Web應用程序將web.xml
文件用作部署描述符文件。同樣,它在URL.path和web.xml
文件中的servlet之間定義了映射。
Spring Boot不再是這種情況。如果需要特殊的過濾器,可以在Java類配置中註冊它。 web.xml
文件包括過濾器,servlet和偵聽器。
當我們想從傳統的Spring MVC遷移到現代的Spring Boot應用程序時,如何將web.xml
移植到新的Spring Boot應用程序?在Spring Boot應用程序中,我們可以通過幾種方式添加這些概念。
4.1。註冊Filter
讓我們通過實現Filter
接口來創建一個過濾器:
@Component
public class CustomFilter implements Filter {
Logger logger = LoggerFactory.getLogger(CustomFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
logger.info("CustomFilter is invoked");
chain.doFilter(request, response);
}
// other methods
}
如果沒有Spring Boot,我們將配置CustomFilter
在web.xml文件中:
<filter>
<filter-name>customFilter</filter-name>
<filter-class>CustomFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>customFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
為了使Spring Boot能夠識別過濾器,我們只需要使用@Component
註釋將其定義為bean。
4.2。註冊一個Servlet
讓我們通過擴展HttpServlet
類來定義一個servlet:
public class CustomServlet extends HttpServlet {
Logger logger = LoggerFactory.getLogger(CustomServlet.class);
@Override
protected void doGet(
HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
logger.info("CustomServlet doGet() method is invoked");
super.doGet(req, resp);
}
@Override
protected void doPost(
HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
logger.info("CustomServlet doPost() method is invoked");
super.doPost(req, resp);
}
}
如果沒有Spring Boot,我們將在web.xml
文件中CustomServlet
<servlet>
<servlet-name>customServlet</servlet-name>
<servlet-class>CustomServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>customServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
在Spring Boot應用程序中,該servlet被註冊為Spring @Bean
或通過@WebServlet
註釋的類。
使用Spring @Bean
方法,我們可以使用ServletRegistrationBean
類來註冊servlet。
因此,我們將ServletRegistrationBean
類CustomServlet
定義為一個bean:
@Bean
public ServletRegistrationBean customServletBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new CustomServlet(), "/servlet");
return bean;
}
4.3。註冊Listener
讓我們通過擴展ServletContextListener
類來定義一個偵聽器:
public class CustomListener implements ServletContextListener {
Logger logger = LoggerFactory.getLogger(CustomListener.class);
@Override
public void contextInitialized(ServletContextEvent sce) {
logger.info("CustomListener is initialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
logger.info("CustomListener is destroyed");
}
}
如果沒有Spring Boot,我們將在web.xml
文件中CustomListener
<listener>
<listener-class>CustomListener</listener-class>
</listener>
要在Spring Boot應用程序中定義偵聽器,我們可以使用*@Bean或@WebListener*批註。
使用Spring @Bean
方法,我們可以使用ServletListenerRegistrationBean
類註冊Listener
。
因此,讓我們ServletListenerRegistrationBean
CustomListener
定義為一個bean。 班級:
@Bean
public ServletListenerRegistrationBean<ServletContextListener> customListenerBean() {
ServletListenerRegistrationBean<ServletContextListener> bean = new ServletListenerRegistrationBean();
bean.setListener(new CustomListener());
return bean;
}
啟動我們的應用程序後,我們可以檢查日誌輸出以查看是否已成功初始化偵聽器的確認:
2020-09-28 08:50:30.872 INFO 19612 --- [main] c.baeldung.demo.listener.CustomListener: CustomListener is initialized
5.結論
在本快速教程中,我們了解瞭如何在Spring Boot應用程序中DispatcherServlet
和web.xml
元素,包括filter
, servlet
和listener
。