Why Your SpringBoot Components Aren’t Initialized and How to Fix It

This article explains why SpringBoot fails to initialize components after extracting common modules due to mismatched package names, demonstrates how to use @ComponentScan and custom @Enable annotations to specify scan paths, and outlines the basics of creating a custom starter for automated configuration.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Why Your SpringBoot Components Aren’t Initialized and How to Fix It

Problem Cause

Spring Boot scans the package of the main application class and its sub‑packages. When the main class resides in com.abc but a common library defines beans in com.def, those beans are invisible to the container, causing initialization failure.

Extended Scenarios

If the main class is placed in a deeper package, components in its parent packages are not scanned, which often leads to startup errors for beginners.

Conversely, you may deliberately keep certain classes outside the scan path; in such cases you can use @ComponentScan with exclusion filters.

Using @ComponentScan

The simplest fix is to declare the packages to scan explicitly. @SpringBootApplication already includes @ComponentScan, so you can provide the desired base packages directly.

@SpringBootApplication
@ComponentScan({"com.abc.xx", "com.def.xx"})
public class SpringBootMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMainApplication.class, args);
    }
}

List the business package first, then the common package. Omit the second entry if the common beans are not required.

Custom Enable Annotation

To avoid hard‑coding package names, create a meta‑annotation that imports a configuration class scanning the common package.

@Component
@ComponentScan("com.def.xx")
public class CommonConfig {
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(CommonConfig.class)
public @interface EnableCommon {
}
@EnableCommon
@SpringBootApplication
public class SpringBootMainApplication {
    // ...
}

If CommonConfig is already reachable by Spring Boot’s default scan, the custom annotation is unnecessary.

Custom Starter Overview

Spring Boot’s auto‑configuration starters can be packaged as a custom starter to encapsulate the common module’s setup.

Add the auto‑configuration dependency in the starter’s pom.xml:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
  </dependency>
</dependencies>

Implement a service (e.g., HelloWorldService) and a properties class annotated with @ConfigurationProperties. Provide an auto‑configuration class that is conditional on the presence of the service.

Register the auto‑configuration in META-INF/spring.factories:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.secbro.HelloWorldAutoConfiguration

When other projects include this starter, Spring Boot automatically configures and exposes HelloWorldService without additional code.

Conclusion

Use @ComponentScan to resolve package‑visibility problems; a custom meta‑annotation offers a reusable way to group scan settings; and a custom starter provides a higher‑level, automated configuration mechanism. Keep the scan path simple by avoiding multiple top‑level packages in the same project.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaSpringBootComponentScanCustomAnnotationStarter
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

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.