java 为什么Spring容器创建后立即销毁bean?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7502950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is Spring Container Destroying Beans Immediately After Creating Them?
提问by kdone
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.
创建在我的应用程序的各种上下文文件中声明的所有 bean 后,Spring 立即通知(见下文)它正在销毁单例并且上下文初始化失败。
[INFO] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory org.springframework.web.context.ContextLoader [ERROR] Context initialization failed
[信息] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory org.springframework.web.context.ContextLoader 中销毁单例 [ERROR] 上下文初始化失败
Does anyone know why the Spring container is destroying all the beans right after creating them?
有谁知道为什么 Spring 容器在创建它们后立即销毁所有 bean?
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.
[DEBUG] 急切地缓存 bean 'uploadService' 以允许解决潜在的循环引用 2011-09-21 15:19:08 org.springframework.beans.factory.annotation.InjectionMetadata
[DEBUG] 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] 创建单例 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
[信息] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory@b0ede6 中销毁单例:定义 bean [org.springframework.beans.
采纳答案by gkamal
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.
上下文初始化失败导致 spring 销毁已经成功创建的 bean - 而不是相反。您可能需要将日志级别提高到 INFO 或 DEBUG 才能找到根本原因。
回答by atrain
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.
当遇到不知道是什么导致问题的情况时,请消除复杂性。在您的情况下,从配置中删除大部分 bean,无论是 XML 还是基于注释的。开始重新添加它们,看看哪个打破了启动周期。然后,您可以专注于该 bean 导致故障的原因。
回答by vucalur
Short answer:
简答:
Try increasing JVM memory
尝试增加 JVM 内存
here: -XX:PermSize=64m -XX:MaxPermSize=128m -Xms256m -Xmx768m
这里: -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.
通常,Spring 会拼命销毁 bean(因此进行通信)作为获得一些内存的一种方式。
此外,高垃圾收集器活动会减慢 Spring 初始化速度。
上面我提供了适合我的设置。根据您的应用程序复杂性,您可能想要使用这些值。
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.
免责声明:情况并非总是如此。但是我的 spring 应用程序经常因为使用默认的 JVM 内存设置运行它们而崩溃。
回答by Hariharasuthan Ganesan
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.
调试是找到根本原因的最佳方式。如果您使用 Eclipse 进行开发,请在调试模式下运行。等待控制进入 catch 块,在变量编辑器中你可以找到异常对象,它应该有堆栈跟踪。这样您就可以找到问题的根本原因。
回答by tazarov
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:
我最近遇到了类似的问题。该问题的一种可能解决方案是检查您的主类或您初始化 spring 上下文的任何地方。有时会发生 spring 上下文抛出的异常被捕获并且永远不会打印或重新抛出的情况。考虑下面的例子:
AbstractApplicationContext context = null;
try {
context = new ClassPathXmlApplicationContext("beans.xml");
// context.registerShutdownHook();
} catch (Exception e) {
e.printStackTrace();
//print or log error
} finally {
if (context != null) {
context.close();
}
}