Springboot + Maven multi module project startup Error: XxxService cannot be found

1. Problem background

Split springboot into multi module projects (split in different forms)

Split by hierarchy: one item for controller, one item for service, and one item for Dao (mapper).

Split by business: a business module is split into one project (including the controller, service, Dao, etc. of the business)

After the split is completed, start the original application and prompt application failed to start

2. Problem phenomenon

Description:

Field deptService in com.fanzyx.xx.controller.XxxController required a bean of type 'com.fanzyx.xx.service.XxxService' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.fanzyx.xx.service.XxxService' in your configuration.

3. Cause of problem

If the basic package path for scanning is not configured in the application, it will only be found under the current module

e.g.

@SpringBootApplication()
public class XxxApplication {
    public static void main(String[] args) {
        SpringApplication.run(XxxApplication.class,args);
    }
}

4. Solution

Add the scan base path in the @springbootapplication() annotation

// Multi-module projects need to be configured to scan for base packages, otherwise they will not be able to load entities from other modules
@SpringBootApplication(scanBasePackages = {"com.fanzyx.crane"})
public class XxxApplication {
    public static void main(String[] args) {
        SpringApplication.run(XxxApplication .class,args);
    }
}

Similar Posts: