SoFunction
Updated on 2025-03-04

Detailed explanation of the use of scan annotations in SpringBoot

In Spring Boot, scanning annotations refers to using annotations to tell the Spring framework which packages, classes or specific components should be scanned and managed as beans in Spring containers. Spring Boot mainly implements automatic scanning through the following annotations:

  • @ComponentScan
  • @SpringBootApplication
  • @Component
  • @Service
  • @Repository
  • @Controller

The purpose of these annotations is to tell the Spring container which classes to scan for and register them as Spring Beans.

1. @SpringBootApplication Annotation

@SpringBootApplicationIt is a combination annotation that contains three important annotations:

  • @Configuration: Indicates that this class is a Spring configuration class, equivalent toor@Configuration
  • @EnableAutoConfiguration: Enable Spring Boot's automatic configuration mechanism.
  • @ComponentScan: It is usually automatically applied on the startup class.@ComponentScanAnnotation, specify the location of the Spring Boot scan package.

Usually, you just need to use@SpringBootApplicationAnnotation is enough, it will automatically enable component scanning.

Case: @SpringBootApplication Startup Class

import ;
import ;

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        (, args);
    }
}

In this example,@SpringBootApplicationIt will be automatically enabled fromMyApplicationComponent scans of the package where the class resides and its subpackages.

2. @ComponentScan Annotation

@ComponentScanAnnotations are the basic annotations of Spring, used to specify the packages scanned by Spring containers. If you don't use it@SpringBootApplication, can be used directly@ComponentScanTo manually specify the scanned package.

Case: Manual configuration @ComponentScan annotation

import ;
import ;

@Configuration
@ComponentScan(basePackages = "")  // Specify the scan packagepublic class AppConfig {
}

In this case, the Spring container will only scan all components in the package.

3. @Component, @Service, @Repository, @Controller annotation

These annotations are marked with different types of Spring Beans. @Component is a general annotation, while @Service, @Repository, and @Controller are its specialized versions, which are used to annotate components of the service layer, data access layer and controller layer respectively.

  • @Component: Tag a common Spring Bean.
  • @Service: Beans used to mark the service layer.
  • @Repository: Beans used to mark the data access layer.
  • @Controller: Beans used to mark the web layer (Spring MVC controller).

When these annotations are marked on the class, Spring will automatically register them as beans in the container and perform dependency injection.

Case: Use @Component and other special annotations

import ;
import ;
import ;
import ;

@Component
public class MyComponent {
    public void doSomething() {
        ("doSomething!");
    }
}

@Service
public class MyService {
    public void performService() {
        ("performService...");
    }
}

@Repository
public class MyRepository {
    public void saveData() {
        ("Saving data...");
    }
}

@Controller
public class MyController {
    public void handleRequest() {
        (" request...");
    }
}

In this example,MyComponentMyServiceMyRepositoryandMyControllerThey will be automatically scanned and registered as beans by Spring containers.

4. Spring Boot Automatically configure scanning

In Spring Boot, many functions (such as database connections, web configuration, etc.) are implemented through automatic configuration. Spring Boot automatically configures related functions based on dependencies in the classpath. This automatic configuration scan is also done through @ComponentScan and @EnableAutoConfiguration.

For example, if your project contains spring-boot-starter-web dependencies, Spring Boot will automatically enable relevant web configurations (such as those of embedded Tomcat) and scan the @Controller annotated class.

5. Component scan range

By default, Spring Boot starts scanning from the package and its subpackage where the main application class (usually a class marked with the @SpringBootApplication annotation). If you need to change the scope of the scan, you can specify other packages via @ComponentScan.

Example: Customize the scope of the scan package

import ;
import ;

@SpringBootApplication
@ComponentScan(basePackages = "")  // Customize the scan packagepublic class MyApplication {
    public static void main(String[] args) {
        (, args);
    }
}

In this example, Spring will scanAll in the package and its subpackage@Component@Service@Repository@ControllerClasses with annotations.

Summarize

  • @SpringBootApplication: Enable automatic configuration, configuration class, and component scanning.
  • @ComponentScan: Custom scanned package or class.
  • @Component@Service@Repository@Controller: Different types of Spring Bean annotations.
  • Automatic configuration: Spring Boot automatically scans for dependencies in the classpath and automatically configures related components.

These annotations help developers easily manage beans in Spring containers through scanning and auto-assembly, without manually registering each bean, making the development process more concise and efficient.

This is the end of this article about the detailed explanation of the use of scanning annotations in SpringBoot. For more relevant content on the use of scanning annotations in SpringBoot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!