【问题标题】:'Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.' error in springboot application“配置数据源失败:未指定‘url’属性,无法配置嵌入式数据源。” springboot应用程序中的错误
【发布时间】:2021-12-08 14:28:06
【问题描述】:

我创建了一个 springboot 应用程序,它使用 Spring Boot 和 Apache Camel JDBC 组件在 postgreSQL 中插入记录。为此,我使用了以下依赖项:

    <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <scope>runtime</scope>
            </dependency>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
        <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.9.0</version>
    </dependency>
        <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jdbc</artifactId>
        <version>${camel.version}</version>
        <!-- use the same version as your Camel core version -->
    </dependency>

至于数据库配置,我用 application.properties 文件创建了以下 java 类:

数据库配置java类:


    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
    import org.apache.camel.support.SimpleRegistry;
    import org.apache.commons.dbcp2.BasicDataSource;
    
    
    public class DatabaseConfiguration {
        public static SimpleRegistry createDatabaseConfiguration() throws IOException {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src/main/resources/application.properties"));
            BasicDataSource basic = new BasicDataSource();
            basic.setDriverClassName(properties.getProperty("PostgresDBClassname"));
            basic.setUsername(properties.getProperty("PostgresDBUsername"));
            basic.setPassword(properties.getProperty("PostgresDBPassword"));
            basic.setUrl(properties.getProperty("PostgresDBUrl"));
            SimpleRegistry registry = new SimpleRegistry();
            registry.bind("myDataSource", basic);
            return registry;
        }
    }

application.properties 文件:

PostgresDBUsername = username
PostgresDBPassword = password
PostgresDBClassname = org.postgresql.Driver
PostgresDBUrl = jdbc:postgresql://localhost:5432/postgres

我按以下方式编写了路由器,注意到我尝试将 dataSource 替换为 myDataSource:

   @Component
public class InsertRestService extends RouteBuilder {

  

    @Override
    public void configure() throws Exception {
        

        rest("/").produces("text/plain")
            .get("insert")
            .to("direct:hello");

        from("direct:hello")
            .transform().simple("INSERT INTO person (name, country) VALUES (DANY, LB)")
            .to("jdbc:dataSource") //spring boot starter jdbc creates the bean in the registry
            .transform().simple("Data inserted in Postgres successfully");
    }
}


我收到以下错误:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

我在 application.properties 文件 PostgresDBUrl 中提供了 URL

另外注意我下载了jdbc驱动jar文件并添加到模块路径中,右键打包后,构建路径,配置构建路径 那么我能做些什么来解决这个问题呢? 谢谢!!

【问题讨论】:

  • 摆脱你的配置:这就是Boot为你做的。只需设置spring.datasource.url 并在:/@ 中包含用户名/密码,或者设置用户名和密码属性。

标签: java postgresql spring-boot jdbc apache-camel


【解决方案1】:

我删除了配置文件(DatabaseConfiguration)并将application.properties文件的内容替换为:

spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.platform=postgres
spring.jpa.hibernate.ddl-auto=none

它成功了!

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 2019-09-12
    • 2023-02-21
    • 2020-12-17
    • 1970-01-01
    • 2022-07-21
    • 2020-05-31
    • 2021-05-14
    • 2019-03-25
    相关资源
    最近更新 更多