【发布时间】:2020-10-16 00:54:46
【问题描述】:
在切换到 Java 11 并将模块添加到充当 REST API 的 Spring Boot 应用程序后,我遇到了问题。运行应用程序时我没有收到任何错误,它在使用退出代码 0 初始化后关闭。Tomcat 嵌入式服务器没有启动,调度服务器也没有启动,这将阻止应用程序关闭和监听用于传入的请求。
在我看来,它似乎没有启动嵌入式容器,因为模块化阻止了 Spring Boot Autoconfigure 找到一些条件 Bean 来启动 REST 服务器。
没有错误。就像您在没有控制器的情况下运行应用程序一样,它会在没有错误的情况下关闭,因为没有服务器可以阻止它。我已经在上下文中列出了 bean 并且 HelloController 在那里,但是就像我说的那样,我找不到运行服务器应该存在的任何 bean,例如 web.servlet.DispatcherServlet。
我尝试过搜索这个,但不幸的是,模块这个术语早在 Java 9 之前就已经存在,具有不同的含义,因此很难找到任何答案。如果这个问题已经在某个深层次的地方发布了,我深表歉意。
即使是基本示例我也无法使其工作
Application.java
@SpringBootApplication()
public class SchoolsApplication {
public static void main(String[] args) {
SpringApplication.run(SchoolsApplication.class, args);
}
}
HelloController.java
@RestController
public class HelloController {
@GetMapping("hello")
public String hello(){
return "hello";
}
}
module-info.java
module controllers {
requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.context;
requires spring.beans;
requires spring.web;
requires com.fasterxml.jackson.databind;
opens package.controllers to spring.core;
}
我使用的是 Spring 2.3.4.RELEASE,实际上我使用的是 JDK14,但目标是 Java 11。
我尝试使用所需的模块(即 spring.webmvc、tomcat.embedded.core)或搜索要包含的确切 Spring bean,但没有成功。
编辑
当显式添加requires org.apache.tomcat.embed.core; 时,服务器启动并崩溃并出现错误
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory]: Factory method 'tomcatServletWebServerFactory' threw exception
...
java.util.ServiceConfigurationError: org.apache.juli.logging.Log: module org.apache.tomcat.embed.core does not declare `uses`
Tomcat 版本为9.0.38
【问题讨论】:
-
启动失败的日志是什么?
-
@Naman 没有错误。就像如果您使用 now 控制器运行应用程序,它将在没有错误的情况下关闭。我已经在上下文中列出了 bean 并且
HelloController在那里,但就像我说的那样,我找不到运行服务器应该存在的任何 bean。
标签: java spring-boot java-11 spring-restcontroller java-module