Spring Boot的@Service和@Autowired和@ComponentScan註解
SpringBoot項目的Bean裝配默認規則是根據Application類(SpringBoot項目入口類)所在的包位置從上往下掃描,即只掃描該類所在的包及其子包。
1.如果需要掃描其上層包中的類,則將其移動到其上層包中
2.使用@ComponentScan註解,在SpringBoot啓動類上註解配置,需要掃描的包。
SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}