Backend Development 10 min read

Common Spring Boot Bean Injection Issues and Their Solutions

This article explains typical Spring Boot bean injection failures—such as missing component scanning, multi‑module scanning gaps, incorrect @Qualifier/@Resource names, and injection in filters or listeners—and provides concrete configuration and code examples to resolve each problem.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Common Spring Boot Bean Injection Issues and Their Solutions

When a Spring Boot application starts, it automatically scans the package of the main class and its sub‑packages for beans. If a bean resides outside these packages, it will not be registered in the IOC container, leading to injection failures.

Case 1: Bean not scanned – For example, the main class is in com.training while utility classes are in com.utils . Since com.utils is not part of the default scan path, beans defined there are ignored. The fix is to add the extra package with @ComponentScan or the scanBasePackages attribute of @SpringBootApplication .

package com.training;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
// Add com.utils to the scan range
@ComponentScan(basePackages = {"com.training", "com.utils"})
@MapperScan(basePackages = "com.training.dao")
public class MusicMainClass {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(MusicMainClass.class, args);
    }
}

Alternatively, use @SpringBootApplication(scanBasePackages = {"com.training", "com.utils"}) :

package com.training;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

// Using scanBasePackages achieves the same effect
@SpringBootApplication(scanBasePackages = {"com.training", "com.utils"})
@MapperScan(basePackages = "com.training.dao")
public class MusicMainClass {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(MusicMainClass.class, args);
    }
}

Case 2: Multi‑module architecture bean not scanned – In a project where sub‑modules use the package pattern com.common.* while the main class is under com.training.* , beans in the sub‑module are also missed. The same @ComponentScan or scanBasePackages configuration resolves the issue.

Case 3: @Qualifier or @Resource name does not exist – Spring’s default bean naming follows the class name with a lower‑case first letter, unless the first two letters are uppercase. When multiple beans of the same type exist, @Autowired becomes ambiguous. Using @Qualifier("beanName") or @Resource(name="beanName") explicitly selects the desired bean.

public class Teacher {
    @Autowired
    private Student student;
}

XML configuration with two Student beans:

<bean id="student1" class="com.test.Student">
    <property name="name" value="zhangsan"/>
</bean>

<bean id="student2" class="com.test.Student">
    <property name="name" value="lisi"/>
</bean>

Resolving the ambiguity with @Qualifier :

public class Teacher {
    @Qualifier("student1")
    @Autowired
    private Student student;
}

@Resource injects by name first; if a name is specified, only that name is considered, otherwise the field name is used.

Case 4: Bean injection in interceptor, filter, or listener fails – In the Web MVC startup sequence ( context‑param → listener → filter → servlet → interceptor → … → bean instantiation ), beans are not yet created when a filter or interceptor tries to autowire them, resulting in null references. The solution is to obtain the bean via constructor injection in a configuration class or to manually retrieve it from the application context.

Example of constructor‑based injection (illustrated in the original screenshots):

Using IDEA to view the IOC container:

Run the Spring Boot application and click the Run (or Debug ) button.

Open the Console tab and click the Actuator link.

Select Beans and then Application to see all registered beans.

This visual tool helps quickly locate missing beans and diagnose injection problems.

By understanding the default scanning rules, bean naming conventions, and the correct use of @ComponentScan , @Qualifier , and @Resource , developers can avoid common pitfalls and ensure that all required beans are properly registered and injected.

BackendJavaSpringBootResourceQualifierComponentScanBeanInjection
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.