Java modules Jigsaw JPMS modularization prevents Spring container from starting rest controller because of org.apache.juli.logging.Log

huangapple go评论91阅读模式
英文:

Java modules Jigsaw JPMS modularization prevents Spring container from starting rest controller because of org.apache.juli.logging.Log

问题

我在切换到 Java 11 并为充当 REST APISpring Boot 应用添加模块后遇到了问题。在运行应用程序时没有收到任何错误消息,它在初始化后以退出代码 0 关闭。嵌入式 Tomcat 服务器和调度服务器均未启动,这将阻止应用程序关闭并等待传入请求。

对我来说,看起来像是由于模块化的原因,Spring Boot 自动配置无法找到某些条件 Bean 来初始化 REST 服务器,因此没有启动嵌入式容器。

没有错误。就像在没有控制器的情况下运行应用程序一样,如果没有服务器阻止它,它将在没有错误的情况下关闭。我已经在上下文中列出了这些 Bean,并且HelloController 是存在的,但是就像我所说,我找不到任何应该存在于运行服务器的 Bean,比如 web.servlet.DispatcherServlet

我尝试过搜索这个问题,但不幸的是,“模块” 这个术语在 Java 9 之前就已经存在,意义不同,这使得很难找到答案。如果在 Stack Overflow 的深处已经有人提出了这个问题,我向你道歉。

即使是最基本的示例我也无法使其工作。

  • 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,实际上我在使用 JDK 14,但目标是 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

英文:

I am having an issue after switching to Java 11 and adding modules to a Spring Boot application that acts as a REST API. I am not getting any errors when running the application and it shuts down after initialization with exit code 0. The Tomcat embedded server does not start, nor the dispatch server, which would prevent the application from closing and listen for incoming requests.

To me it looks like it does not start the embedded container because the modularization is preventing the Spring Boot Autoconfigure to find some Conditional Beans to initiate the REST server.

There is no error. It acts like if you run the application without a controller, it will shutdown without errors since there is no server to block it. I have listed the beans in the context and the HelloController is there, but like I said I can not find any beans that should exist for a running server, like web.servlet.DispatcherServlet.

I have tried searching for this, but unfortunately the fact that the term module existed long before Java 9 with a different meaning, makes it hard to find any answer. I apologize if somewhere deep in SO this question was already posted.

Even the basic example I can not make it work

  • 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;
}

I am using Spring 2.3.4.RELEASE and I am in fact using JDK14, but target is Java 11.

I have tried to play with the required modules (i.e. spring.webmvc, tomcat.embedded.core) or search for the exact Spring beans to include, but with no success.

EDIT
When adding explicitly requires org.apache.tomcat.embed.core; the server starts and crashes with the error

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`

The Tomcat version is 9.0.38

答案1

得分: 2

我成功将错误定位到了嵌入式 Tomcat 服务器。spring-web-starter 使用的版本是 tomcat-embed-core:9.0.38。该问题在 tomcat-embed-core:9.0.39 中得到修复:

open module org.apache.tomcat.embed.core {
   ...
   uses org.apache.juli.logging.Log;
}

因此,我通过 Maven 依赖管理解决了这个问题。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>9.0.39</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>9.0.39</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>
英文:

I managed to pinpoint the error to the Tomcat embedded server. The version used by spring-web-starter is tomcat-embed-core:9.0.38 . The issue was fixed in tomcat-embed-core:9.0.39 with

open module org.apache.tomcat.embed.core {
   ...
   uses org.apache.juli.logging.Log;
}

So I resolved this through maven dependency management.

   &lt;dependencyManagement&gt;
		&lt;dependencies&gt;
			&lt;dependency&gt;
				&lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
				&lt;artifactId&gt;tomcat-embed-core&lt;/artifactId&gt;
				&lt;version&gt;9.0.39&lt;/version&gt;
			&lt;/dependency&gt;
		&lt;/dependencies&gt;
	&lt;/dependencyManagement&gt;

   &lt;dependencies&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
		&lt;artifactId&gt;tomcat-embed-core&lt;/artifactId&gt;
		&lt;version&gt;9.0.39&lt;/version&gt;
	&lt;/dependency&gt;
    &lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;exclusions&gt;
			&lt;exclusion&gt;
				&lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
				&lt;artifactId&gt;tomcat-embed-core&lt;/artifactId&gt;
			&lt;/exclusion&gt;
		&lt;/exclusions&gt;
	&lt;/dependency&gt;
&lt;/dependencies&gt;

huangapple
  • 本文由 发表于 2020年10月16日 08:54:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64381530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定