Spring Boot运行发生异常:Factory method 'dataSource' threw exception; nested exception is org.springframe

本文介绍了一种常见的Spring Boot应用中数据源配置异常的问题,该问题表现为无法确定嵌入式数据库驱动类。文中提供了具体的异常堆栈信息,并给出了通过在启动类的@EnableAutoConfiguration注解中使用exclude属性来排除特定自动配置类的方法,以此解决数据源配置异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常:

 Caused by: org.springframework.beans.BeanInstantiationException:
 Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: 
 Factory method 'dataSource' threw exception; 
 nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: 
 Cannot determine embedded database driver class for database type NONE. 
 If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a 
particular profile you may need to active it (no profiles are currently active).


原因:

 maven依赖包冲突,有重复的依赖。


解决:



   在启动类的@EnableAutoConfiguration添加exclude = {DataSourceAutoConfiguration.class},排除此类的autoconfig。启动以后就可以正常运行。



 


### Spring Boot 中 `dataSource` 方法抛出 `ExceptionInInitializerError` 的原因分析 在 Spring Boot 应用程序中,当定义数据源 (`DataSource`) 时如果发生初始化错误,则可能会触发 `java.lang.ExceptionInInitializerError` 异常。此异常通常是由底层的 `NoSuchBeanDefinitionException` 或其他配置问题引起的。 以下是可能导致该问题的原因以及对应的解决方案: #### 1. 数据库驱动未正确引入 如果没有正确导入所使用的数据库驱动依赖项(例如 MySQL、PostgreSQL),则会在尝试加载 `DataSource` Bean 时失败。这通常是由于缺少 Maven 或 Gradle 构建工具中的相应依赖项所致[^1]。 ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> ``` 上述 XML 片段展示了如何通过 Maven 添加 MySQL 连接器作为运行时依赖项。确保已根据实际使用的数据库调整相应的依赖项版本和范围[^2]。 #### 2. 配置文件缺失或不完整 Spring Boot 使用 `application.properties` 或 `application.yml` 文件来存储应用程序属性设置。对于 JDBC 数据源而言,至少需要提供以下几个参数才能正常工作[^3]: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 以上是一个典型的 MySQL 数据源配置示例。注意 URL 地址应指向目标数据库实例,并且用户名密码需匹配有效的登录凭证。任何拼写错误或者遗漏都可能引发初始化阶段的问题。 #### 3. 自动装配扫描路径不当 假如项目结构复杂并且存在多个模块,则有可能因为组件扫描机制未能覆盖到自定义实现类而导致找不到特定 Factory Method 实现的情况。此时可以显式指定基础包名以便让框架能够发现所有必要的 Beans 定义[^4]: ```xml <context:component-scan base-package="com.yourcompany.project"/> ``` 这段代码片段来自传统 Spring XML 方式的声明方式;而在现代基于 Java Config 的场景下可以通过 @ComponentScan 注解达到相同效果: ```java @Configuration @ComponentScan(basePackages = {"com.yourcompany.project"}) public class AppConfig { } ``` #### 4. Seata 分布式事务集成影响 如果正在使用 Seata 来管理跨服务间的分布式事务处理逻辑,那么还需要额外考虑其对本地资源隔离策略的影响。具体来说,在 AT 模式下的全局锁机制会临时锁定某些记录直到整个流程完成提交操作之前都不会释放它们。因此建议仔细阅读官方文档了解最佳实践指南以避免潜在冲突风险[^5]. --- ### 示例代码展示正确的 DataSource 配置方法 下面给出一段完整的 Spring Boot Starter Data JPA 示例代码用于演示标准的数据访问层搭建过程: ```java import org.apache.commons.dbcp2.BasicDataSource; import javax.sql.DataSource; @Configuration @EnableTransactionManagement public class DatabaseConfig { @Bean(name = "primaryDataSource") public DataSource primaryDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC"); dataSource.setUsername("root"); dataSource.setPassword("password"); return dataSource; } } ``` ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值