首页 > 解决方案 > Spring Boot 2.4.5 和 Java 11 的模块系统问题

问题描述

在上面的示例中,我在类路径中有 spring.boot.starter.web 并且应用程序无法启动。如果我添加 org.apache.tomcat.embed.core 则应用程序启动成功。

当我以这种方式配置 module-info.java

requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.web;
requires spring.boot.starter.web;

2021-05-20 10:56:09.501  INFO 17112 --- [           main] com.example.police.PoliceApplication     : Starting PoliceApplication using Java 11 on zhaozhiguang-pc with PID 17112 (D:\item\police\target\classes started by zhaozhiguang in D:\item\police)
2021-05-20 10:56:09.501  INFO 17112 --- [           main] com.example.police.PoliceApplication     : No active profile set, falling back to default profiles: default
2021-05-20 10:56:10.565  INFO 17112 --- [           main] com.example.police.PoliceApplication     : Started PoliceApplication in 1.435 seconds (JVM running for 3.153)

Process finished with exit code 0

我的猜测是Tomcat没有启动

或者

requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.web;
requires org.apache.tomcat.embed.core;

这个有效

2021-05-20 10:57:48.097  INFO 13740 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-05-20 10:57:48.334  INFO 13740 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-05-20 10:57:48.381  INFO 13740 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-20 10:57:48.396  INFO 13740 --- [           main] com.example.police.PoliceApplication     : Started PoliceApplication in 2.001 seconds (JVM running for 3.202)
2021-05-20 10:57:48.846  INFO 13740 --- [)-192.168.1.107] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-05-20 10:57:48.846  INFO 13740 --- [)-192.168.1.107] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-05-20 10:57:48.847  INFO 13740 --- [)-192.168.1.107] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

为什么?

spring.boot.starter.web 依赖于 org.apache.tomcat.embed.core ?

标签: javaspringspring-boot

解决方案


Spring Boot 的主要功能之一是自动配置,这意味着它会配置一个可用的功能。

但是,对于 Java 模块,“可用”不仅取决于 jar 在类路径上,还取决于requires语句授予的访问权限。

如果没有该requires语句,Spring Boot 的自动配置就看不到 Tomcat,因此它甚至不会尝试对其进行配置。

结果:默默地忽略该功能。


推荐阅读