gpt4 book ai didi

spring - 无法自动接线。有多个 'DataSource' 类型的 bean

转载 作者:IT老高 更新时间:2023-10-28 13:49:59 25 4
gpt4 key购买 nike

我正在尝试通过

Autowiring 数据库
@Autowired
private DataSource dataSource;

我的 application.yml

中有一个数据源
spring:
profiles:
active: dev

---
spring:
profiles: dev
datasource:
driverClassName: org.mariadb.jdbc.Driver
url: jdbc:mariadb://localhost:3306/dbname
username: user
password: password

name: dev

logging:
level:
org.springframework: INFO


---

spring:
profiles: prod
name: prod

logging:
level:
org.springframework: INFO

但我收到一条错误消息。

Could not autowire. There is more than one bean of 'DataSource' type.

Beans:dataSource (DataSourceConfiguration.class)
dataSource (DataSourceConfiguration.class)

enter image description here

我觉得很奇怪,因为我的 application.yml 中只定义了一个数据源,据我所知,我没有定义任何其他数据源。

我尝试了一个配置,但仍然遇到同样的问题。

@Configuration
@EnableConfigurationProperties
public class AppConfig {

@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}

这是我的“pom”文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>id.group</groupId>
<artifactId>ProjectName</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>


<dependencies>
<!--Spring Boot dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--Spring Boot dependencies-->
<!--Spring Security dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--Spring Security dependencies-->
<!--JWT dependencies-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
<!--JWT dependencies-->
<!--Actuator and HAL browser dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
<!--Actuator and HAL browser dependencies-->
<!--Database dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>1.5.7</version>
</dependency>
<!--Database dependencies-->

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

我正在使用 Spring Boot 1.5.2 和 IntelliJ 2017.1

最佳答案

试试这个对我有用,像这样使用@Primary

    @Primary
@Bean(name ="prodDataSource")
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "prodJdbc")
public JdbcTemplate prodJdbcTemplate(@Qualifier("prodDataSource") DataSource prodDataSource){
return new JdbcTemplate(prodJdbcTemplate);
}

@Bean(name = "devDataSource")
@ConfigurationProperties(prefix = "spring.datasource.dev")
public DataSource devDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "devJdbc")
public JdbcTemplate devJdbcTemplate(@Qualifier("devDataSource") DataSource devDataSource) {
return new JdbcTemplate(devDataSource);
}

然后像这样使用@autowire

@Autowired
@Qualifier("prodJdbc")
private JdbcTemplate prodJdbcTemplate;

我希望这可以帮助你或其他人:)

关于spring - 无法自动接线。有多个 'DataSource' 类型的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43455869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com