Spring 开机报错 URL must start with 'jdbc'
Spring Boot gives an error of URL must start with 'jdbc'
我正在尝试使用注解配置在 spring 启动时配置 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 必须以 'jdbc'
开头
尝试通过为 postgres 定义端口号来更改 url 参数的值。假设 postgres 在默认端口 5432 上 运行。
改变
jdbc:postgresql://localhost/mydb
到
jdbc:postgresql://localhost:5432/mydb
用于端口号检查Find the host name and port using PSQL commands
更新:
也将 @Value("${username}")
更改为 @Value("${database.username}")
和其他属性也通过前缀 database
.
我正在尝试使用注解配置在 spring 启动时配置 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 必须以 'jdbc'
开头尝试通过为 postgres 定义端口号来更改 url 参数的值。假设 postgres 在默认端口 5432 上 运行。
改变
jdbc:postgresql://localhost/mydb
到
jdbc:postgresql://localhost:5432/mydb
用于端口号检查Find the host name and port using PSQL commands
更新:
也将 @Value("${username}")
更改为 @Value("${database.username}")
和其他属性也通过前缀 database
.