简体   繁体   English

将接口类型的集合注入到使用 @Bean 注释的方法中

[英]Injecting Collection of Interface Type into Method Annotated with @Bean

Using Spring and given several classes that implement a common interface, how would I reference all classes that implemented this interface using the @Bean annotation at the method level?使用 Spring 并给定几个实现公共接口的类,我将如何在方法级别使用@Bean注释引用实现该接口的所有类?

I want to retrieve all implementing instances, apply some logic to each instance, then return a managed Map<String, Animal> object which can be injected to other classes or components.我想检索所有实现实例,对每个实例应用一些逻辑,然后返回一个托管Map<String, Animal> object 可以注入其他类或组件。

Common Interface(s)通用接口

public interface Animal {

   String makeNoise();

}
public interface Person {

   String getOccupation();

}

Animal Implementation #1动物实施#1

public Dog implements Animal {

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

} 

Animal Implementation #2动物实施#2

public Cat implements Animal {

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

} 

Person Implementation #1人员实施#1

public Developer implements Person {

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

}

Person Implementation #2人员实施#2

public Lawyer implements Person {

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

}

Configuration配置

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

}

Any help would be appreciated, thanks!任何帮助将不胜感激,谢谢!

Try this kind of configuration.试试这种配置。 The only point here is that the order of beans in the collection is random and can't be under control.这里唯一的一点是集合中bean的顺序是随机的,无法控制。

    @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
        }
    }

Here is more about that https://www.baeldung.com/spring-injecting-collections这里有更多关于https://www.baeldung.com/spring-injecting-collections

Need to leverage co-variance with a List .需要利用List的协方差。 See below pseudo-code/code snippet.请参阅下面的伪代码/代码片段。

@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> ...)

   }

}

Resources:资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM