AnnotationConfigApplicationContext 尚未刷新 - 怎么了?
AnnotationConfigApplicationContext has not been refreshed yet - what's wrong?
我最基本的 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>
配置class:
@Configuration
public class MyConfig {
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
豆子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());
}
}
我遇到了一个错误
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);
如果您不想显式调用 ctx.refresh()
,只需像这样初始化 ApplicationContext:
new AnnotationConfigApplicationContext(MyConfig.class)
,然后配置将被隐式注册和刷新
以防万一有人遇到类似问题并且不能直接与上面的示例相关,这可能会有所帮助:
我 运行 当我的存储库之一位于
中包含的文件夹之外时,我遇到了问题
@EnableJpaRepositories(basePackages = {"com.myproject.repositores"})
导致第一个异常的原因:
Description: Field profileRepository in
com.myproject.featurepackage.config.ProfileService required a bean of
type 'com.myproject.featurepackage.ProfileRepository' that could not
be found. The injection point has the following annotations: -
@org.springframework.beans.factory.annotation.Autowired(required=true)
当我之后访问ApplicationContext实例化一个bean时,我运行进入错误
org.springframework.context.annotation.AnnotationConfigApplicationContext@4983159f
has not been refreshed yet
我最基本的 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>
配置class:
@Configuration
public class MyConfig {
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
豆子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());
}
}
我遇到了一个错误
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);
如果您不想显式调用 ctx.refresh()
,只需像这样初始化 ApplicationContext:
new AnnotationConfigApplicationContext(MyConfig.class)
,然后配置将被隐式注册和刷新
以防万一有人遇到类似问题并且不能直接与上面的示例相关,这可能会有所帮助:
我 运行 当我的存储库之一位于
中包含的文件夹之外时,我遇到了问题@EnableJpaRepositories(basePackages = {"com.myproject.repositores"})
导致第一个异常的原因:
Description: Field profileRepository in com.myproject.featurepackage.config.ProfileService required a bean of type 'com.myproject.featurepackage.ProfileRepository' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
当我之后访问ApplicationContext实例化一个bean时,我运行进入错误
org.springframework.context.annotation.AnnotationConfigApplicationContext@4983159f has not been refreshed yet