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

标签 java spring postgresql jdbc spring-boot

我刚刚开始学习 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: Error creating bean with name 'relationalDatabase': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.scientifi.papers.db.relational.RelationalDatabase.setDataSource(javax.sql.DataSource);

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed;

nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.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 (the profiles "dev " are currently active).

谢谢!

最佳答案

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

spring.database.driverClassName=org.postgresql.Driver

代替

spring.database.driverClassName=org.postgresql.Drive

关于java - Spring如何在Spring Boot中设置数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35029011/

相关文章:

java - 尝试制作 GUI,找不到显示的符号?

java - 获取 TinkerVertex 属性中某个键对应的值

spring - spring框架引用文档3.2.2中@Inject是否有错误?

r - 将原始字节作为 R 中的原始字节导入

sql - 左连接和右表中的数据

java - 使用 Retrofit Rest Adapter 时出错 : NoClassDefFoundError: Retrofit. Restadapter$Builder

java - 在任何线程死亡之前,它都会为等待线程引用的所有对象调用notify

java - 如何从@ComponentScan 包中获取接口(interface)列表

java - 在 Spring Boot JPA 中使用 Hibernate 过滤器

postgresql - 如何在 PostgreSQL 中修剪文本数组?