current position:Home>Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path

Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path

2022-01-27 07:51:11 Archie_ java

SpringBoot Default package scanning mechanism and @ComponentScan Specify scan path details

SpringBoot Default package scanning mechanism

Marking the @Component and @Component Derived annotations such as @Controller,@Service,@Repository You can put the current Bean Add to IOC In the container . that SpringBoot How to know to scan @Component It's annotated .@ComponentScan The thing to do is to tell Spring Where to find bean

SpringBoot Default package scanning mechanism : Start with the package where the startup class is located , Scan all files under the current package and its child packages . We can verify it through the following tests .

Launch the app and access BannerController This controller , Directory structure as shown

 Insert picture description here

The result of the interview is normal

 Insert picture description here

When put BannerController Move to a higher level directory , The application can start normally

 Insert picture description here

However, the following error occurred when accessing the path just now , The code is unchanged , yes Controller scanning Not yet .

 Insert picture description here

actually SpringBoot It's through @ComponentScan scan . By default , Above the entry class @SpringBootApplication There's a @ComponentScan, Which is equivalent to @ComponentScan Marked on the entry class .

So by default , Scan all files under the same level and its child packages of the entry class . When we want to make our own package scanning path, we need to add one @ComponentScan

@ComponentScan Use

Meaning of common parameters

  • basePackages And value: Used to specify the path of the package , scan ( Default parameters )
  • basePackageClasses: Used to specify the path of a package of a class to scan
  • includeFilters: Filter conditions included
  • FilterType.ANNOTATION: Filter by comment
  • FilterType.ASSIGNABLE_TYPE: According to the given type
  • FilterType.ASPECTJ: Use ASPECTJ expression
  • FilterType.REGEX: Regular
  • FilterType.CUSTOM: Custom rule
  • excludeFilters: Filter conditions excluded , Usage and includeFilters equally
  • nameGenerator: bean The name of the generator
  • useDefaultFilters: Do you want to turn it on @Component,@Repository,@Service,@Controller Class to detect

Specify packages to scan

The above example , If you want to scan the upper level package of the startup class , Use @ComponentScan Specify the package scan path , Can be BannerController Add to container

@SpringBootApplication
@ComponentScan("com.lin")
public class MissyouApplication {
    
    public static void main(String[] args) {
    
        SpringApplication.run(MissyouApplication.class, args);
    }
}

excludeFilters Exclude scanning of some packages

Test class preparation :

@Controller
public class BannerController {
    
    BannerController(){
    
        System.out.println("Hello BannerController");
    }
}
--------------------------------------------------------------------
@Service
public class TestService {
    
    TestService(){
    
        System.out.println("Hello TestService");
    }
}

The directory structure is as follows :

 Insert picture description here

Add... To the startup class @ComponentScan Specify scan lin This package does not exclude @Controller The class marked by this annotation

@SpringBootApplication
@ComponentScan(value = "com.lin",
        excludeFilters = {
    @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {
    Controller.class})})
public class MissyouApplication {
    
    public static void main(String[] args) {
    
        SpringApplication.run(MissyouApplication.class, args);
    }
}

Start the application , The console printed out TestService But not BannerController

 Insert picture description here

@Component And @ComponentScan

Use... On a class @Component annotation , Indicates that when a class needs to be created , The annotated class is a candidate class . It's like a classmate raising his hand . @ComponentScan Used to scan the classes under the specified package . It's like seeing which hands have been raised .




springboot Solution to the problem of multi module package scanning

Problem description :

springboot Build multiple modules , When one module needs to use the services of another module , Components that need to be injected into another module , As an example in the figure below :

 Insert picture description here

 Insert picture description here

memberservice Module MemberServiceApiImpl Class needs injection common Module RedisService Components , How to inject ?

solve :

stay memberservice Add... To the startup class of the module RedisService Component scanning of the full path of the package where the class is located , Just like this. :

 Insert picture description here

Note the annotation above the startup class @ComponentScan(basePackages={“com.whu.commom.redis”}), This sentence has actually been added RedisService Component scan of , But there's a problem with that , I found that the service cannot be accessed normally after startup . After searching the data, I found that it was because @ComponentScan and @SpringBootApplication There is a conflict in the package scanning of annotations ,@ComponentScan Annotation package scanning will overwrite @SpringBootApplication A bag scan of . The solution is in @ComponentScan(basePackages={“com.whu.commom.redis”}) Plus @SpringBootApplication Scanned bag , that @SpringBootApplication Which packages were scanned ? actually , By default, it scans the package where the startup class is located and its sub packages , So my example needs to be changed to @ComponentScan(basePackages={“com.whu.commom.redis”,“com.whu.memberservice”}). OK , end !!

copyright notice
author[Archie_ java],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270751002012.html

Random recommended