Java 尚未使用ApplicationContextAware刷新AnnotationConfigApplicationContext
我在非Spring框架中使用Springbeans,为此我实现了ApplicationContextAware来访问SpringbeansJava 尚未使用ApplicationContextAware刷新AnnotationConfigApplicationContext,java,spring,spring-boot,Java,Spring,Spring Boot,我在非Spring框架中使用Springbeans,为此我实现了ApplicationContextAware来访问Springbeans @Service public class ApplicationContextProviderService implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public voi
@Service
public class ApplicationContextProviderService implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextProviderService.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> beanType) {
System.out.println("bean out: " + applicationContext);
return applicationContext.getBean(beanType);
}
}
我得到以下错误:
java.lang.IllegalStateException:
**org.springframework.context.annotation.AnnotationConfigApplicationContext@7f485fda has not been refreshed yet at
** org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1072) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1102) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at com.example.app.service.ApplicationContextProviderService.getBean(ApplicationContextProviderService.java:19) ~[classes/:na] at com.example.app.CustomFilter.<init>(CustomFilter.java:26) ~[classes/:na]
java.lang.IllegalStateException:
**org.springframework.context.annotation。AnnotationConfigApplicationContext@7f485fda尚未在刷新
**org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1072)~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]位于org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1102)~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]在com.example.app.service.ApplicationContextProviderService.getBean(ApplicationContextProviderService.java:19)~[classes/:na]在com.example.app.CustomFilter.(CustomFilter.java:26)~[classes/:na]
如何解决这个问题?我认为您在未初始化上下文时获得了bean 因此,请确保您正在调用这段代码:
this.connectionStateService = ApplicationContextProviderService.getBean(ConnectionStateService.class);
初始化上下文后在应用程序运行之前,当应用程序尝试调用ApplicationContext时,问题与此相关,因此您将无法拥有应用程序上下文,因为它未创建。解决方案是为ConnectionStateService类创建一个单例,这样您就不需要创建调用ApplicationContext的服务
public class ConnectionStateService {
// static variable instance of type Singleton
private static ConnectionStateService single_instance = null;
// private constructor restricted to this class itself
private ConnectionStateService() {
// some stuffs for initialize here
}
// static method to create instance of Singleton class
public static ConnectionStateService getInstance() {
if (instance == null)
instance = new ConnectionStateService();
return instance;
}
}ApplicationContext对象没有刷新方法。该bean在从spring上下文调用时返回,但在从非spring类调用时不起作用。AnnotationConfigApplicationContext具有该bean,并且您遇到异常。我的目的是上下文没有初始化Yet当我这样做的时候,我得到一个错误,上下文只能刷新一次。当你从非bean类调用你的方法时?服务注释是从@Component继承的。你说最好在之前使用注入它,那么它应该更改什么?是的,我想,我之前做过一个测试,我注意到这是第一个被注入的组件被引用,就像Bean一样。我认为您的问题在于您调用上下文的方法。你能添加这个方法吗?Mykhailo已经开始聊天,我在那里发布了一些详细信息:我的第一篇文章给出了主要的详细信息。这个问题主要是因为应用程序上下文没有初始化。虽然原因我不知道,但那就是原因。
public class ConnectionStateService {
// static variable instance of type Singleton
private static ConnectionStateService single_instance = null;
// private constructor restricted to this class itself
private ConnectionStateService() {
// some stuffs for initialize here
}
// static method to create instance of Singleton class
public static ConnectionStateService getInstance() {
if (instance == null)
instance = new ConnectionStateService();
return instance;
}