Spring Boot的@Service和@Autowired和@ComponentScan註解

Spring Boot的@Service和@Autowired和@ComponentScan註解java

SpringBoot項目的Bean裝配默認規則是根據Application類(SpringBoot項目入口類)所在的包位置從上往下掃描,即只掃描該類所在的包及其子包。code

1.若是須要掃描其上層包中的類,則將其移動到其上層包中io

2.使用@ComponentScan註解,在SpringBoot啓動類上註解配置,須要掃描的包。class

SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
相關文章
相關標籤/搜索