简体   繁体   English

我可以使用 spring @Autowired 依赖注入来构建 class 的多个实例吗?

[英]Can I use spring @Autowired dependency injection to build multiple instances of a class?

I have a vaadin UI class with a constructor taking 2 arguments.我有一个带有 2 个 arguments 的构造函数的 vaadin UI class。 It builds a simple line with some fields, showing data.它用一些字段构建了一条简单的线,显示数据。 In another (parent) UI, I want to embed that first UI (child) multiple times, depending on some data loaded in the parent.在另一个(父) UI 中,我想多次嵌入第一个 UI(子),具体取决于父级中加载的一些数据。 So now I have two questions:所以现在我有两个问题:

  1. Can I use springs @Autowired annotation to inject multiple instances of my child UI to the parent?我可以使用 springs @Autowired注释将我的子 UI 的多个实例注入父级吗? If yes, how do I do this?如果是,我该怎么做?
  2. How do I pass arguments to the constructor of my @Autowired child class?如何将 arguments 传递给我的@Autowired子 class 的构造函数?

I already found out, that I must annotate the constructor of my child class with @Autowired .我已经发现,我必须用@Autowired注释我的孩子 class 的构造函数。

My child UI class with constructor (annotated with @Autowired )我的孩子 UI class 带有构造函数(用@Autowired注释)

public class ChildUI {

   private String arg1;
   private String arg2;

   @Autowired
   public ChildUI(String arg1, String arg2){
      this.arg1 = arg1;
      this.arg2 = arg2;
   }

}

In my parent class, I want to do something like this (personList is loaded from database):在我的父母 class 中,我想做这样的事情(personList 是从数据库加载的):

public class ParentUI {

   ...
   for(Person p : personList){
      //inject instance of ChildUI here and pass p.getLastName() to arg1 and p.getFirstName() to arg2
   }
   ...

}

I googled for a while but I did not really find what I was looking for.我用谷歌搜索了一段时间,但我并没有真正找到我想要的东西。 Maybe I just don't know what keywords to search for.也许我只是不知道要搜索什么关键字。 Can maybe someone try to explain what to do?也许有人可以尝试解释该怎么做?

Just create ChildUI like your normally would像往常一样创建ChildUI

  for(Person p : personList){
     ChildUI someChild=nChildUI(p.getLastName(),m.getFirstName());
   }
   ...

and do something with someChildsomeChild

Or if ChildUI have some other dependencies injected - first make it prototype scoped, then或者如果ChildUI注入了一些其他依赖项 - 首先使其原型作用域,然后

    @Autowire
    private ApplicationContext ctx;
....
      for(Person p : personList){
         ChildUI someChild=ctx.getBean(ChildUI.class,p.getLastName(),m.getFirstName());
       }

I'm not sure I completely understand what you ask, so let me know if that's not what you meant.我不确定我是否完全理解你的要求,所以如果这不是你的意思,请告诉我。

Creating multiple instances of your childUI: This is simple, create multiple beans in your configuration class:创建 childUI 的多个实例:这很简单,在配置 class 中创建多个 bean:

@Configuration
public class ApplicationConfiguration {

  @Bean
  public ChildUI firstBean(){
    return new ChildUI(arg1,arg2);
  }

  @Bean
  public ChildUI secondBean(){
    return new ChildUI(otherArg1,otherArg2);
  }

  @Bean
  public ChildUI thirdBean(){
    return new ChildUI(arg1,arg2);
  }
}

multiple instances injected to other bean: If you autowire a set (or list) of the type of your bean, all instances of it will be injected to it:注入其他 bean 的多个实例:如果自动装配 bean 类型的集合(或列表),它的所有实例都将注入它:

public class ParentUI {

  private final Set<ChildUI> children;

  @Autowired
  public ParentUI(Set<ChildUI> children) {
    this.childern = children;//this will contain all three beans
  }
}

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

相关问题 我可以在多个类中使用Spring @Autowired吗? - Can I use Spring @Autowired in multiple classes? 使用Spring Security自动连接依赖注入 - Autowired Dependency Injection with Spring Security Spring 依赖注入@Autowired VS 依赖注入没有@Autowired 的对象 - Spring dependency injection @Autowired VS dependency injection of an object without @Autowired 依赖注入和多个实例 - Dependency injection and multiple instances (春季)我可以在循环内使用依赖项注入吗? - (Spring) Can I use dependency injection inside a loop? 使用@Autowired注释的Spring依赖注入,不带setter - Spring dependency injection with @Autowired annotation without setter Spring Hello依赖注入@Autowired抛出NullPointerException - Spring Hello Dependency Injection @Autowired throws NullPointerException Spring(autowired)组件在同一类中使用多个 - Spring (autowired) component use multiple in the same class 我如何在不使用getBean的情况下在Spring项目中使用@Autowired正确注入依赖项? 可能吗? - How can i correctly inject dependency with @Autowired in the spring project without use getBean? Is it Possible? 如果我在一个类的成员上使用Spring的@Autowired注释,我可以在课堂上新建吗? - If I use Spring's @Autowired annotation on a member of a class, can I new up the class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM