首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@Bean注解方法中接口类型的注入采集

@Bean注解方法中接口类型的注入采集
EN

Stack Overflow用户
提问于 2019-10-01 21:57:16
回答 2查看 67关注 0票数 0

使用Spring并给出几个实现公共接口的类,如何在方法级别引用使用@Bean注释实现该接口的所有类?

我希望检索所有实现实例,对每个实例应用一些逻辑,然后返回一个托管Map<String, Animal>对象,该对象可以注入到其他类或组件中。

公共接口

代码语言:javascript
复制
public interface Animal {

   String makeNoise();

}
代码语言:javascript
复制
public interface Person {

   String getOccupation();

}

动物实现#1

代码语言:javascript
复制
public Dog implements Animal {

   @Override
   String makeNoise() {
      return "Bark! Bark!";
   }

} 

动物实现#2

代码语言:javascript
复制
public Cat implements Animal {

   @Override
   String makeNoise() {
      return "Meow! Meow!";
   }

} 

Person实现#1

代码语言:javascript
复制
public Developer implements Person {

   @Override
   public String getOccupation() {
      return "Software Engineer";
   }

}

Person实现#2

代码语言:javascript
复制
public Lawyer implements Person {

   @Override
   public String getOccupation() {
      return "Litigator";
   }

}

Configuration

代码语言:javascript
复制
@Configuration
public class Initialize {

   //<snip> Beans created for Developer, and Lawyer objects </snip>

   @Bean
   Map<String, Developer> getDevelopers(List<Developer> developers) { // This is fine
      return new HashMap<>(...);
   }

   @Bean
   Map<String, Lawyer> getLawyers(List<Person> people) { // Spring wires this dependency fine
      return new HashMap<>(...);
   }

   @Bean
   Map<String, Dog> getOwners(Map<String, Person> owners) { // Spring reports it cannot auto-wire this dependency
                                                            // what do I do here? 
   }

}

任何帮助都将不胜感激,谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-02 02:31:41

需要利用与List的协方差。见下面的伪代码/代码片段。

代码语言:javascript
复制
@Configuration
public class Initialize {

   //<snip> Beans created for Developer, and Lawyer objects </snip>

   @Bean
   Map<String, Developer> getDevelopers(List<Developer> developers) {
      return new HashMap<>(...);
   }

   @Bean
   Map<String, Lawyer> getLawyers(List<Person> people) {
      return new HashMap<>(...);
   }

   @Bean
   Map<String, Dog> getOwners(List<Map<String, ? extends Person>> owners) { // Spring will auto-wire the "owners" variable 
                                                                            // with all bean objects that match this signature 
                                                                            // (✅ Map<String, Lawyer>, ✅ Map<String, Developer> ...)

   }

}

资源:

票数 0
EN

Stack Overflow用户

发布于 2019-10-01 22:05:17

试试这种配置。这里唯一的一点是,集合中bean的顺序是随机的,不能被控制。

代码语言:javascript
复制
    @Configuration
    public class CollectionConfig {

        @Bean
        public Animal getCat() {
            return new Cat();
        }

        @Bean
        public Animal getDog() {
            return new Dog();
        }

        @Bean
        Map<String, Animals> gatherAnimals(List<Animals> animals) {
           // any code
        }
    }

下面是关于那个https://www.baeldung.com/spring-injecting-collections的更多信息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58192781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档