AnnotationConfigApplicationContext尚未刷新 - 出了什么问题? [英] AnnotationConfigApplicationContext has not been refreshed yet - what's wrong?

查看:10197
本文介绍了AnnotationConfigApplicationContext尚未刷新 - 出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的非常基本的春天应用程序停止工作,我不明白发生了什么。
pom.xml:

My very basic spring application stopped working and I can't understand what's happened. 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类: / p>

Bean class:

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



And I'm getting an error


线程main中的异常java.lang.IllegalStateException:org.springframework.context.annotation.AnnotationConfigApplicationContext@6ebf8cf5尚未刷新
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)

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)


推荐答案

ctx.refresh()之前,您可以调用 ctx.getBean(HelloWorld.class);

这篇关于AnnotationConfigApplicationContext尚未刷新 - 出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆