我有一个非常简单的应用程序,完全适用于Java 1.8,它包含一个Main类
package com.company.getworks.start;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan()
@EnableAutoConfiguration
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class, args);
}
}
还有一个@RestContoller
类,它只包含一个@RequestMapping("/hello")
方法
我添加了看起来像的module-info.java
module com.company.getworks.start {
requires spring.boot;
requires spring.webmvc;
}
我的build.gradle是
group 'com.company'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.9
repositories {
mavenCentral()
}
dependencies {
final def springVersion='4.3.10.RELEASE'
final def springBootVersion='1.5.6.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile (group: 'org.springframework', name: 'spring-webmvc', version: springVersion) {
exclude group: 'commons-logging', module: 'commons-logging'
}
compile(group: "org.springframework.boot", name: "spring-boot-starter-web", version: springBootVersion) {
exclude group: 'commons-logging', module: 'commons-logging'
}
}
ext.moduleName = 'com.company.getworks.start'
compileJava {
inputs.property("moduleName", moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'spring.boot',
'--add-modules', 'spring.webmvc',
]
classpath = files()
}
}
运行clean compileJava
以Build成功结束,但是当我尝试运行我的ApplicationMain.java时,它以Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer : org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer
Caused by: java.lang.NoClassDefFoundError: java/sql/SQLException
结束我做错了什么?
我不得不
1)升级到弹簧靴2
2)将我的依赖列表更改为
dependencies {
final def springVersion='4.3.10.RELEASE'
final def springBootVersion='2.0.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile (group: 'org.springframework', name: 'spring-webmvc', version: springVersion) {
exclude group: 'commons-logging', module: 'commons-logging'
}
compile(group: "org.springframework.boot", name: "spring-boot-starter-web", version: springBootVersion) {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'javax.annotation', module: 'javax.annotation-api'
}
}
3)更新module-info.java
module com.company.getworks.start {
requires spring.boot;
requires spring.webmvc;
requires spring.context;
requires spring.boot.autoconfigure;
requires spring.web;
opens com.company.getworks.start;
exports com.company.getworks.web;
}
其中com.company.getworks.web
是包含我的@RestController类的包
4)正确提到@miroh将--add-modules java.sql添加到我的java命令中