0

I have a bean that I am positive is in the package being scanned by spring (all its siblings work successfully).


@Service
class BeanB {

}

@Service
@DependsOn("BeanB")
class BeanA {

  @Autowired
  BeanB b;

}

However when I run it I get this:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'BeanB' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:682)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1218)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
    ... 34 more

However BeanB is present.

When I drill into the spring internals (DefaultListableBeanFactory beanDefinitionMap), it says that it has a bean named beanB. Why is spring not able to find and wire the correct @AutoWired (or @Inject bean, it appears the capitalization is off for the first letter?

I have looked at What is a NoSuchBeanDefinitionException and how do I fix it? but it doesn't seem to deal with the case of "my bean is in there but can't be found from capitalization"

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • The linked duplicate has a section named "Using wrong bean name" that explains how a bean is named and how to refer to it. – Savior Nov 02 '20 at 20:33
  • OK yeah that's it. It's too bad that googling "wrong capitaliztion" for bean didn't take me there. Hopefully this question won't get deleted, for followers... – rogerdpack Nov 02 '20 at 20:56

1 Answers1

0

Turns out it wasn't dying trying to find the right bean to autowire, the actual culprite was the @DependsOn but it sure threw me for a loop, so documenting it here.

Fix: use spring style class -> name convention:

@DependsOn("beanB")
class BeanA {

...
rogerdpack
  • 62,887
  • 36
  • 269
  • 388