org.springframework.beans.factory.BeanCreationException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • {code} @Scope("prototype") public class Lamp { public Lamp() { throw new RuntimeException( "must not be created because it is prototype"); } } public class Main { @Configuration public static class Conf { @Bean public Lamp lamp() { return new Lamp(); } } public static void main(String[] args) { new AnnotationConfigApplicationContext(Conf.class); System.out.println("must reach this"); } } {code} This code causes {code} Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lamp' defined in class ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public ru.yandex.commune.junk.stepancheg.spring.scope.Lamp ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp()] threw exception; nested exception is java.lang.RuntimeException: must not be created because it is prototype at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:983) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:879) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73) at ru.yandex.commune.junk.stepancheg.spring.scope.Main.main(Main.java:20) Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public ru.yandex.commune.junk.stepancheg.spring.scope.Lamp ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp()] threw exception; nested exception is java.lang.RuntimeException: must not be created because it is prototype at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:169) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:570) ... 13 more Caused by: java.lang.RuntimeException: must not be created because it is prototype at ru.yandex.commune.junk.stepancheg.spring.scope.Lamp.<init>(Lamp.java:8) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp(Main.java:14) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148.CGLIB$lamp$0(<generated>) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148$$FastClassByCGLIB$$1b831bfa.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:253) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148.lamp(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:149) ... 14 more {code} because Spring ignores type-level @Scope annotation for explicitly declared bean with @Bean-annotated method. This is documented behavior, so this is an enhancement request, not bug. Spring cannot find actual type of bean until it calls @Bean method, however in the most cases it is sufficient to analyze return type of @Bean-annotated method. @Scope should be allowed as type-level annotation to avoid misuse of "library" classes: if library class is intended to be request-scoped, and library class is used as @Bean, developer may forget to add @Scope annotation to @Bean method, and this bug may be hard to find. Possibility to annotate class with @Scope solves the problem.
    via by Stepan Koltsov,
  • {code} @Scope("prototype") public class Lamp { public Lamp() { throw new RuntimeException( "must not be created because it is prototype"); } } public class Main { @Configuration public static class Conf { @Bean public Lamp lamp() { return new Lamp(); } } public static void main(String[] args) { new AnnotationConfigApplicationContext(Conf.class); System.out.println("must reach this"); } } {code} This code causes {code} Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lamp' defined in class ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public ru.yandex.commune.junk.stepancheg.spring.scope.Lamp ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp()] threw exception; nested exception is java.lang.RuntimeException: must not be created because it is prototype at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:983) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:879) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73) at ru.yandex.commune.junk.stepancheg.spring.scope.Main.main(Main.java:20) Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public ru.yandex.commune.junk.stepancheg.spring.scope.Lamp ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp()] threw exception; nested exception is java.lang.RuntimeException: must not be created because it is prototype at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:169) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:570) ... 13 more Caused by: java.lang.RuntimeException: must not be created because it is prototype at ru.yandex.commune.junk.stepancheg.spring.scope.Lamp.<init>(Lamp.java:8) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp(Main.java:14) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148.CGLIB$lamp$0(<generated>) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148$$FastClassByCGLIB$$1b831bfa.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:253) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148.lamp(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:149) ... 14 more {code} because Spring ignores type-level @Scope annotation for explicitly declared bean with @Bean-annotated method. This is documented behavior, so this is an enhancement request, not bug. Spring cannot find actual type of bean until it calls @Bean method, however in the most cases it is sufficient to analyze return type of @Bean-annotated method. @Scope should be allowed as type-level annotation to avoid misuse of "library" classes: if library class is intended to be request-scoped, and library class is used as @Bean, developer may forget to add @Scope annotation to @Bean method, and this bug may be hard to find. Possibility to annotate class with @Scope solves the problem.
    via by Stepan Koltsov,
    • org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lamp' defined in class ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public ru.yandex.commune.junk.stepancheg.spring.scope.Lamp ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp()] threw exception; nested exception is java.lang.RuntimeException: must not be created because it is prototype at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:983) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:879) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73) at ru.yandex.commune.junk.stepancheg.spring.scope.Main.main(Main.java:20) Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public ru.yandex.commune.junk.stepancheg.spring.scope.Lamp ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp()] threw exception; nested exception is java.lang.RuntimeException: must not be created because it is prototype at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:169) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:570) ... 13 more Caused by: java.lang.RuntimeException: must not be created because it is prototype at ru.yandex.commune.junk.stepancheg.spring.scope.Lamp.<init>(Lamp.java:8) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp(Main.java:14) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148.CGLIB$lamp$0(<generated>) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148$$FastClassByCGLIB$$1b831bfa.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:253) at ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148.lamp(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:149) ... 14 more
    No Bugmate found.