java - 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 ?
解决方案
Spring Boot 的主要功能之一是自动配置,这意味着它会配置一个可用的功能。
但是,对于 Java 模块,“可用”不仅取决于 jar 在类路径上,还取决于requires
语句授予的访问权限。
如果没有该requires
语句,Spring Boot 的自动配置就看不到 Tomcat,因此它甚至不会尝试对其进行配置。
结果:默默地忽略该功能。
推荐阅读
- c# - Unity LookAt,而是将整个身体向 3d 空间中的方向旋转
- visual-studio-2010 - 如果我发布了对我的 nuget 包的更新但我没有更改版本号,Visual Studio 如何处理它?
- jenkins - Jenkins Pipeline 在使用 catchError 后获取当前阶段状态
- javascript - 确保文本的双引号被关闭
- java - 我是否正确使用了 clp-java
- windows - 一键在两个浏览器中打开两个网站?
- java - 如何从我的数组列表中返回最近加入的成员?
- c - 为什么 printf 不只打印 ini 字符串?
- java - java.time: 比较两个 Instants - 获取两者之间的小时数、分钟数、秒数、年数、月数
- python-3.x - 如何从 txt 文件中追加值?Python3