org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'contactRepository' is defined

Spring JIRA | Jim Moore | 9 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

    @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.NoSuchBeanDefinitionException: No bean named 'contactRepository' is defined
  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.NoSuchBeanDefinitionException: No bean named 'contactRepository' is defined
  3. 0

    [SJC-56] No indication why @Configuration object does not configure anything - Spring JIRA

    spring.io | 1 year ago
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'contactRepository' is defined
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    NoSuchBeanDefinitionException: No bean named 'org.springframework.security.filterChains

    Stack Overflow | 2 years ago | Jamie White
    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [oauth.xml]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.security.filterChains' is defined
  6. 0

    OnBeanCondition FactoryBean considering causes NoSuchBeanDefinitionException

    GitHub | 3 years ago | twicksell
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'EurekaInjectionManager' is defined

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

      No bean named 'contactRepository' is defined

      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition()
    2. Spring Beans
      DefaultListableBeanFactory.getBeanDefinition
      1. org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:378)
      1 frame