🌷🍁 博主猫头虎 带您 Go to New World.✨🍁 🦄 博客首页——猫头虎的博客🎐 🐳《面试题大全专栏》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺 🌊 《IDEA开发秘籍专栏》学会IDEA常用操作,工作效率翻倍~💐 🌊 《100天精通Golang(基础入门篇)》学会Golang语言,畅玩云原生,走遍大小厂~💐
🪁🍁 希望本文能够给您带来一定的帮助🌸文章粗浅,敬请批评指正!🍁🐥
在@SpringBootApplication后添加exclude = { DataSourceAutoConfiguration.class }来排除自动配置 :
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
在application.yml文件中添加排除自动配置
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
application.properties
spring.autoconfigure.exclude= org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.2)
2022-09-28 17:03:06 [INFO] org.springframework.boot.StartupInfoLogger:55 : Starting JoySpaceApplicationKt using Java 1.8.0_342 on DESKTOP-PFPD85S with PID 12140 (E:\Project-Code\OutsideApi\build\classes\kotlin\main started by DELL in E:\Project-Code\OutsideApi)
2022-09-28 17:03:06 [INFO] org.springframework.boot.SpringApplication:634 : No active profile set, falling back to 1 default profile: "default"
2022-09-28 17:03:06 [INFO] org.springframework.data.repository.config.RepositoryConfigurationDelegate:132 : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-09-28 17:03:06 [INFO] org.springframework.data.repository.config.RepositoryConfigurationDelegate:201 : Finished Spring Data repository scanning in 8 ms. Found 0 JPA repository interfaces.
2022-09-28 17:03:07 [INFO] org.springframework.boot.web.embedded.tomcat.TomcatWebServer:108 : Tomcat initialized with port(s): 6060 (http)
2022-09-28 17:03:07 [INFO] org.apache.juli.logging.DirectJDKLog:173 : Initializing ProtocolHandler ["http-nio-6060"]
2022-09-28 17:03:07 [INFO] org.apache.juli.logging.DirectJDKLog:173 : Starting service [Tomcat]
2022-09-28 17:03:07 [INFO] org.apache.juli.logging.DirectJDKLog:173 : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-28 17:03:07 [INFO] org.apache.juli.logging.DirectJDKLog:173 : Initializing Spring embedded WebApplicationContext
2022-09-28 17:03:07 [INFO] org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext:292 : Root WebApplicationContext: initialization completed in 862 ms
2022-09-28 17:03:07 [WARN] org.springframework.context.support.AbstractApplicationContext:591 : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2022-09-28 17:03:07 [INFO] org.apache.juli.logging.DirectJDKLog:173 : Stopping service [Tomcat]
2022-09-28 17:03:07 [INFO] org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener:136 :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-09-28 17:03:07 [ERROR] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter:40 :
***************************
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).
Process finished with exit code 1
兜底方案:
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
但是很多时候,加了这个注解,还是不能解决自动寻找配置文件中url进行初始化数据库连接的异常。 原因在于,在pom文件中,使用跟数据库相关的依赖,如spring-data,druid等,需要把数据库相关的依赖去掉,然后再加上注解,就能实现无数据库启动springboot了。
亲测方案2有效~