使用jigsaw模块使用jdk9运行spring boot

Dan*_*iel 6 java spring java-platform-module-system java-9

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

Java(TM)SE运行时环境(版本9 + 176)

IntelliJ IDEA 2017.1.4

module-info.java

module test {
    requires spring.boot.autoconfigure;
    requires spring.boot;
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

线程"main"中的异常java.lang.IllegalArgumentException:无法在spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication中实例化接口org.springframework.context.ApplicationContextInitializer:org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer .createSpringFactoriesInstances(SpringApplication.java:439)spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)spring.boot@2.0.0.M2/org.springframework spring.boot @2.0.0.M2 org.springframework.boot.SpringApplication.(SpringApplication.java:247)spring.boot@2.0.0.M2/org.springframework.boot.SpringApplication.run(SpringApplication.java:1245)spring.boot@2.0.0 .M2/org.springframework.boot.SpringApplication.run(SpringApplication.java:1233)at test/com.foo.test.App.main(App.java:10)引起: java.lang.NoClassDefFoundError:java/sql/SQLException ,发布于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更多引起:java.lang.ClassNotFoundException :java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)中的java.sql.SQLException,位于java.base/jdk.internal.loader.ClassLoaders $ AppClassLoader.loadClass(ClassLoaders.java:185)在java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)... 9更多

Ala*_*man 7

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

  • 是的,我的意思是`--add-modules = java.sql`以确保解析java.sql模块.否则将无法解决,因为没有模块需要它.你已经通过向你的模块添加`require java.sql`来解决这个问题,但是不应该这样做(并且一旦spring.boot迁移到显式模块就不需要了). (4认同)

Dan*_*iel 5

终于,我明白了……我的模块信息必须看起来像这样:

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
}
Run Code Online (Sandbox Code Playgroud)

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

  • 因为`spring`需要java.sql。由于`spring`是自动模块,它不会声明需要工作的模块,因此您需要通过--add-modules java.sql`或`requires java.sql`将java.sql包含到模块图中。你自己的模块 (2认同)