SpringBoot—— @ComponentScan

SpringBoot在寫啓動類的時候若是不使用@ComponentScan指明對象掃描範圍,默認指掃描當前啓動類所在的包裏的對象,若是當前啓動類沒有包,則在啓動時會報錯:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package錯誤。java

由於啓動類不能直接放在main/java文件夾下(不然會報錯),必需要建一個包把它放進去或者使用@ComponentScan指明要掃描的包。代碼示例以下:code

@SpringBootApplication
@ComponentScan(basePackageClasses=MytestApplication.class)//掃描該類所在的包
//或者使用basePackage屬性直接指定包
public class MytestApplication {

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