英文:
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: "jdbc:mysql://url"
driver-class-name: com.mysql.cj.jdbc.Driver
username: user
password: pwd
bean method
@Bean(name = "primaryDataSource")
@ConfigurationProperties("app.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
Dependencies
<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>
The same setup works with 2.2.X spring boot version, what I am missing
答案1
得分: 0
url: "jdbc:mysql://url"
需要更新为 jdbc-url: "jdbc:mysql://url"
。
英文:
I have mentioned the jdbc url in config as
url: "jdbc:mysql://url"
it need to be updated as
jdbc-url: "jdbc:mysql://url"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论