按类型将 bean 自动装配到列表中 [英] Autowire reference beans into list by type

查看:29
本文介绍了按类型将 bean 自动装配到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 Daemon 类型的对象列表的类.

class Xyz {列表<守护进程>守护进程;}

我的弹簧配置看起来像这样.

<属性名称=守护进程"引用=守护进程列表"></bean><bean id="daemon1" class="package1.DaemonImpl1"/><bean id="daemon2" class="package1.DaemonImpl2"/><bean id="daemonsList" class="java.util.ArrayList"><构造函数参数><列表><ref bean="daemon1"/><ref bean="daemon2"/></list></constructor-arg></bean>

现在可以在列表中自动连接所有 Daemon 类型的 bean,而不是在列表中显式地连接每个守护程序实现.我试图解决的问题是,如果有人创建了一个 Daemon 类的新实现的 bean 并且忘记将它连接到列表中.

我在 stackoverflow 上的某个地方看到过这个问题,但无法再次找到.对此深表歉意.

解决方案

它应该像这样工作(从你的 XML 中删除 ArrayList bean):

public Class Xyz {私人列表<守护进程>守护进程;@自动连线public void setDaemons(List daemons){this.daemons = 守护进程;}}

我认为在 XML 中没有办法做到这一点.

<小时>

见:3.9.2.@Autowired@Inject:

<块引用>

也可以提供特定类型的所有 beanApplicationContext 通过将注解添加到字段或方法需要该类型的数组:

public class MovieRecommender {@自动连线私人电影目录[] 电影目录;//...}

<块引用>

这同样适用于类型化集合:

public class MovieRecommender {私人集<电影目录>电影目录;@自动连线//或者如果您不想要 setter,请注释该字段public void setMovieCatalogs(Set movieCatalogs) {this.movi​​eCatalogs = 电影目录;}//...}

顺便说一句,从 Spring 4.x 开始,这些列表可以使用@Ordered 机制自动排序.

I have one class that has a list of objects of Daemon type.

class Xyz {    
    List<Daemon> daemons;
}

My spring configuration looks like this.

<bean id="xyz" class="package1.Xyz">
   <property name="daemons" ref="daemonsList">
</bean>

<bean id="daemon1" class="package1.DaemonImpl1"/>
<bean id="daemon2" class="package1.DaemonImpl2"/>

<bean id="daemonsList" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <ref bean="daemon1" />      
                <ref bean="daemon2" />
            </list>
        </constructor-arg>
</bean>

Now instead of explicitly wiring each daemon implementation in list, is it possible to autowire all beans of type Daemon automatically in list. Problem I am trying to solve is, If someone creates a bean of new implementation of Daemon class and forgets to wire it into list.

I have seen this question somewhere on stackoverflow but not able to find that again. Apologies for it.

解决方案

It should work like this (remove the ArrayList bean from your XML):

public Class Xyz {    

    private List<Daemon> daemons;

    @Autowired
    public void setDaemons(List<Daemon> daemons){
        this.daemons = daemons;
    }

}

I don't think there's a way to do this in XML.


See: 3.9.2. @Autowired and @Inject:

It is also possible to provide all beans of a particular type from the ApplicationContext by adding the annotation to a field or method that expects an array of that type:

public class MovieRecommender {

  @Autowired
  private MovieCatalog[] movieCatalogs;

  // ...
}

The same applies for typed collections:

public class MovieRecommender {

  private Set<MovieCatalog> movieCatalogs;

  @Autowired
  // or if you don't want a setter, annotate the field
  public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
      this.movieCatalogs = movieCatalogs;
  }

  // ...
}

BTW, as of Spring 4.x, these lists can be ordered automatically using the @Ordered mechanism.

这篇关于按类型将 bean 自动装配到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆