spring 无法自动装配。'DataSource' 类型的 bean 不止一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43455869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not autowire. There is more than one bean of 'DataSource' type
提问by g3blv
I'm trying to Autowire a database by
我正在尝试通过以下方式自动装配数据库
@Autowired
private DataSource dataSource;
I have one datasource in my application.yml
我有一个数据源 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
But I get an error message saying.
但是我收到一条错误消息说。
Could not autowire. There is more than one bean of 'DataSource' type.
Beans:dataSource (DataSourceConfiguration.class)
dataSource (DataSourceConfiguration.class)
Which I find strange since I only have one datasource defined in my application.ymland to my knowledge I don't have any other datasource defined.
我觉得很奇怪,因为我只定义了一个数据源application.yml,据我所知,我没有定义任何其他数据源。
I gave a try with a config but I still get the same issue.
我尝试了一个配置,但我仍然遇到同样的问题。
@Configuration
@EnableConfigurationProperties
public class AppConfig {
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
This is my 'pom' file
这是我的“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>
I'm using Spring Boot 1.5.2 and IntelliJ 2017.1
我正在使用 Spring Boot 1.5.2 和 IntelliJ 2017.1
回答by Carlos Chávez
Try this it worked for me, use @Primary like this
试试这个对我有用,像这样使用@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);
}
And then use @autowire like this
然后像这样使用@autowire
@Autowired
@Qualifier("prodJdbc")
private JdbcTemplate prodJdbcTemplate;
I hope this can help you or someone else :)
我希望这可以帮助您或其他人:)
回答by Sarvar Nishonboev
I solved by adding qualifier above the property:
我通过在属性上方添加限定符来解决:
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
回答by Yann Cébron
Please note that Spring Boot autoconfigured beans are not supported 100% yet, see https://youtrack.jetbrains.com/issue/IDEA-139669for progress and possible workaorunds.
请注意,Spring Boot 自动配置的 bean 尚未 100% 得到支持,有关进展和可能的工作,请参阅https://youtrack.jetbrains.com/issue/IDEA-139669。


