Failed to configure a DataSource: ‘url’ attribute is not specified

当Springboot的spring.datasource.url没有配置的时候,抛出异常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。当在pom.xml文件中配置了spring-boot-starter-data-jpa依赖的时候,springboot会自动去配置数据源datasource(JDBC连接池),但是发现database url没有配置,这时候就会抛出上面的异常。

当使用基于内存的数据库(例如H2,Derby)的时候,如果不设置spring.datasource.url,是不会报错的,因为会有一个默认值。但是对于外部的数据库,例如Mysql, Oracle等,datasource配置的参数必须要指定, 否则就会抛出上面的异常。

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

解决方法:

1.如果你的springboot工程中没有使用数据库,那么可以从pom.xml文件中删除spring-boot-starter-data-jpa依赖。

2.springboot中的类DataSourceAutoConfiguration.java负责自动配置数据源和ORM(JPA,如果配置了的话),我们可以在springboot启动的时候排除这个类,不要自动配置。有以下2种方式。

  • 在springboot 启动类上加注解 -> @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
  • 在application.properties文件中加入配置 -> spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Root Cause:

JPA(Java persistence API)是ORM的一个java 规范,spring-boot-starter-data-jpa在springboot的上下文中自动配置ORM(当然包括datasource数据源)。

JPA的自动配置功能,会使用database url等信息配置数据源datasource,并且会尝试去连接数据库。当然了,ORM是在JDBC的基础上做了封装,使得用户能以面向对象的方式对数据库进行增删改查,所以需要database driver。用户应该在pom.xml文件中加入database driver依赖(包括基于内存的数据库),当然了如果使用外部数据库,例如Mysql, Oracle, SQL Server, Postgres, MongoDB等,则还需要提供database JDBC连接信息。

所以,可以通过配置database driver和database JDBC连接信息去解决这个异常"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."

1.对于关系型数据库 RDBMS Database:

Mysql Database:

1.pom.xml:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

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

2.application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/multi_datasource_test1?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

SQL Server Database:

1. pom.xml:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
	<groupId>com.microsoft.sqlserver</groupId>
	<artifactId>mssql-jdbc</artifactId>
	<scope>runtime</scope>
</dependency>

2. application.properties:
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=recycle
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

Oracle Database:

1. pom.xml:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
	<groupId>com.oracle</groupId>
	<artifactId>ojdbc7</artifactId>
	<version>12.1.0.1</version>
</dependency>

2. application.properties:
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:data
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

Postgres Database:

1.pom.xml:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
	<groupId>org.postgresql</groupId>
	<artifactId>postgresql</artifactId>
	<scope>runtime</scope>
</dependency>    

2. application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

 2.内存数据库:

配置内存数据库的时候,不需要提供JDBC连接信息。

H2 Database:
 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

Derby Database:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <scope>runtime</scope>
</dependency>


3.NoSQL Databases:

NoSQL数据库例如MongoDB,Redis需要JDBC连接信息。在pom.xml文件中加入相应的依赖,然后在application.properties文件中加入JDBC连接信息。

MongoDB:

1.pom.xml:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

2.application.properties:
spring.data.mongodb.uri=mongodb://root:rootpassword@localhost:27017/recycle

总结:

之所以会抛出这种异常,是因为开发者配置数据库信息的时候,缺少了database driver或者没有提供JDBC连接信息。

 

  • 11
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值