问题

Description:

Failed to auto-configure a DataSource: ‘spring.datasource.url’ is not
specified and no embedded datasource could be auto-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、启动时加载本不需要加载的数据源

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//去掉数据源
public class SpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class,args);
    }
}

2、有数据源依旧报错,配置文件没有加载成功

可以尝试在pom.xml文件的build标签中加入如下内容:
<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*</include>
                    <include>*/*</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M1</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <testIncludes>
                        <testInclude>none</testInclude>
                    </testIncludes>
                    <compilerArguments>
                        <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

3、非上述两种情况

  • 查看build后target–classes中是否存在*.yml文件,如果不存在,可以直接从项目中将此类配置文件copy至此,再运行即可。
  • 出现这种build后,target–classes不存在yml配置文件,请再仔细check pom.xml文件是否出现包冲突或者yml文件内容格式是否正确。
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐