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

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 '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
  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 '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
  3. 0

    Spring code-based configuration - IllegalArgumentException: A ServletContext is required

    Stack Overflow | 3 years ago | nikopol86
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.HandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping()] threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Spring code-based configuration - IllegalArgumentException: A ServletContext is required

    qnundrum.com | 8 months ago
    org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
  6. 0

    why cant i get spring load time weaving to work

    Stack Overflow | 3 years ago | WILLIAM WOODMAN
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver' defined in class path resource [org/springframework/context/annotation/LoadTimeWeavingConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.instrument.classloading.LoadTimeWeaver org.springframework.context.annotation.LoadTimeWeavingConfiguration.loadTimeWeaver()] threw exception; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar

  1. dafman 6 times, last 2 months ago
  2. fima 4 times, last 2 months ago
  3. asdasd 6 times, last 2 months ago
  4. qavid 14 times, last 4 months ago
  5. Bugger 5 times, last 7 months ago
3 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 '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()
  2. Spring Beans
    ConstructorResolver.instantiateUsingFactoryMethod
    1. org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:413)
    1 frame