Springboot reported an error that the automatically injected service could not be found

Springboot multi-module project, sometimes it will appear, error can't find the automatically injected service

springboot Field xxxService in required a bean of type

The implementation class of each service interface is annotated with @service, but the service is still not found.

Finally, the investigation found that because the project is a multi-module, the startup class is not in the same package or sub-package, which makes it impossible to automatically inject

Insert picture description here
Insert picture description here

Solution:

1. Add scanning @ComponentScan("com.demo") to the startup class to scan the upper package com.demo

@SpringBootApplication
@EnableSwagger2
@MapperScan("com.demo.dao")
@ComponentScan("com.demo")
public class TaskApplication {

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

}

2. Adjust the position of TaskApplication startup class to the com.demo package
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_41003771/article/details/114684732