开发者

Why is Spring Container Destroying Beans Immediately After Creating Them?

开发者 https://www.devze.com 2023-04-06 06:13 出处:网络
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.

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 creatin开发者_运维百科g 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.


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.


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.


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.


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.


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:

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();
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消