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

Spring JIRA | Stepan Koltsov | 6 years ago
tip
Your exception is missing from the Samebug knowledge base.
Here are the best solutions we found on the Internet.
Click on the to mark the helpful solution and get rewards for you help.
  1. 0

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

    Spring JIRA | 6 years ago | 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
  2. 0

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

    Spring JIRA | 6 years ago | 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

    Root Cause Analysis

    1. java.lang.RuntimeException

      must not be created because it is prototype

      at ru.yandex.commune.junk.stepancheg.spring.scope.Lamp.<init>()
    2. ru.yandex.commune
      Main$Conf$$EnhancerByCGLIB$$f6643148$$FastClassByCGLIB$$1b831bfa.invoke
      1. ru.yandex.commune.junk.stepancheg.spring.scope.Lamp.<init>(Lamp.java:8)
      2. ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf.lamp(Main.java:14)
      3. ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148.CGLIB$lamp$0(<generated>)
      4. ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148$$FastClassByCGLIB$$1b831bfa.invoke(<generated>)
      4 frames
    3. IDEA
      MethodProxy.invokeSuper
      1. net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215)
      1 frame
    4. Spring Context
      ConfigurationClassEnhancer$BeanMethodInterceptor.intercept
      1. org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:253)
      1 frame
    5. ru.yandex.commune
      Main$Conf$$EnhancerByCGLIB$$f6643148.lamp
      1. ru.yandex.commune.junk.stepancheg.spring.scope.Main$Conf$$EnhancerByCGLIB$$f6643148.lamp(<generated>)
      1 frame
    6. Java RT
      Method.invoke
      1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      4. java.lang.reflect.Method.invoke(Method.java:597)
      4 frames
    7. Spring Beans
      DefaultListableBeanFactory.preInstantiateSingletons
      1. org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:149)
      2. org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:570)
      3. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:983)
      4. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:879)
      5. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
      6. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      7. org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
      8. org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
      9. org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
      10. org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      11. org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
      11 frames
    8. Spring Context
      AnnotationConfigApplicationContext.<init>
      1. org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
      2. org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
      3. org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
      3 frames
    9. ru.yandex.commune
      Main.main
      1. ru.yandex.commune.junk.stepancheg.spring.scope.Main.main(Main.java:20)
      1 frame