我最基本的spring应用程序停止工作,我无法理解发生了什么。
pom.xml:
<properties>
<spring.version>4.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
配置类:
@Configuration
public class MyConfig {
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
Bean类:
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
应用程序的入口点:
public class MainApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MyConfig.class);
HelloWorld bean = ctx.getBean(HelloWorld.class);
bean.setMessage("ladjfaj");
System.out.println(bean.getMessage());
}
}
我收到一个错误
最佳答案
您必须先致电ctx.refresh()
,然后才能致电ctx.getBean(HelloWorld.class);
关于java - AnnotationConfigApplicationContext尚未刷新-怎么了?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28404817/