0

I am trying to add a new Spring bean to one of my projects. The bean is defined and created in another package like so:

@Configuration
public class UtilityBeans {
    public static String MY_BEAN_NAME = "my.bean.name";

    @Bean(name = MY_BEAN_NAME)
    public MyUtilBeanClass getMyBeanClass() {
        return new MyUtilBeanClass();
    }
}

I use it in my other package like this:

@Configuration
@Import({
    UtilityBeans.class
)}
...
    @Resource(name = UtilityBeans.MY_BEAN_NAME) 
    private MyUtilBeanClass myUtilBeans;

During runtime I get:

ERROR Caused by: org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'my.bean.name': 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!) ...

The logs do not give me any useful information as the stack trace is all in Spring library. How can I find out what failed? Is it incorrect usage of resource or is it that the bean creation itself is wrong?

I am using Spring-4 with JDK8.

Niru
  • 1,407
  • 1
  • 28
  • 47

1 Answers1

0

The issue here was that the bean was being created in 2 different points in my spring configuration due to some refactoring and the fix was to remove duplicate code. I had the same bean creation code:

@Bean(name = MY_BEAN_NAME)
public MyUtilBeanClass getMyBeanClass() {
    return new MyUtilBeanClass();
}

... in another class I had half way refactored. In this case my mistake was that I did not grep across all the log files being generated. The exceptions were being split and then buried into 2 different logs, one for a server start up and one for application runtime. The above exception was being posted to the application log. The other logs contained the relevant exception which stated that duplicate bean creation failed and the fix was to remove duplicate code.

Niru
  • 1,407
  • 1
  • 28
  • 47