Java AnnotationConfigApplicationContext 尚未刷新 - 有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28404817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AnnotationConfigApplicationContext has not been refreshed yet - what's wrong?
提问by Nikolas
My very basic spring application stopped working and I can't understand what's happened. pom.xml:
我非常基本的 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>
Config class:
配置类:
@Configuration
public class MyConfig {
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
Bean class:
豆类:
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
Entry point of application:
申请入口:
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());
}
}
And I'm getting an error
我收到一个错误
Exception in thread "main" java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext@6ebf8cf5 has not been refreshed yet at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:943) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:967) at com.nikolas.config.MainApp.main(MainApp.java:12)
线程“main”中的异常 java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext@6ebf8cf5 尚未在 org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:943) 处刷新。 springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:967) 在 com.nikolas.config.MainApp.main(MainApp.java:12)
采纳答案by Jens
You have to call ctx.refresh()
before you can call ctx.getBean(HelloWorld.class);
你必须先打电话ctx.refresh()
才能打电话ctx.getBean(HelloWorld.class);
回答by Назар Кулян
If you dont want to call ctx.refresh()
explicitly, just initialize ApplicationContext like this:
new AnnotationConfigApplicationContext(MyConfig.class)
, then configuration will be registered and refreshed implicitly
如果不想ctx.refresh()
显式调用,只需像这样初始化ApplicationContext:,
new AnnotationConfigApplicationContext(MyConfig.class)
然后配置将被隐式注册和刷新