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

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);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章