错误描述:我在springCloud项目下创建了一个子项目,启动子项目一直报下面的错误。
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Disconnected from the target VM, address: '127.0.0.1:61897', transport: 'socket'
Process finished with exit code 0
原因:
当我们的项目下面有HSQLDB的依赖时,Spring Boot会创建默认的内存数据库的数据源DataSource,如果你自己创建了DataSource,Spring Boot就不会创建默认的DataSource。
解决办法:
1、在启动类上面加上下面这个注解。表示让springboot不要根据maven中依赖自动配置了。
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2、自己写一个DataSource配置类
@Configuration
public class DruidDataSourceConfig {
@ConfigurationProperties(prefix="spring.datasource.druid")
@Bean(initMethod="init", destroyMethod="close")
public DruidDataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setProxyFilters(org.assertj.core.util.Lists.newArrayList(statFilter()));
return dataSource;
}
@Bean
public Filter statFilter(){
StatFilter statFilter = new StatFilter();
statFilter.setSlowSqlMillis(1); // 慢SQL
statFilter.setLogSlowSql(true);// 打印慢sql日志
statFilter.setMergeSql(true);
return statFilter;
}
@SuppressWarnings("rawtypes")
@Bean
public ServletRegistrationBean servletRegistrationBean(){
return new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
}
}