未配置Datasource时, 启动 SpringBoot 程序报错的问题

SpringBoot will show error if there is no datasource configuration in application.yml/application.properties

221229 11:14:44906     main W bServerApplicationContext#591 Exception encountered during context initialization ... nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
...
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).

Because on start up SpringBoot will attempt to auto initialise a datasource, to run a spring boot application without datasource. You must disable the auto configuration for the datasource and may be for JPA also by adding exclude = {...} to the main entry.

Method #1

@SpringBootApplication
@EnableAutoConfiguration(exclude = {
    DataSourceAutoConfiguration.class,
    DataSourceTransactionManagerAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(PayPalApplication.class, args);
    }
}

Method #2

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class AppBoot {

    public static void main(String[] args) {
        SpringApplication.run(AppBoot.class, args);
    }
}

posted on 2022-12-31 21:04  Milton  阅读(199)  评论(0编辑  收藏  举报

导航