我刚刚开始学习 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/