@Qualifier无效

时间:2017-08-08 22:01:11

标签: spring spring-boot dependency-injection spring-4

我有3个数据源,我设置如下:

@Configuration
@Component
public class DataSourceConfig {

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

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

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

}

我正在尝试将它们自动装入我的消费者类,如下所示:

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyClass {

    @Autowired
    public MyClass(
            @Autowired @Qualifier("foo") DataSource foo,
            @Autowired @Qualifier("bar") DataSource bar,
            @Autowired @Qualifier("baz") DataSource baz
            ) {
        ;
    }

}

当应用尝试启动时,我收到错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.example.mypackage.MyClass required a single bean, but 3 were found:
    - foo: defined by method 'foo' in class path resource [com/example/DataSourceConfig.class]
    - bar: defined by method 'bar' in class path resource [com/example/DataSourceConfig.class]
    - baz: defined by method 'baz' in class path resource [com/example/DataSourceConfig.class] 

为什么@Qualifier无效?

我正在使用Spring Boot 1.5.6.RELEASE

3 个答案:

答案 0 :(得分:5)

通过添加@Primary注释使其中一个bean成为主要bean。然后应该识别限定词。

答案 1 :(得分:0)

试试这个:

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

    @Component
    public class MyClass {

    private DataSource foo;
    private DataSource bar; 
    private DataSource baz;

    @Autowired
    public MyClass(
            @Qualifier("foo") final DataSource foo,
            @Qualifier("bar") final DataSource bar,
            @Qualifier("baz") final DataSource baz
            ) {
        ;
    }

}

答案 2 :(得分:0)

此解决方案解决了有问题的问题,但它无法呈现完全正常运行的配置。

  1. 设置主数据库。
  2. 更改构造函数格式。
  3. MyClass.java

    @Component
    public class MyClass {
    
        private final DataSource foo;
        private final DataSource bar;
        private final DataSource baz;
    
        @Autowired
        public MyClass(@Qualifier("foo") DataSource foo,
                       @Qualifier("bar") DataSource bar,
                       @Qualifier("baz") DataSource baz) {
    
            this.foo = foo;
            this.bar = bar;
            this.baz = baz;
    
        }
    
    }
    

    DataSourceConfig.java

    @Configuration
    @Component
    public class DataSourceConfig {
    
        @Primary
        @Bean("foo")
        @ConfigurationProperties(prefix = "spring.datasource.foo")
        public DataSource foo() {
            DataSource dataSource = DataSourceBuilder.create().build();
            return dataSource;
        }
    
        @Bean("bar")
        @ConfigurationProperties(prefix = "spring.datasource.bar")
        public DataSource bar() {
            DataSource dataSource = DataSourceBuilder.create().build();
            return dataSource;
        }
    
        @Bean("baz")
        @ConfigurationProperties(prefix = "spring.datasource.baz")
        public DataSource baz() {
            DataSource dataSource = DataSourceBuilder.create().build();
            return dataSource;
        }
    
    }
    

    这会留下错误org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set。为此,我建议关注this question,因为它侧重于问题的主题,此问题主要围绕@Bean个名称,并使用@Qualifier引用它们。