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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35029011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:15:25  来源:igfitidea点击:

Spring how to set datasource in Spring Boot

javaspringpostgresqljdbcspring-boot

提问by Prichmp

I'm just learning Spring and I've been struggling for the past couple of days on how to configure the Spring JdbcTemplate to use my PostgreSQL database. I don't know how to do this. I keep reading the documentation, (such as http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html) but it seems like I'm going around in circles.

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

From the errors thrown it seems that it can't instantiate the RelationalDatabase Class that I wrote as a bean. I'm not sure what it would take to instantiate the class properly.

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

How do I move from something like the guides (Like https://spring.io/guides/gs/relational-data-access/) which totally work to a more complex solution?

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

Relational Database Class

关系数据库类

@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

\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

Stack Trace (Summary)

堆栈跟踪(摘要)

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).

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

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

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

Thanks!

谢谢!

回答by haihui

Did you forget the "r" in Driver? (Cannot determine embedded database driver class for database type NONE)

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

spring.database.driverClassName=org.postgresql.Driver

instead of

代替

spring.database.driverClassName=org.postgresql.Drive

回答by dassum

I Got the same Exception when i was testing my Spring Boot Application from Junit.

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

I resolved it using the @SpringApplicationConfiguration(classes = Application.class) above the Junit Test class. The Spring Boot application was not getting initialized properly

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