簡體   English   中英

使用 jigsaw 模塊使用 jdk9 運行 spring boot

[英]Run spring boot with jdk9 using jigsaw modules

這個應用程序有什么問題。 我認為類路徑 jar 和模塊 jar 的混合是有效的。 對於所有沒有顯式模塊信息的 jar 成為自動模塊? 當我刪除我的 module-info.java 它工作。 因為 IDEA 在這種情況下使用了類路徑。

Java(TM) SE 運行時環境(構建 9+176)

IntelliJ IDEA 2017.1.4

模塊信息.java

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

應用程序.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>

線程“main”中的異常 java.lang.IllegalArgumentException:無法實例化接口 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) 在 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) 在 test/com.foo.test.App.main(App.java:10) 引起: 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 引起: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 更多

我假設 spring.boot 是一個自動模塊。 自動模塊不會聲明它的依賴關系,因此您必須使用--add-modules來確保解析所需的任何顯式模塊。 如果 spring.boot 是一個顯式模塊,那么我認為它requires java.sql並且您不會遇到這個問題。

最后,我明白了……我的模塊信息必須如下所示:

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; 當我不使用它時在我自己的模塊中?

從 Java 8 升級到 Java 11 時,我遇到了同樣的問題。

通過在 Intellij 運行配置中選擇不同的選項來解決: 縮短命令行

這幾乎可以肯定是因為您缺少 MySql 的 jdbc 驅動程序 jar。 您需要使用此依賴項:-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM