org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myFactory' defined in class path resource [myConfig.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [MyFactory]: Is it an abstract class?; nested exception is java.lang.InstantiationException

Spring JIRA | Andreas Thaler | 3 years ago
  1. 0

    Assumed you have a BeanFactory designed for extension (for example using lookup-method) {code} public abstract class MyFactory implements FactoryBean{ ... } {code} configured as abstract bean {code} <bean id="myFactory" abstract="true" class="MyFactory"> {code} then a call to {code}ApplicationContext.getBeansWithAnnotation(..){code} will try to instantiate the class, which is against the contract for the abstract flag of the configuration (Specify "true" to tell the bean factory to not try to instantiate that particular bean in any case.) As the class is an abstract class the call will fail with: {code} org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myFactory' defined in class path resource [myConfig.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [MyFactory]: Is it an abstract class?; nested exception is java.lang.InstantiationException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:767) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:698) at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:578) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAnnotationOnBean(DefaultListableBeanFactory.java:464) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansWithAnnotation(DefaultListableBeanFactory.java:449) at org.springframework.context.support.AbstractApplicationContext.getBeansWithAnnotation(AbstractApplicationContext.java:1182) at ... {code} Same scenario will work fine if class is not implementing FactoryBean, so it seems that for the special case of a FactoryBean the abstract flag is not repected.

    Spring JIRA | 3 years ago | Andreas Thaler
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myFactory' defined in class path resource [myConfig.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [MyFactory]: Is it an abstract class?; nested exception is java.lang.InstantiationException
  2. 0

    Assumed you have a BeanFactory designed for extension (for example using lookup-method) {code} public abstract class MyFactory implements FactoryBean{ ... } {code} configured as abstract bean {code} <bean id="myFactory" abstract="true" class="MyFactory"> {code} then a call to {code}ApplicationContext.getBeansWithAnnotation(..){code} will try to instantiate the class, which is against the contract for the abstract flag of the configuration (Specify "true" to tell the bean factory to not try to instantiate that particular bean in any case.) As the class is an abstract class the call will fail with: {code} org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myFactory' defined in class path resource [myConfig.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [MyFactory]: Is it an abstract class?; nested exception is java.lang.InstantiationException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:767) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:698) at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:578) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAnnotationOnBean(DefaultListableBeanFactory.java:464) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansWithAnnotation(DefaultListableBeanFactory.java:449) at org.springframework.context.support.AbstractApplicationContext.getBeansWithAnnotation(AbstractApplicationContext.java:1182) at ... {code} Same scenario will work fine if class is not implementing FactoryBean, so it seems that for the special case of a FactoryBean the abstract flag is not repected.

    Spring JIRA | 3 years ago | Andreas Thaler
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myFactory' defined in class path resource [myConfig.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [MyFactory]: Is it an abstract class?; nested exception is java.lang.InstantiationException
  3. 0

    GitHub comment 711#164033053

    GitHub | 12 months ago | timwebster9
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hystrixDemoApplication': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.oauth2.client.OAuth2RestTemplate uk.co.aquilauk.HystrixDemoApplication.restTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.oauth2.client.OAuth2RestTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Spring BeanCreationException : Bean instantiation via factory method failed; nested exception

    Stack Overflow | 7 months ago | ValentinD
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nickeedApplication': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.nickeed.ServiceInterface.UserServiceInterface com.nickeed.NickeedApplication.userServiceInterface; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.nickeed.repository.UserRepository com.nickeed.ServiceImpl.UserServiceImpl.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class com.nickeed.model.User
  6. 0

    @Configuration public abstract class JdbcConfiguration { @Bean public JdbcContactRepository contactRepository() { return new JdbcContactRepository(dataSource()); } @ExternalBean protected abstract DataSource dataSource(); } Doing this test @Test public void get_from_JCCtx() { ApplicationContext ctx = new JavaConfigApplicationContext(JdbcConfiguration.class); assertNotNull(ctx.getBean("contactRepository")); } gives: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'contactRepository' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:378) and this @Test public void get_from_CXCtx() { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/app-config.xml"); assertNotNull(ctx.getBean("contactRepository")); } gives org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'app.JdbcConfiguration#0' defined in class path resource [app-config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [app.JdbcConfiguration]: Is it an abstract class?; nested exception is java.lang.InstantiationException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:813) Which is odd because the documentation gives examples where the class is abstract because of @ExternalBean... Changing it so it's not external helps a little. get_from_CXCtx() now works fine, but get_from_JCCtx() gives org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactRepository' defined in Bean creation method contactRepository in class app.JdbcConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public final app.JdbcContactRepository app.JdbcConfiguration$$EnhancerByCGLIB$$2883abcc.contactRepository()] threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:413) After stepping through the code I finally tracked down the problem: ClassUtils.isConfigurationClass(..) returns false if the class is abstract and there are no *public* @ExternalBean methods. You can have protected @Bean methods, but apparently that's not legit for @ExternalBean or @ExternalValue. If the class is marked as being @Configurable or there are @Bean definitions, then it should also check to see if there are any non-public @ExternalBean or @ExternalValue methods and throw an exception if there are rather than silently ignoring them.

    Spring JIRA | 9 years ago | Jim Moore
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'app.JdbcConfiguration#0' defined in class path resource [app-config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [app.JdbcConfiguration]: Is it an abstract class?; nested exception is java.lang.InstantiationException

    1 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. org.springframework.beans.factory.BeanCreationException

      Error creating bean with name 'myFactory' defined in class path resource [myConfig.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [MyFactory]: Is it an abstract class?; nested exception is java.lang.InstantiationException

      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean()
    2. Spring Beans
      DefaultListableBeanFactory.getBeansWithAnnotation
      1. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
      2. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
      3. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:767)
      4. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:698)
      5. org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:578)
      6. org.springframework.beans.factory.support.DefaultListableBeanFactory.findAnnotationOnBean(DefaultListableBeanFactory.java:464)
      7. org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansWithAnnotation(DefaultListableBeanFactory.java:449)
      7 frames
    3. Spring Context
      AbstractApplicationContext.getBeansWithAnnotation
      1. org.springframework.context.support.AbstractApplicationContext.getBeansWithAnnotation(AbstractApplicationContext.java:1182)
      1 frame