【问题标题】:AnnotationConfigApplicationContext has not been refreshed yet using ApplicationContextAwareAnnotationConfigApplicationContext 尚未使用 ApplicationContextAware 刷新
【发布时间】:2018-11-27 22:57:01
【问题描述】:

我在非 Spring 框架中使用 Spring bean,为此我实现了 ApplicationContextAware 来访问 Spring bean。

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

}

我尝试从非 spring 类访问 Spring 服务:ConnectionStateService:

this.connectionStateService = ApplicationContextProviderService.getBean(ConnectionStateService.class);

我收到以下错误:

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 spring spring-boot


    【解决方案1】:

    当应用程序在应用程序运行之前尝试调用 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; 
    } 
    

    }

    【讨论】:

    • 服务注解继承自@Component
    • 你说最好先用注入,那要改什么?
    • 是的,我想,我之前做过一个测试,我注意到它是第一个注入的组件,它被引用为 Bean 我认为你的问题在于你调用上下文的方法。你能添加那个方法吗?
    • Mykhailo 已经开始聊天,我在那里发布了一些详细信息:chat.stackoverflow.com/rooms/184244/…我的第一篇文章提供了主要详细信息。
    • 这个问题主要是因为应用程序上下文没有被初始化。虽然我不知道原因,但这就是原因。
    【解决方案2】:

    我认为当上下文未初始化时你正在获取 bean

    所以实际上请确保您正在调用这段代码:

    this.connectionStateService = ApplicationContextProviderService.getBean(ConnectionStateService.class);
    

    上下文初始化后

    【讨论】:

    • ApplicationContext 对象没有刷新方法。当从 spring 上下文调用时,bean 确实会返回,但从非 spring 类调用时它不起作用。
    • AnnotationConfigApplicationContext 有它
    • 你遇到了一个异常。我的目的是上下文尚未初始化
    • 当我这样做时,我得到上下文只能刷新一次的错误。
    • 当你从非 bean 类调用你的方法时?
    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 2013-09-06
    • 2017-01-16
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多