简体   繁体   English

春季5中的Hello World与应用程序上下文

[英]Hello World in spring 5 with application context

I started to learn Spring Framework as my first ever app.I tried to get defined Beans count but i can't run it. 我开始学习Spring Framework是我的第一个应用程序。我试图获取定义的Beans数量,但无法运行它。

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());
    }
}

and my gradle file 和我的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')
}

and when i run this: 当我运行这个:

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 ...

I have spring context and spring core in the External Libraries loaded. 我在外部库中加载了spring上下文和spring核心。

EDIT:I upgraded to Intellij idea 2017.2 and it still give the same error 编辑:我升级到Intellij idea 2017.2,它仍然给出相同的错误

i replaced 我取代了

compile('org.springframework.boot:spring-boot-starter')

with

compile('org.springframework.boot:spring-boot-starter-web')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM