java - 从使用 Spring 的 DataSourceBuilder 创建的数据源获取到 Postgresql 数据库的连接

标签 java spring postgresql maven spring-boot

我是 Spring 和 Spring Boot 的新手。我正在尝试连接到 Spring Boot 应用程序中的 Postgresql 数据库。 我试图从中汲取灵感

http://spring.io/guides/gs/accessing-data-mysql/

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html

基本上我有一个 application.properties 文件,其中包含:

app.datasource.url=jdbc:postgresql://localhost:5432/db_example
app.datasource.username=myusername
app.datasource.password=mypassword
app.datasource.driver-class-name=org.postgresql.Driver

使用 psql 工具,我可以正常使用 5432 端口以及这些用户名和密码连接到本地主机上的 db_example 数据库。

然后我有一个 MyConfig.java 其中包含:

package hello;

import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class MyConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "app.datasource")
    public DataSource dataSource() {
        DataSourceBuilder dsb = DataSourceBuilder.create();
        if (dsb == null) {
            return null;
        }
        return dsb.build();
    }

}

然后,在具有 @GetMapping 注释的 MainController.java 方法之一中,我调用 dataSource() 方法。在返回的对象上,我调用 getConnection() 但会抛出 SQLException 并显示消息 The url can not be null

此外,Spring Boot 应用程序在控制台中写入了几项内容:

Not loading a JDBC driver as driverClassName property is null.

Unable to create initial connections of pool

我错过了什么?我猜 MyConfig.java 中有一些注释?

编辑:您可能还想查看我的 pom.xml 用于使用 mvn 打包该内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-mysql-data</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->

        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>-->

        <!-- Use MySQL Connector-J -->

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

最佳答案

看来DataSourceBuilder没有设置属性。您可以尝试使用 Spring 中的默认配置吗:

spring.datasource.url=jdbc:postgresql://localhost:5432/db_example
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.postgresql.Driver

更多详细信息请参见:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

然后在您的代码中,您必须获取这些属性并设置它们:

@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driver;

DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName(driver);
dataSourceBuilder.username(username);
dataSourceBuilder.password(password);
dataSourceBuilder.url(url)

希望这有帮助。

关于java - 从使用 Spring 的 DataSourceBuilder 创建的数据源获取到 Postgresql 数据库的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47402278/

相关文章:

java - 将 observable 转换为列表

java - Sprite 的旋转,libgdx android

java - 从适配器访问 fragment

java - Spring MVC : Spring Security 4 doesn't intercept

sql - 何时使用 JSON[] 而不是 JSON?

java - 随机排序不会终止

java - 构造函数需要一个无法找到的 bean 类型

java - Spring:无法导入库

ruby-on-rails - Rails Postgres 不断告诉我用户不存在

r - 如何在 Heroku 上安装 libpq?