首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >‘未能配置url:未指定’datasource‘属性,并且无法配置任何嵌入的数据源。’springboot应用程序出错

‘未能配置url:未指定’datasource‘属性,并且无法配置任何嵌入的数据源。’springboot应用程序出错
EN

Stack Overflow用户
提问于 2021-10-21 20:36:38
回答 1查看 102关注 0票数 1

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

代码语言:javascript
复制
    <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类:

代码语言:javascript
复制
    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文件:

代码语言:javascript
复制
PostgresDBUsername = username
PostgresDBPassword = password
PostgresDBClassname = org.postgresql.Driver
PostgresDBUrl = jdbc:postgresql://localhost:5432/postgres

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

代码语言:javascript
复制
   @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");
    }
}

我得到了以下错误:

代码语言:javascript
复制
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

那么我能做些什么来解决这个问题呢?谢谢你!!

EN

回答 1

Stack Overflow用户

发布于 2021-10-22 07:46:10

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

代码语言:javascript
复制
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

它成功了!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69668486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档