本文介绍了无法自动接线.有一个以上的"DataSource"类型的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过

@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

但是我收到一条错误消息.

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)

我感到很奇怪,因为我在application.yml中仅定义了一个数据源,而据我所知,我没有定义任何其他数据源.

Which I find strange since I only have one datasource defined in my application.yml and to my knowledge I don't have any other datasource defined.

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

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();
    }
}

这是我的"pom"文件

This is my 'pom' file

<?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

I'm using Spring Boot 1.5.2 and IntelliJ 2017.1

推荐答案

尝试一下对我有用,像这样使用@Primary

Try this it worked for me, use @Primary like this

    @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

And then use @autowire like this

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

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

I hope this can help you or someone else :)

这篇关于无法自动接线.有一个以上的"DataSource"类型的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:46