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);
    }
}
相关文章
相关标签/搜索