Error creating bean with name ‘dataSourceScriptDatabaseInitializer’ defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method ‘dataSourceScriptDatabaseInitializer’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in class path resource
Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource
我的报错原因是我在初次学习spring Data Jpa的时候利用@SpringbootTest自动注入的时候出错.
@SpringBootTest
//springbootTest默认不会开启服务器,
public class MainTest {
@Autowired
UserRepository userRepository;
@Test
void t1(){
System.out.println(userRepository);
}
}
我在网上找了好几个小时,并且进行了调试,发现DataSource总是为空,因为当时是在Test方法中测试运行的,给出的错误总是如上面一样.但是我明明已经在application.yml中配置了database,后来整体进行调试的时候才发现,Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader,然后,然后就加上了依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
当然也有可能是因为其他的错误,如果你们也遇到了这个错误,可以看看自己的依赖上是否添加了mysql的依赖.
|