为什么 Spring 容器在创建 Bean 后立即销毁它们?
在创建在我的应用程序的各个上下文文件中声明的所有 bean 后,Spring 立即通知(见下文)它正在销毁单例并且上下文初始化失败。
[INFO] 销毁单例 org.springframework.beans.factory.support.DefaultListableBeanFactory org.springframework.web.context.ContextLoader [错误] 上下文 初始化失败
有谁知道为什么 Spring 容器在创建所有 bean 后立即销毁它们?
注意:除了上述上下文初始化失败错误之外,日志输出中没有任何警告或错误 - 见下文。
[DEBUG] 急切地缓存 bean“uploadService”以允许解决潜在的循环引用 2011-09-21 15:19:08 org.springframework.beans.factory.annotation.InjectionMetadata
[DEBUG] 处理 bean“uploadService”的注入方法:AutowiredFieldElement 为私有 org.apache.commons.fileupload.disk.DiskFileItemFactory com.faciler.ws.services.UploadService.diskFileFactory 2011-09-21 15:19:08 org.springframework.beans.factory.support.DefaultListableBeanFactory
[DEBUG] 创建单例 bean 的共享实例 'diskFileItemFactory' 2011-09-21 15:19:08 org.springframework.beans.factory.support.DefaultListableBeanFactory
[DEBUG] 创建 bean 'diskFileItemFactory' 实例 2011-09-21 15:19:08 org.springframework.beans.factory.support.DefaultListableBeanFactory
[INFO] 销毁单例 org.springframework.beans.factory.support.DefaultListableBeanFactory@b0ede6: 定义bean [org.springframework.beans.
Immediately after creating all the beans declared in the various context files of my application, Spring notifies (see below) that it is destroying singletons and that context initialization failed.
[INFO] Destroying singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory
org.springframework.web.context.ContextLoader [ERROR] Context
initialization failed
Does anyone know why the Spring container is destroying all the beans right after creating them?
NOTE: There are no warnings or errors in the log output aside from the above context initialization failure error -- see below.
[DEBUG] Eagerly caching bean 'uploadService' to allow for resolving potential circular references
2011-09-21 15:19:08 org.springframework.beans.factory.annotation.InjectionMetadata[DEBUG] Processing injected method of bean 'uploadService': AutowiredFieldElement for private
org.apache.commons.fileupload.disk.DiskFileItemFactory com.faciler.ws.services.UploadService.diskFileFactory 2011-09-21 15:19:08
org.springframework.beans.factory.support.DefaultListableBeanFactory[DEBUG] Creating shared instance of singleton bean
'diskFileItemFactory' 2011-09-21 15:19:08
org.springframework.beans.factory.support.DefaultListableBeanFactory[DEBUG] Creating instance of bean 'diskFileItemFactory' 2011-09-21
15:19:08
org.springframework.beans.factory.support.DefaultListableBeanFactory[INFO] Destroying singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@b0ede6:
defining beans [org.springframework.beans.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
上下文初始化失败导致 spring 销毁已经成功创建的 bean - 而不是相反。您可能需要将日志级别提高到 INFO 或 DEBUG 才能找到根本原因。
The context initialization failure is causing spring to destroy the beans already successfully created - not the other way round. You will probably need to up the log level to INFO or DEBUG to get to the root cause.
当您遇到不知道问题原因的情况时,请消除复杂性。根据您的情况,从配置中删除大部分 bean,无论是基于 XML 还是基于注释。开始将它们添加回来,看看哪一个破坏了启动周期。然后您就可以专注于为什么那个 Bean 导致失败。
When faced with a situation where you don't know what's causing the issue, remove complexity. In your case, remove most of your beans from the configuration, whether XML or annotation-based. Start adding them back in and see which one breaks the startup cycle. Then you can focus in on why that one bean is causing the failure.
简短回答:
尝试在此处增加 JVM 内存
:
-XX:PermSize=64m -XX:MaxPermSize=128m -Xms256m -Xmx768m
详细回答:
Spring 通常会拼命销毁 beans(因此进行通信)作为获取的一种方式一些记忆。
另外,高垃圾收集器活动会减慢 spring 初始化速度。
上面我提供了适合我的设置。根据您的应用程序的复杂性,您可能需要使用这些值。
免责声明:情况并非总是如此。但我的 Spring 应用程序经常由于使用默认 JVM 内存设置运行而崩溃。
Short answer:
Try increasing JVM memory
here:
-XX:PermSize=64m -XX:MaxPermSize=128m -Xms256m -Xmx768m
Detailed answer:
Often Spring desperately destroys beans (hence the communicate) as a way of gaining some memory.
Additionally high Garbage Collector activity slows down spring initialization.
Above I provide settings working for me. Depending on your application complexity you may want to play around with these values.
Disclaimer: It's not always the case. But frequently my spring apps beak down as a result of running them with default JVM memory settings.
调试是找到根本原因的最佳方法。如果您使用Eclipse进行开发,请在调试模式下运行。等待控件转到 catch 块,然后在变量编辑器中您可以找到异常对象,该对象应该具有堆栈跟踪。这样您就可以找到问题的根本原因。
Debuging is the best way to find the root cause. If you are using Eclipse for development, run in the debug mode. wait for the control goes to the catch block and in the variables editor you can find the exception object, which should have the stack trace. This way you can find the root cause of the issue.
我最近遇到了类似的问题。该问题的一种可能的解决方案是检查您的主类或初始化 spring 上下文的位置。有时会发生 spring 上下文抛出的异常被捕获并且从不打印或重新抛出的情况。考虑下面的例子:
I have recently faced similar issue. One possible solution to the problem would be to check your main class or wherever you initialize the spring context. Sometimes it happens that exceptions thrown by spring context are caught and never printed or re-thrown. Consider the example below: