Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Spring Container Destroying Beans Immediately After Creating Them?

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.

like image 500
kdone Avatar asked Sep 21 '11 16:09

kdone


People also ask

When a Spring bean is destroyed which is the order in which the following steps are executed?

Here, we will use init() method to execute all its code as the spring container starts up and the bean is instantiated, and destroy() method to execute all its code on closing the container. Spring provides three ways to implement the life cycle of a bean.

What is the typical bean life cycle in Spring bean factory container?

1. What is Life Cycle of a Bean? A Spring bean needs to be instantiated when the container starts, based on Java or XML bean definition. The framework may also be required to perform some pre and post-initialization steps to get the bean into a usable state.

How do I initialize a bean instance in Spring?

To define setup and teardown for a bean, we simply declare the <bean> with initmethod and/or destroy-method parameters. The init-method attribute specifies a method that is to be called on the bean immediately upon instantiation.


2 Answers

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.

like image 170
gkamal Avatar answered Oct 18 '22 00:10

gkamal


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.

like image 25
atrain Avatar answered Oct 18 '22 02:10

atrain