Problems with @Qualifier

假如想象 提交于 2021-02-08 03:40:47

问题


I am working in a Java Spring environment and been having issues with getting @Qualifier to work. Other portions of our project are using @Inject to acquire a bean, but I need to have two versions of the same one and it looks like using @Autowired and @Qualifier should do the trick, but I cannot get them to work. I am probably missing one little thing, but haven't had any luck finding an answer.

Here are the pertinent parts of the code. I have been trying out various things, so I probably have more annotations and parameter than I need at the moment.

public class MongoDbConfig {
    @Bean(name="sourceTemplate")
    @Qualifier("sourceTemplate")
    public MongoTemplate getSourceTemplate() {
        MongoTemplate mt = new MongoTemplate(getMongoDbFactory(sourceServers, sourceDatabaseName));
        return mt;
    }

    @Bean(name="destinationTemplate")
    @Qualifier("destinationTemplate")
    public MongoTemplate getDestinationTemplate() {
        MongoTemplate mt = new MongoTemplate(getMongoDbFactory(destinationServers, destinationDatabaseName));
        return mt;
    }
}

public class SourceDaoImpl implements SourceDao {
    @Autowired
    @Qualifier("sourceTemplate")
    private MongoOperations mongoOps;
}


public class DestinationDaoImpl implements DestinationDao {
    @Autowired
    @Qualifier("destinationTemplate")
    private MongoOperations mongoOps;
}

When I try to start up my application, I get the following:

Parameter 1 of method gridFsTemplate in org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration required a single bean, but 2 were found:
    - sourceTemplate: defined by method 'getSourceTemplate' in MongoDbConfig
    - destinationTemplate: defined by method 'getDestinationTemplate' in MongoDbConfig

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Thanks for any suggestions about what I am missing here.


回答1:


Try this:

public class MongoDbConfig {
    @Bean(name = {"sourceTemplate", "mongoTemplate"})
    public MongoTemplate getSourceTemplate() {
        MongoTemplate mt = new MongoTemplate(getMongoDbFactory(sourceServers, sourceDatabaseName));
        return mt;
    }

    @Bean(name="destinationTemplate")
    public MongoTemplate getDestinationTemplate() {
        MongoTemplate mt = new MongoTemplate(getMongoDbFactory(destinationServers, destinationDatabaseName));
        return mt;
    }
}

public class SourceDaoImpl implements SourceDao {
    @Autowired
    @Qualifier("sourceTemplate")
    private MongoOperations mongoOps;
}


public class DestinationDaoImpl implements DestinationDao {
    @Autowired
    @Qualifier("destinationTemplate")
    private MongoOperations mongoOps;
}

Updated

Actually the method:

@Bean
    @ConditionalOnMissingBean
    public GridFsTemplate gridFsTemplate(MongoDbFactory mongoDbFactory,
            MongoTemplate mongoTemplate) {
        return new GridFsTemplate(
                new GridFsMongoDbFactory(mongoDbFactory, this.properties),
                mongoTemplate.getConverter());
    }

in MongoDataAutoConfigurationrequires a bean of MongoTemplate of name "mongoTemplate", that can't be found, instead you have defined your own 2 other beans "sourceTemplate" and "destinationTemplate".

I think this will resolve your issue!




回答2:


You have several ways to do that :

public class MongoDbConfig {
  @Bean
  @Qualifier("sourceTemplate")
  public MongoTemplate getSourceTemplate() {
    MongoTemplate mt = new MongoTemplate(getMongoDbFactory(sourceServers, sourceDatabaseName));
    return mt;
  }
....
}

Or :

public class MongoDbConfig {
  @Bean(name ="sourceTemplate")
  public MongoTemplate getSourceTemplate() {
    MongoTemplate mt = new MongoTemplate(getMongoDbFactory(sourceServers, sourceDatabaseName));
    return mt;
  }

Then

public class SourceDaoImpl implements SourceDao {
  @Autowired
  @Qualifier("sourceTemplate")
  private MongoOperations mongoOps;
}


来源:https://stackoverflow.com/questions/43938572/problems-with-qualifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!