【发布时间】:2020-10-31 18:55:42
【问题描述】:
我正在尝试使用注释配置在 Spring Boot 中配置 Postgres 数据库。我在一个名为 database.properties 的文件中拥有所有数据库凭据,配置文件名为 DBconfig.java
database.url= jdbc:postgresql://localhost/mydb
database.driverClassName= com.postgresql.jdbc.Driver
database.username= postgres
database.password= password
dbConfig 文件 -
@Configuration
@PropertySource("classpath:databaseAccess/database.properties")
public class DBconfig {
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Value("${url}")
private String url;
@Bean
@Qualifier("postgresDB")
public DataSource dataSource() {
DataSourceBuilder dataSource = DataSourceBuilder.create();
dataSource.url(url);
dataSource.password(password);
//dataSource.driverClassName(driverClassName);
dataSource.username(username);
return dataSource.build();
}
@Bean
@Qualifier("jdbcTemplate")
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
}
这是我的主文件
@SpringBootApplication
public class GetNotificationsApplication {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(DBconfig.class);
JdbcTemplate template= ctx.getBean("jdbcTemplate", JdbcTemplate.class);
template.execute("CREATE TABLE TEST( test VARCHAR(20))");
}
}
我不断收到错误 工厂方法“dataSource”抛出异常;嵌套异常是 java.lang.IllegalArgumentException: URL must start with 'jdbc'
【问题讨论】:
-
url 中的值是否与属性文件中的值一样?
-
是的,我在我的计算机上的 8080 端口启动了一个 Postgres 服务器。
-
Postgres 服务器在 8080,那么你的 spring 应用程序正在运行哪个端口?
-
哎呀对不起我的意思是 5432 标准端口
标签: postgresql spring-boot spring-jdbc