运行 spring 使用 jigsaw 模块通过 jdk9 启动

Run spring boot with jdk9 using jigsaw modules

这个应用程序有什么问题。我认为类路径 jar 和模块 jar 的混合是有效的。对于所有没有显式模块信息的罐子成为自动模块?当我删除我的 module-info.java 时,它起作用了。因为 IDEA 在这种情况下使用类路径。

Java(TM) SE Runtime Environment (build 9+176)

IntelliJ IDEA 2017.1.4

模块-info.java

module test {
    requires spring.boot.autoconfigure;
    requires spring.boot;
}

App.java

package com.foo.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo.test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M2</version>
    </parent>

    <name>test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.9</maven.compiler.source>
        <maven.compiler.target>1.9</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer : org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer at spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:439) at spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418) at spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:409) at spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.(SpringApplication.java:266) at spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.(SpringApplication.java:247) at spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) at spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) at test/com.foo.test.App.main(App.java:10) Caused by: java.lang.NoClassDefFoundError: java/sql/SQLException at spring.beans@5.0.0.RC2/org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:145) at spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:435) ... 7 more Caused by: java.lang.ClassNotFoundException: java.sql.SQLException at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496) ... 9 more

这几乎可以肯定是因为您缺少 MySql 的 jdbc 驱动程序 jar。您需要使用此依赖项:-

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>6.0.5</version>
</dependency>

终于,我明白了...我的模块信息必须如下所示:

module test {
    requires java.sql; // my real problem solved with this
    requires spring.boot.autoconfigure;
    requires spring.boot;
    exports com.foo.test; // subsequent error 1: beeing accessible for some spring modules
    opens com.foo.test to spring.core; // subsequent error 2: beeing accessible for spring.core in a deep reflection way
}

有人可以解释为什么我必须要求 java.sql;当我不使用它时在我自己的模块中?

我假设 spring.boot 是一个自动模块。自动模块不会声明它的依赖关系,因此您必须使用 --add-modules 来确保解析所需的任何显式模块。如果 spring.boot 是一个显式模块,那么我认为它会 requires java.sql 而你不会遇到这个问题。

我从 Java 8 升级到 Java 11 时遇到了同样的问题。

通过在 Intellij 运行 配置中选择不同的选项解决: