如何在Spring中定义一个List bean?

我正在使用Spring来定义我的应用程序中的阶段。 它被configuration为必要的类(这里称为Configurator )被注入阶段。
现在我需要另一个类中的阶段列表,名为LoginBeanConfigurator器不能访问他的阶段列表。

我不能更改类Configurator

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

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

成豆。

像这样的东西不起作用:

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

有人可以帮我吗?

导入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> 

值types是要使用的genericstypes,并且是可选的。 您也可以使用属性list-class指定列表实现list-class

这里是一个方法:

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

另一个select是使用JavaConfig。 假设所有阶段已经注册为春豆,你只需要:

 @Autowired private List<Stage> stages; 

弹簧会自动将它们注入到这个列表中。 如果你需要保持秩序(上面的解决scheme不这样做),你可以这样做:

 @Configuration public class MyConfiguration { @Autowired private Stage1 stage1; @Autowired private Stage2 stage2; @Bean public List<Stage> stages() { return Lists.newArrayList(stage1, stage2); } } 

另一个解决方法是在bean上使用@Order注解。 然后,列表将包含按升序注释值sorting的bean。

 @Bean @Order(1) public Stage stage1() { return new Stage1(); } @Bean @Order(2) public Stage stage2() { return new Stage2(); } 
 <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> 

而在SomeClass中:

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

堆栈器提出了一个很好的答案,我会更进一步,使其更具dynamic性,并使用Spring 3 EL Expression。

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

我试图找出如何使用util:list做到这一点,但由于转换错误无法工作。

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

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

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

作为Jakub答案的补充,如果您打算使用JavaConfig,那么您也可以通过以下方式进行自动assembly:

 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); } } 

这就是如何在Spring中注入一些属性:

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