即使使用限定符,也无法自动装配2个相同类型但名称不同的bean

时间:2017-06-29 04:02:20

标签: java spring autowired

我正在开发一个spring-boot应用程序,我有一个配置类,我在其中定义了2个相同类型但具有不同名称的bean,我使用该bean作为不同bean的参数,我使用限定符来有不同的名称,但我仍然无法自动装配豆

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.io.File;

/**
 * Created by jauharia on 6/28/2017.
 */
@Configuration
public class DbConfig {

    @Bean(name = "sourceJdbcTemplate")
    public JdbcTemplate sourceJdbcTemple(@Qualifier("srcDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


    @Bean(name = "srcDataSource")
    public DataSource sourceDataSource(Config config) {
        String driverClassName = config.getString("dbPullConfig.Database.Source.driver");
        String url = config.getString("dbPullConfig.Database.Source.url");
        String userName = config.getString("dbPullConfig.Database.Source.username");
        String password = config.getString("dbPullConfig.Database.Source.password");

        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(driverClassName);
        driverManagerDataSource.setUrl(url);
        driverManagerDataSource.setUsername(userName);
        driverManagerDataSource.setPassword(password);

        return driverManagerDataSource;
    }

    @Bean(name = "destDataSource")
    public DataSource destinationDataSource(Config config) {
        String driverClassName = config.getString("dbPullConfig.Database.Destination.driver");
        String url = config.getString("dbPullConfig.Database.Destination.url");
        String userName = config.getString("dbPullConfig.Database.Destination.username");
        String password = config.getString("dbPullConfig.Database.Destination.password");

        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(driverClassName);
        driverManagerDataSource.setUrl(url);
        driverManagerDataSource.setUsername(userName);
        driverManagerDataSource.setPassword(password);

        return driverManagerDataSource;
    }

    @Bean(name = "config")
    public Config config() {
        return ConfigFactory.parseFile(new File("DbPullConfig.conf"));
    }
}

我收到以下错误

parameter 0 of method sourceJdbcTemple in com.ambuj.risk.DbConfig required a single bean, but 2 were found:
    - srcDataSource: defined by method 'sourceDataSource' in class path resource [com/ambuj/risk/DbConfig.class]
    - destDataSource: defined by method 'destinationDataSource' in class path resource [com/ambuj/risk/DbConfig.class]

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>

    <artifactId>DbPull</artifactId>

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

    <dependencies>
        <dependency>
            <groupId>com.typesafe</groupId>
            <artifactId>config</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sybase.jconnect</groupId>
            <artifactId>jconn4</artifactId>
            <version>7.0</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>

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

0 个答案:

没有答案