如何处理“IllegalStateException:BeanFactory 未初始化或已关闭”?

Posted

技术标签:

【中文标题】如何处理“IllegalStateException:BeanFactory 未初始化或已关闭”?【英文标题】:How can I cope with "IllegalStateException: BeanFactory not initialized or already closed"? 【发布时间】:2011-10-31 13:33:54 【问题描述】:

在 Tomcat 7 上使用 Grails 2.0.0,我在启动时得到以下正确结果:

2011-08-21 11:10:09,758 [main] ERROR StackTrace  - Full Stack Trace:
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
        at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
        at org.springframework.context.support.AbstractApplicationContext.containsBean(AbstractApplicationContext.java:1109)
        at org.codehaus.groovy.grails.web.context.GrailsContextLoader$1.run(GrailsContextLoader.java:88)
        at org.codehaus.groovy.grails.lifecycle.ShutdownOperations.runOperations(ShutdownOperations.java:62)
        at org.codehaus.groovy.grails.web.context.GrailsContextLoader.closeWebApplicationContext(GrailsContextLoader.java:136)
        at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:142)
        at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4763)
        at org.apache.catalina.core.StandardContext$4.run(StandardContext.java:5472)
        at java.lang.Thread.run(Thread.java:619)
        at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5481)
        at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:225)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:812)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:787)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:607)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:633)
        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:558)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:468)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1322)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:89)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:379)
        at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:324)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1041)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:774)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:291)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:620)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:303)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:431)
2011-08-21 11:10:09,761 [main] ERROR StackTrace  - Full Stack Trace:
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
        at java.lang.Thread.run(Thread.java:619)

这可能是什么原因?如何解决?

注意:如果您知道解决方案,您也可以回答其他用户发布的 "call 'refresh'" error resulting from Grails WAR hot-deploy。

【问题讨论】:

在此之前应该有另一个痕迹 其实就是整个日志文件... 【参考方案1】:

原来在日志文件catalina.out 中提供了其他信息。当我发布问题时,我只检查了特定于应用程序的stacktrace.log

感谢 https://***.com/users/203907/bozho 的提示!

问题源于应用程序无法连接到数据库。

因此可以通过在数据库端配置访问权限来解决。

【讨论】:

谢谢,我的最终是由错误的 spring 安全配置引起的,但这帮助我找到了它。

以上是关于如何处理“IllegalStateException:BeanFactory 未初始化或已关闭”?的主要内容,如果未能解决你的问题,请参考以下文章

如何处理 UsernameNotFoundException 春季安全

如何处理c#中的错误代码

Akka 如何处理消息版本?

开玩笑测试 - 如何处理 JsonWebToken 响应

如何处理 JSON 响应

“不知道如何处理' nvcc 致命错误