spring boot下非web应用,mysql数据源报错问题

spring boot 专栏收录该内容
0 篇文章 0 订阅

在非web应用下,spring boot启动时提示如下错误:

org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

spring boot启动时默认会自动配置数据源为org.apache.tomcat.jdbc.pool.DataSource。即使在配置文件或代码中指定了其他的数据源(如druid)。
解决方法是:在@SpringBootApplication注解中取消自动配置,如下:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ThisApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(ThisApplication.class);
    }
}

另外,pom.xml中加入依赖:

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
</dependency>

有时会出现ClassNotFoundException: com.mysql.jdbc.Driver。并且在IDEA Project Structure ->Modules ->Dependencies中有Maven:mysql:mysql-connector-java:unknown项。
解决在pom.xml指定mysql-connector-java的版本即可

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值