【发布时间】:2017-08-06 17:18:00
【问题描述】:
我开始学习 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 ...
我在外部库中加载了弹簧上下文和弹簧核心。
编辑:我升级到 Intellij IDEA 2017.2 仍然报同样的错误
【问题讨论】:
-
你是如何运行它的?
-
我刚刚运行了 runDemo 类...我做了视频教程,但我不知道现在出了什么问题?
-
您使用 IDE 吗?如果这是您的第一个应用程序,可能会从更简单的东西开始?就像没有 Spring 的 Hello World。
-
我用java两年了...我用intellij idea
-
好的,现在我们到了某个地方。因为您编写了“Spring Framework 作为我的第一个应用程序”,所以我认为您刚刚开始编程。检查this 是否对您有帮助。