spring 无法将属性绑定到“DataSource”

k97glaaz  于 2023-03-07  发布在  Spring
关注(0)|答案(1)|浏览(169)

我已经将springboot从2.2.3.RELEASE升级到2.7.9,然后数据源bean创建开始抛出以下异常
未满足通过构造函数参数0表示的依赖项;嵌套异常是org.springframework. Boot .context.properties。配置属性绑定异常:创建名为“primaryDataSource”的Bean时出错:无法将属性绑定到“数据源”:前缀=应用.数据源.主要,忽略无效字段=假,忽略未知字段=真;嵌套异常是org.springframework. Boot .context.properties.绑定。绑定异常:无法将“app.datasource.primary”下的属性绑定到javax.sql.数据源
配置中的属性如下

app:
  datasource:
    primary:
      minimum-idle: 5
      maximum-pool-size: 20
      connection-timeout: 30000
      maxLifetime: 540000
      idleTimeout: 30000
      url: "jdbc:mysql://url"
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: user
      password: pwd

豆状体法

@Bean(name = "primaryDataSource")
    @ConfigurationProperties("app.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

依赖性

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.32</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>4.0.3</version>
        </dependency>

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

同样的设置适用于2.2.X Spring Boot 版本,我所缺少的

biswetbf

biswetbf1#

我在config中提到jdbc url为url: "jdbc:mysql://url",需要将其更新为jdbc-url: "jdbc:mysql://url"

相关问题