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

Spring JIRA | Jim Moore | 9 years ago
  1. 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
  2. 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
  3. 0

    Class Loading Configuration (JBoss/WildFly forum at JavaRanch)

    coderanch.com | 1 year ago
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'casesAutoFormController' defined in ServletContext resource [/WEB-INF/mvc-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.actimize.cases.controller.autoforms.CasesAutoFormController]: Constructor threw exception; nested exception is : loader constraints violated when linking javax/xml/namespace/NamespaceContext class
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    New Spring/BlazeDS Integration Test Drive | Christophe Coenraets

    coenraets.org | 2 months ago
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter’: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/commons/EmptyVisitor
  6. 0

    @Value annotation & NullPointerException

    Stack Overflow | 3 years ago | Unguis Prime
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wartosciParametruGridBean' defined in file [...]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [pl.sygnity.cbop.admin.web.beans.WartosciParametruGridBean]: Constructor threw exception; nested exception is java.lang.NullPointerException

  1. fima 4 times, last 2 months ago
  2. tvrmsmith 4 times, last 4 months ago
  3. tfr 3 times, last 4 months ago
  4. aldrinleal 5 times, last 4 months ago
  5. thiago.santos 11 times, last 6 months ago
5 more registered users
2 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 '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()
  2. Spring Beans
    AbstractAutowireCapableBeanFactory.instantiateBean
    1. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:813)
    1 frame