Spring 如何在 Spring Boot 中设置数据源

Posted

技术标签:

【中文标题】Spring 如何在 Spring Boot 中设置数据源【英文标题】:Spring how to set datasource in Spring Boot 【发布时间】:2016-05-03 21:52:44 【问题描述】:

我刚刚学习 Spring,过去几天我一直在努力研究如何配置 Spring JdbcTemplate 以使用我的 PostgreSQL 数据库。我不知道该怎么做。我一直在阅读文档(例如http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html),但似乎我在绕圈子。

从抛出的错误看来,它似乎无法实例化我作为 bean 编写的 RelationalDatabase 类。我不确定如何正确实例化该类。

如何从完全有效的指南(如 https://spring.io/guides/gs/relational-data-access/)转向更复杂的解决方案?

关系数据库类

@Repository
public class RelationalDatabase 


    private JdbcTemplate jdbcTemplate;

    public RelationalDatabase()


    public RelationalDatabase(JdbcTemplate jdbcTemplate)
    
        this.jdbcTemplate = jdbcTemplate;
    

    @Autowired
    public void setDataSource(javax.sql.DataSource dataSource) 
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    

    public JdbcTemplate getJdbcTemplate() 
        return jdbcTemplate;
    


    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) 
        this.jdbcTemplate = jdbcTemplate;
    


\src\main\resources\application-dev.properties

spring.datasource.url=jdbc:postgresql://192.168.56.102:5432/scibase
spring.datasource.type=org.postgresql.ds.PGPoolingDataSource 
spring.datasource.username=lemon
spring.datasource.password=XXXXXX
spring.datasource.platform=postgres
spring.datasource.max-active=100
spring.datasource.name=lime
spring.database.driverClassName=org.postgresql.Drive

堆栈跟踪(摘要)

org.springframework.beans.factory.BeanCreationException: 错误 创建名为“relationalDatabase”的bean:注入自动装配 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 自动装配方法:公共无效 com.scientifi.papers.db.relational.RelationalDatabase.setDataSource(javax.sql.DataSource);

嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建在类路径资源中定义的名称为“dataSource”的bean [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: 通过工厂方法实例化 Bean 失败;

嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [javax.sql.DataSource]:工厂方法 'dataSource' 抛出 例外;嵌套异常是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: 无法确定数据库类型的嵌入式数据库驱动程序类 没有。如果您想要一个嵌入式数据库,请放置一个受支持的数据库 类路径。如果您有要从 您可能需要激活它的特定配置文件(配置文件“dev” 目前处于活动状态)。

谢谢!

【问题讨论】:

你是如何运行spring boot应用的?您是否使用 -Dspring.profiles.active=dev VM 参数运行? 您是否已将 PostgreSQL JAR 添加到 pom/classpath 中? 【参考方案1】:

您是否忘记了驱动程序中的“r”? (无法确定数据库类型 NONE 的嵌入式数据库驱动程序类

spring.database.driverClassName=org.postgresql.Driver

而不是

spring.database.driverClassName=org.postgresql.Drive

【讨论】:

我确实忘记了“r”,但事实并非如此。我完全放弃了 Spring。我去了 Spark 微框架,实际上它要容易得多。我可以通过这种方式控制我的代码,而且我一直在从 Spring 导入库,所以我觉得我实际上并没有损失太多。【参考方案2】:

当我从 Junit 测试我的 Spring Boot 应用程序时,我遇到了同样的异常。

我使用 Junit 测试类上方的 @SpringApplicationConfiguration(classes = Application.class) 解决了它。 Spring Boot 应用程序未正确初始化

【讨论】:

以上是关于Spring 如何在 Spring Boot 中设置数据源的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring Boot 中设置 UTF-8 字符编码?

如何在spring boot中设置禁用浏览器缓存?

如何在 Spring Boot 中设置副本集?

如何在 Spring Boot 中设置 enableLoggingRequestDetails='true'

如何在 Spring Boot 中的 DatabaseConfig 中设置属性

如何在 Spring Boot 中设置 ActiveMQ 端口?