16
votes

I am getting the following exception when I execute the code via JUnit Test Case

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'somarFactory': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

Could someone advise what might be the issues?

9
This Exception is thrown in case of a bean being requested despite bean creation currently not being allowed (for example, during the shutdown phase of a bean factory). - Lucky
Could you please add some more details to understand it better? - Kathir
Could you please guide here: stackoverflow.com/questions/53959982/… ? - Jeff Cook

9 Answers

10
votes

Check if you have more than one tomcat's instance.
if this is your case, shutdown all instance then open one and only one instance.
I hope that will help you

7
votes

Go to this thread

I assume that you too have the same issue around there.. It got solved by setting the JAVA_HOME path And Updating your JDK to version 7 and try restarting your server..(solution)

I think that could solve your issue..

5
votes

In my case ,
I'm using multiple threads to call beans methods
and before finishing all threads
calling context.close()
made and throws this exception
with removing context.close
my problem solved.
hope useful

1
votes

@Transactional resolved in my case after using this it resolved transaction conflict between multiple transaction.

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)

I am using Spring Boot + Spring Data

1
votes

I was having this error while debugging an application on my integration test, trying to call a find method from Spring Data Repository.

My tested code was using @Async and @Scheduled. After some research, I disabled these two features on my spring integration test and the problem was resolved.

0
votes

You can also get this exception if you have a dependency that's in provided scope but it's not available at run time.

0
votes

In my case I had:

<dependency>
    <groupId>org.springframework.boot</groupId>-->
     <artifactId>spring-boot-starter-tomcat</artifactId>-->
    <scope>provided</scope>-->
</dependency>

in my pom.xml in a new project. I removed it and now it works.

0
votes

I was getting that error when tests were finished before the moment of finishing the async thread ran at the tested method.

The fix was to ensure, that tests won't finish until the thread job will be done.

-1
votes

I had this error also, but in my case I find out at the end of the error message, that was something related to two endpoints having the same @requestmapping value, I hope it helps :)