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.

  • @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.
    via by Jim Moore,
  • @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.
    via by Jim Moore,
  • Class Loading Configuration
    via by Guy Roth,
    • 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)

    Users with the same issue

    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    Tahir
    49 times, last one,
    Unknown visitor1 times, last one,
    6 more bugmates