我开始学习Spring Framework作为我的第一个应用程序。我试图获得定义的Bean计数,但我无法运行它。
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class runDemo {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext();
System.out.println(context.getBeanDefinitionCount());
}
}
和我的gradle文件
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
当我运行时:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext ...
我在外部库中加载了spring context和spring core。
编辑:我升级到Intellij的想法2017.2,它仍然给出相同的错误答案 0 :(得分:0)
我换了
compile('org.springframework.boot:spring-boot-starter')
带
compile('org.springframework.boot:spring-boot-starter-web')