SoFunction
Updated on 2025-04-04

SpringBoot @ComponentScan Scan Limitations

Limitations of SpringBoot @ComponentScan Scan

When using @ComponentScan annotation, Spring only injects the set class or package and subset objects of the package.

This will cause the original @SpringBootApplication auto-configuration function not to inject the current project during object injection.

@ComponentScan

Scan the dependency injection module service [Note that the scan of this project @ComponentScan must manually join the package scan path of the current project]

package ;
 
import ;
import ;
import ;
import ;
import ;
import ;
import ..EnableSwagger2;
 
/**
  * Enable asynchronous request
  */
@EnableAsync
/**
  * Turn on interface cache
  */
@EnableCaching
/**
  * Turn on scheduled task scheduling
  */
@EnableScheduling
/**
  * Open interface document description
  */
@EnableSwagger2
/**
  * Scan the dependency injection module service [Note that the scan of this project @ComponentScan must manually add the package scan path of the current project]
  */
@ComponentScan(basePackages = {"", "", "", ""})
/**
  * @SpringBootApplication is equivalent to @Configuration, @EnableAutoConfiguration and @ComponentScan and has their default property values
  */
@SpringBootApplication
public class PatrolMobileServiceApplication {
 
    public static void main(String[] args) {
        (, args);
    }
 
}

@ComponentScan's limitations are obvious, only the configured packages or classes are scanned.

Annotation with @SpringbootApplication

It can solve the problem of too many annotations on the root class or configuration class (my own term, the class where main is). A @SpringbootApplication is equivalent to @Configuration, @EnableAutoConfiguration and @ComponentScan and has their default property values.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.