1.不能直接放在main/java文件夹下;
2.启动类所在的包是最顶部的包。(ylyangApplication.java即为启动类)
springBoot默认包扫描机制:默认扫描启动类所在的包同级文件和子包下的文件。可以自己指定springboot启动默认要扫描的包,使用注解
@ComponentScan(basePackages = "com.ylyang.learning.app.controller.")
否则启动直接报错:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/liuze/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.0.1.RELEASE/spring-boot-autoconfigure-2.0.1.RELEASE.jar!/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration due to org/springframework/dao/DataAccessException not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
原因:
@SpringBootApplication
启动类上的注解点进去,可以注意 @ComponentScan 这个注解。@ComponentScan 这个注解的作用是告诉spring,哪里可以找到bean,如果在启动类没有配置这个注解,则默认扫描启动类所在的包的同级目录和它的子包,也可以配置具体路径扫描。 基于这个作用所以不能把启动类放在Java文件下,首先Java文件不是一个包,其次它是一个资源文件。故只要一启动就会报错!!!