2017-07-04 33 views
5

Có vấn đề gì với ứng dụng này. Tôi nghĩ rằng sự pha trộn của lọ classpath và lọ mô-đun là hợp lệ. Đối với tất cả các lọ không có một mô-đun thông tin rõ ràng trở thành một mô-đun tự động? Khi tôi xóa module-info.java, nó hoạt động. Vì IDEA sử dụng classpath cho trường hợp này.Chạy khởi động mùa xuân bằng jdk9 bằng cách sử dụng các mô-đun ghép hình

Java (TM) SE Runtime Environment (xây dựng 9 + 176)

IntelliJ IDEA 2017.1.4

module-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 [email protected]/org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:439) at [email protected]/org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418) at [email protected]/org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:409) at [email protected]/org.springframework.boot.SpringApplication.(SpringApplication.java:266) at [email protected]/org.springframework.boot.SpringApplication.(SpringApplication.java:247) at [email protected]/org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) at [email protected]/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 [email protected]/org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:145) at [email protected]/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

+0

Bạn nên đặt tên mô-đun theo gói của mình. Ví dụ 'module com.foo.test {...}' – axiopisty

Trả lời

5

Tôi giả sử spring.boot là một mô-đun tự động. Một mô-đun tự động không khai báo sự phụ thuộc của nó, do đó bạn phải sử dụng --add-modules để đảm bảo rằng mọi mô-đun rõ ràng cần được giải quyết. Nếu spring.boot là một mô-đun rõ ràng thì tôi cho rằng nó sẽ là requires java.sql và bạn sẽ không gặp vấn đề này.

+0

vm đối số: --add-modules = java.sql cũng hoạt động nhưng đó không phải là ý của bạn? – Daniel

+4

Vâng, ý tôi là '--add-modules = java.sql' để đảm bảo rằng mô-đun java.sql được giải quyết. Nó sẽ không được giải quyết khác bởi vì không có module nào yêu cầu nó. Bạn đã làm việc xung quanh nó bằng cách thêm 'require java.sql' vào module của bạn nhưng điều đó không cần thiết (và sẽ không cần thiết khi spring.boot được di trú sang một mô-đun rõ ràng). –

+0

Alex Buckley nói với chúng tôi tại các module tự động devoxxUS (Modular Development with JDK 9): "... Yêu cầu mọi thứ, chúng yêu cầu nhau, chúng yêu cầu tất cả các mô-đun trong hình ảnh jdk và chúng yêu cầu tất cả các mô-đun của bạn, và chúng xuất gói của họ ... "(Trích dẫn tương tự như những gì anh ta nói). Tôi vẫn không hiểu tại sao tôi phải thêm mô-đun java.sql. Và dù sao khi cần thiết thì tại sao chỉ java.sql? Tôi khá chắc chắn spring.boot có một số chi tiết. Yêu cầu và "tuyên bố sự phụ thuộc" không giống nhau? – Daniel

0

Điều này gần như chắc chắn vì bạn đang thiếu jar trình điều khiển jdbc cho MySql. bạn cần phải sử dụng phụ thuộc này: -

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

Không khác nhau, tôi nhận được cùng một stacktrace. – Daniel

3

cuối cùng, tôi đã nhận nó ... tôi mô-đun-info phải trông như thế này:

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 
} 

Ai đó có thể giải thích lý do tại sao tôi phải đòi hỏi java. sql; bên trong mô-đun của riêng tôi khi tôi không sử dụng nó?

Các vấn đề liên quan