SpringBoot+Druid启动报错Failed to configure a DataSource: 'url' attribute is not specified

报错信息:

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).


查遍了所有可能的原因,最后是因为springboot数据源用了Druid,SpringBoot默认是不支持Druid数据源的,需要手动初始化DruidDataSource对象

解决方法:

@Configuration
public class DataSourceConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(env.getProperty("spring.datasource.main.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.main.username"));
        dataSource.setPassword(env.getProperty("spring.datasource.main.password"));
        return dataSource;
    }
}

你可能感兴趣的:(SpringBoot)