Problem Description
After running Spring Boot project, the following error:
*************************** 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).
Or when the value can not be imported @Value annotation appears,
Could not resolve placeholder 'elasticsearch.host' in value "${elasticsearch.host}"
You can use the following solution
Problems and Solutions
Cause of the problem: Mybatis not find a suitable load classes, in fact, most of the spring - datasource - url is not loaded successfully, analyze the reasons as follows.
-
DataSourceAutoConfiguration be loaded automatically.
-
Is not configured datasource - - url spring property.
-
spring - datasource - url address configured format issue.
-
Configuring spring - datasource - url of the file is not loaded.
Internet gives these types of solutions.
Option One (resolve Cause 1)
Autoconfig exclude this class. After starting to run properly.
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
Option II (resolve Cause 2)
Did not add the database configuration information in application.properties/ or application.yml file.
spring: datasource: url: jdbc:mysql://localhost:3306/read_data?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver
Option Three (3 to address the causes)
Referenced database address in the spring xml configuration file so it is necessary to: such as escaping but does not need to escape in application.properties/ or application.yml files, error and the correct way to write the below.
# Error example spring.datasource.url = jdbc: MySQL \: // 192.168.0.20 \: 1504 / = to true f_me setUnicode & characterEncoding = utf8? # Correct example spring.datasource.url = jdbc: MySQL: // 192.168.0.20:1504 / f_me? setUnicode = true & char
Option IV (to address the causes 4)
To file pom <build> </ build> add the following. Yml or properties to ensure that the file can be scanned and properly loaded successfully. (Intellij idea running, not scan src folder (java folder) arranged inside file)
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> </includes> <filtering>false</filtering> </resource> </resources>