无法将属性绑定到 ‘DataSource’ Springboot 2.7.9

huangapple go评论72阅读模式
英文:

Could not bind properties to 'DataSource' Springboot 2.7.9

问题

我已将您提供的内容翻译如下:

升级了Spring Boot从2.2.3.RELEASE到2.7.9之后,数据源(bean)的创建开始抛出以下异常:

通过构造函数参数0表示的不满足依赖项;嵌套异常是org.springframework.boot.context.properties.ConfigurationPropertiesBindException:创建名为'primaryDataSource'的bean时出现错误:无法将属性绑定到'DataSource':前缀=app.datasource.primary,ignoreInvalidFields=false,ignoreUnknownFields=true;嵌套异常是org.springframework.boot.context.properties.bind.BindException:无法将属性绑定到'app.datasource.primary'下的javax.sql.DataSource"。

我在配置中的属性如下:

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方法如下:

@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版本下有效,我漏掉了什么?

英文:

I have upgraded springboot from 2.2.3.RELEASE to 2.7.9, then datasource bean creation started throwing below exception

Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'primaryDataSource': Could not bind properties to 'DataSource' : prefix=app.datasource.primary, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'app.datasource.primary' to javax.sql.DataSource"

the properties I have in config are below

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

bean method

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

Dependencies

       &lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;mysql&lt;/groupId&gt;
			&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
			&lt;version&gt;8.0.32&lt;/version&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.zaxxer&lt;/groupId&gt;
			&lt;artifactId&gt;HikariCP&lt;/artifactId&gt;
			&lt;version&gt;4.0.3&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
		&lt;/dependency&gt;

The same setup works with 2.2.X spring boot version, what I am missing

答案1

得分: 0

url: &quot;jdbc:mysql://url&quot; 需要更新为 jdbc-url: &quot;jdbc:mysql://url&quot;

英文:

I have mentioned the jdbc url in config as
url: &quot;jdbc:mysql://url&quot; it need to be updated as
jdbc-url: &quot;jdbc:mysql://url&quot;

huangapple
  • 本文由 发表于 2023年3月1日 12:25:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定