在 Spring 中嵌入 Tomcat 引导 Web 应用程序不以模块信息启动
Embedded Tomcat in Spring Boot Web application does not start with module-info
我想用 java 9+ 之类的模块学习一些新东西。
所以我用 spring-boot-starter-web
和 module-info.java
:
创建了一个 spring 启动应用程序
module com.example.demo {
requires spring.boot;
requires spring.boot.autoconfigure;
exports com.example.demo;
opens com.example.demo;
}
我用过
-> Java 15.0.2
-> Spring 启动 2.4.3
-> IntelliJ IDE 2020.3
-> Ubuntu 20.04.2 LTS
为了 运行 我在 IntelliJ 中使用 运行 按钮(绿色箭头)的应用程序 - 默认 运行 没有任何额外的参数。问题是当我尝试 运行 应用程序时,嵌入式 tomcat 服务器没有启动。
输出模块-info.java
2021-03-12 08:57:12.182 INFO 4843 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 15.0.2 on vagrant-VirtualBox with PID 4843
2021-03-12 08:57:12.184 INFO 4843 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-03-12 08:57:13.081 INFO 4843 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.511 seconds (JVM running for 3.418)
但是,当我删除 module-info.java
时,一切正常,并且 tomcat 服务器启动了。
无模块输出-info.java
2021-03-12 09:01:49.627 INFO 4928 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 15.0.2 on vagrant-VirtualBox with PID 4928
2021-03-12 09:01:49.633 INFO 4928 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-03-12 09:01:50.897 INFO 4928 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-12 09:01:50.912 INFO 4928 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-12 09:01:50.912 INFO 4928 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-12 09:01:50.958 INFO 4928 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-12 09:01:50.958 INFO 4928 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1188 ms
2021-03-12 09:01:51.179 INFO 4928 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-12 09:01:51.320 INFO 4928 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-12 09:01:51.329 INFO 4928 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.271 seconds (JVM running for 3.774)
我想知道输出之间的区别。当我使用 module-info.java
Tomcat 不 运行 因为它超出了我的模块?
我可以对 运行 应用程序做什么?
您如何看待将 java 模块与 Spring 引导一起使用是否有意义?
为什么?
为什么不呢?
您需要在modules-info.java中添加requires org.apache.tomcat.embed.core;
以包含org.apache.catalina.startup.Tomcat.class
,具体方法见this article Spring 启动 tomcat 服务器。
我想用 java 9+ 之类的模块学习一些新东西。
所以我用 spring-boot-starter-web
和 module-info.java
:
module com.example.demo {
requires spring.boot;
requires spring.boot.autoconfigure;
exports com.example.demo;
opens com.example.demo;
}
我用过
-> Java 15.0.2
-> Spring 启动 2.4.3
-> IntelliJ IDE 2020.3
-> Ubuntu 20.04.2 LTS
为了 运行 我在 IntelliJ 中使用 运行 按钮(绿色箭头)的应用程序 - 默认 运行 没有任何额外的参数。问题是当我尝试 运行 应用程序时,嵌入式 tomcat 服务器没有启动。
输出模块-info.java
2021-03-12 08:57:12.182 INFO 4843 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 15.0.2 on vagrant-VirtualBox with PID 4843
2021-03-12 08:57:12.184 INFO 4843 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-03-12 08:57:13.081 INFO 4843 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.511 seconds (JVM running for 3.418)
但是,当我删除 module-info.java
时,一切正常,并且 tomcat 服务器启动了。
无模块输出-info.java
2021-03-12 09:01:49.627 INFO 4928 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 15.0.2 on vagrant-VirtualBox with PID 4928
2021-03-12 09:01:49.633 INFO 4928 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-03-12 09:01:50.897 INFO 4928 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-12 09:01:50.912 INFO 4928 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-12 09:01:50.912 INFO 4928 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-12 09:01:50.958 INFO 4928 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-12 09:01:50.958 INFO 4928 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1188 ms
2021-03-12 09:01:51.179 INFO 4928 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-12 09:01:51.320 INFO 4928 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-12 09:01:51.329 INFO 4928 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.271 seconds (JVM running for 3.774)
我想知道输出之间的区别。当我使用 module-info.java
Tomcat 不 运行 因为它超出了我的模块?
我可以对 运行 应用程序做什么?
您如何看待将 java 模块与 Spring 引导一起使用是否有意义? 为什么? 为什么不呢?
您需要在modules-info.java中添加requires org.apache.tomcat.embed.core;
以包含org.apache.catalina.startup.Tomcat.class
,具体方法见this article Spring 启动 tomcat 服务器。