Java 如何在 Spring 中定义 List bean?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2416056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:19:10  来源:igfitidea点击:

How to define a List bean in Spring?

javaspring

提问by guerda

I'm using Spring to define stages in my application. It's configured that the necessary class (here called Configurator) is injected with the stages.
Now I need the List of Stages in another class, named LoginBean. The Configuratordoesn't offer access to his List of Stages.

我正在使用 Spring 在我的应用程序中定义阶段。它被配置为必要的类(这里称为Configurator)注入了阶段。
现在我需要另一个名为LoginBean. 在Configurator不提供访问其阶段的名单。

I cannot change the class Configurator.

我不能改变班级Configurator

My Idea:
Define a new bean called Stages and inject it to Configuratorand LoginBean. My problem with this idea is that I don't know how to transform this property:

我的想法:
定义一个名为 Stages 的新 bean 并将其注入ConfiguratorLoginBean。我这个想法的问题是我不知道如何转换这个属性:

<property ...>
  <list>
    <bean ... >...</bean>
    <bean ... >...</bean>
    <bean ... >...</bean>
  </list>
</property>

into a bean.

成豆。

Something like this does not work:

像这样的东西不起作用:

<bean id="stages" class="java.util.ArrayList">

Can anybody help me with this?

有人可以帮我解决这个问题吗?

采纳答案by simonlord

Import the spring util namespace. Then you can define a list bean as follows:

导入 spring util 命名空间。然后你可以定义一个列表 bean,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/util
                    http://www.springframework.org/schema/util/spring-util-2.5.xsd">


<util:list id="myList" value-type="java.lang.String">
    <value>foo</value>
    <value>bar</value>
</util:list>

The value-type is the generics type to be used, and is optional. You can also specify the list implementation class using the attribute list-class.

value-type 是要使用的泛型类型,并且是可选的。您还可以使用属性指定列表实现类list-class

回答by stacker

Here is one method:

这是一种方法:

<bean id="stage1" class="Stageclass"/>
<bean id="stage2" class="Stageclass"/>

<bean id="stages" class="java.util.ArrayList">
    <constructor-arg>
        <list>
            <ref bean="stage1" />
            <ref bean="stage2" />                
        </list>
    </constructor-arg>
</bean>

回答by Stephen C

I think you may be looking for org.springframework.beans.factory.config.ListFactoryBean.

我想你可能正在寻找org.springframework.beans.factory.config.ListFactoryBean.

You declare a ListFactoryBean instance, providing the list to be instantiated as a property withe a <list>element as its value, and give the bean an idattribute. Then, each time you use the declared idas a refor similar in some other bean declaration, a new copy of the list is instantiated. You can also specify the Listclass to be used.

您声明一个 ListFactoryBean 实例,提供要实例化的列表作为属性<list>,并将元素作为其值,并为 bean 提供一个id属性。然后,每次在其他 bean 声明中使用声明id为 aref或类似的声明时,都会实例化列表的新副本。您还可以指定List要使用的类。

回答by Juan Perez

Use the util namespace, you will be able to register the list as a bean in your application context. You can then reuse the list to inject it in other bean definitions.

使用 util 命名空间,您将能够将列表注册为应用程序上下文中的 bean。然后,您可以重用该列表以将其注入其他 bean 定义中。

回答by haju

Stacker posed a great answer, I would go one step farther to make it more dynamic and use Spring 3 EL Expression.

Stacker 提出了一个很好的答案,我会更进一步使其更具动态性并使用 Spring 3 EL Expression。

<bean id="listBean" class="java.util.ArrayList">
        <constructor-arg>
            <value>#{springDAOBean.getGenericListFoo()}</value>
        </constructor-arg>
</bean>

I was trying to figure out how I could do this with the util:list but couldn't get it work due to conversion errors.

我试图弄清楚如何使用 util:list 执行此操作,但由于转换错误而无法使其正常工作。

回答by Koray Tugay

<bean id="someBean"
      class="com.somePackage.SomeClass">
    <property name="myList">
        <list value-type="com.somePackage.TypeForList">
            <ref bean="someBeanInTheList"/>
            <ref bean="someOtherBeanInTheList"/>
            <ref bean="someThirdBeanInTheList"/>
        </list>
    </property>
</bean>

And in SomeClass:

在 SomeClass 中:

class SomeClass {

    List<TypeForList> myList;

    @Required
    public void setMyList(List<TypeForList> myList) {
        this.myList = myList;
    }

}

回答by Jakub Kubrynski

Another option is to use JavaConfig. Assuming that all stages are already registered as spring beans you just have to:

另一种选择是使用 JavaConfig。假设所有阶段都已经注册为 spring bean,你只需要:

@Autowired
private List<Stage> stages;

and spring will automatically inject them into this list. If you need to preserve order (upper solution doesn't do that) you can do it in that way:

spring 会自动将它们注入到这个列表中。如果您需要保留订单(上层解决方案不这样做),您可以这样做:

@Configuration
public class MyConfiguration {
  @Autowired
  private Stage1 stage1;

  @Autowired
  private Stage2 stage2;

  @Bean
  public List<Stage> stages() {
    return Lists.newArrayList(stage1, stage2);
  }
}

The other solution to preserve order is use a @Orderannotation on beans. Then list will contain beans ordered by ascending annotation value.

另一种保持顺序的解决方案是@Order在 bean 上使用注释。然后列表将包含按升序注释值排序的 bean。

@Bean
@Order(1)
public Stage stage1() {
    return new Stage1();
}

@Bean
@Order(2)
public Stage stage2() {
    return new Stage2();
}

回答by Jose Alban

As an addition to Jakub's answer, if you plan to use JavaConfig, you can also autowire that way:

作为 Jakub 答案的补充,如果您打算使用 JavaConfig,您还可以通过这种方式自动装配:

import com.google.common.collect.Lists;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;

<...>

@Configuration
public class MyConfiguration {

    @Bean
    public List<Stage> stages(final Stage1 stage1, final Stage2 stage2) {
        return Lists.newArrayList(stage1, stage2);
    }
}

回答by Slava Babin

And this is how to inject set in some property in Spring:

这是如何在 Spring 的某些属性中注入 set:

<bean id="process"
      class="biz.bsoft.processing">
    <property name="stages">
        <set value-type="biz.bsoft.AbstractStage">
            <ref bean="stageReady"/>
            <ref bean="stageSteady"/>
            <ref bean="stageGo"/>
        </set>
    </property>
</bean>

回答by RaM PrabU

 <bean id="student1" class="com.spring.assin2.Student">  
<property name="name" value="ram"></property>  
<property name="id" value="1"></property> 
<property name="listTest">
        <list value-type="java.util.List">
            <ref bean="test1"/>
            <ref bean="test2"/>
        </list>
    </property>
</bean>  

define those beans(test1,test2) afterwards :)

之后定义这些 bean(test1,test2) :)