出现原因:学习Spring security过程中,整合springboot启动报错(采用了思路一解决)
错误消息如下:
***************************
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).
分析思路:
思路一、项目不需要连接数据库,启动报错
- 解决:在启动类中,将
@SpringBootApplication
修改为@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
,表示在启动的时候不需要连接数据库。
思路二、需要连接数据库,启动报错
- 方案一:修改yml配置文件,添加如下配置
#原因:在application.properties/或者application.yml文件中没有添加数据库配置信息.
spring:
datasource:
url: jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
- 方案二:修改
properties
文件
spring.datasource.url = jdbc:mysql://localhost:3306/user_db?setUnicode=true&characterEncoding=utf8
- 方案三:mysql的版本影响
# mysql8版本写法中多了个cj
driver-class-name: com.mysql.cj.jdbc.Driver
# mysql8的url也需要加入时区,参照如下
url: jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&tinyInt1isBit=false
#mysql8以下的版本,需要检查pom.xml文件中依赖的mysql jar包的版本
driver-class-name: com.mysql.jdbc.Driver
#mysql8以下的url写法
url: jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
- 方案四:项目没有加载到
yml
或properties
文件。例如:pom打包的是jar项目时,检查pom.xml
文件中的打包方式packaging
<packaging>jar</packaging>
如果pom
指定使用jar
打包方式,系统不会自动读取yml
或properties
文件,需要手动进行配置
<!--build放在</dependencies>标签的后面,主要加入的是resources标签 -->
<!--resources标签用来控制系统启动的时候能够读取到这些后缀的文件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>lib</directory>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
</build>
- 方案五:若项目中使用了SpringCloud+Nacos,启动项目时需要在【Program arguments】中手动指定【–spring.profiles.active=test】,那么在resource文件夹下就必须要有bootstarp-test.yml或者application-test.yml文件,同时配置文件中nacos地址里面也必须配置对应的命名空间和对应服务名称的yml文件。
原文链接:https://blog.csdn.net/renkai721/article/details/112257894