21

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kathir
  • 2,733
  • 12
  • 39
  • 67
  • 2
    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 Feb 22 '13 at 05:55
  • 3
    Could you please add some more details to understand it better? – Kathir Feb 22 '13 at 06:25
  • Could you please guide here: https://stackoverflow.com/questions/53959982/error-creating-bean-with-name-eurekainstanceconfigbean-singleton-bean-creatio ? – Jeff Cook Dec 28 '18 at 14:23

10 Answers10

17

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

Kadiri
  • 288
  • 3
  • 9
  • 1
    You saved my life with this hint! I'm running Windows8 and first time i experienced a "detached" process (tomcat) that was not listed anywhere in TaskManager (although my Tomcat8 is running as a service). I opened VisualVM and it showed a tomcat instance/pid which i killed with "taskkill /pid 10752 /f" (f switch =force). Shitty M$ – Mariusz Jun 16 '14 at 23:26
  • 1
    Happy for you dear friend – Kadiri Jun 17 '14 at 13:56
7

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..

Lucky
  • 16,787
  • 19
  • 117
  • 151
  • 7
    I found out the issue is not with JAVA_HOME but that may be true. My issue is caused due to a failure in the test case and the scheduler got shutdown and later bean is being accessed. – Kathir Feb 22 '13 at 09:28
  • 7
    Link seems dead, which is why usually refering to answer outside this site, is bad practice – Anders Metnik Sep 17 '20 at 08:11
  • @AndersMetnik Updating the `JAVA_HOME` and JDK to 1.7 fixed the issue. Which was there in the link that I referred as well. :) – Lucky Sep 17 '20 at 12:20
  • as @Kathir mentioned its not JAVA_HOME issue. There is a case in spring integration test where context shutdown is triggered wherein some thread based execution tries to access beans. – ScanQR Jan 26 '21 at 05:36
  • 2
    The link provided by `http://forums.alfresco.com/forum/installation-upgrades-configuration-integration/installation-upgrades/singleton-bean-creation` can not be opened. – Jeff Tian May 12 '21 at 03:09
5

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

adramazany
  • 625
  • 9
  • 15
3

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

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

Dherik
  • 17,757
  • 11
  • 115
  • 164
2

@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

Dherik
  • 17,757
  • 11
  • 115
  • 164
Sumit Sundriyal
  • 815
  • 1
  • 11
  • 14
  • Exactly how did `@Transactional` resolve your problem? Where did you place this annotation? – Arthur Nov 29 '22 at 14:29
  • @Arthur If I remember correctly then the problem was that my test case was not able to create bean due to the unavailability of transaction. I think i started the trasaction from the test case. – Sumit Sundriyal Nov 30 '22 at 17:19
1

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

bpjoshi
  • 1,091
  • 16
  • 23
0

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.

Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
FishingIsLife
  • 1,972
  • 3
  • 28
  • 51
0

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 :)

0

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.

kkochanski
  • 2,178
  • 23
  • 28
0

I have really no idea what is going on ! I'm running a project remotely through VScode on an Ubuntu server. Everything worked normally until I got this error without touching it.

What I did is close the remote VScode and connect it again. Boom! it's working again!

I know it might not help to answer this question. Just want to share my weird experience on this error by myself if someone come and see this.

Hikaru Shindo
  • 2,611
  • 8
  • 34
  • 59