Correctness32 - SpotBugsExtensionForSpringFrameWork/CS5098 GitHub Wiki

Correctness - Inject a bean that doesn't exist

Description

By far the most common cause of the BeanCreationException is Spring trying to inject a bean that doesn't exist in the context.

For example, BeanA is trying to inject BeanB:

@Component
public class BeanA {

    @Autowired
    private BeanB dependency;
    ...
}

If a BeanB is not found in the context, then the BeanCreationException will be thrown.

Theory

Solution

To diagnose this type of issue – first, make sure the bean is declared:

  • either in an XML configuration file using the element
  • or in a Java @Configuration class via the @Bean annotation
  • or is annotated with: @Component, @Repository, @Service, @Controller and classpath scanning is active for that package

Also check that the configuration files or classes are actually picked up by Spring and loaded into the main context.

⚠️ **GitHub.com Fallback** ⚠️