如何在Spring中定义List bean ?

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

我不能改变类Configurator

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

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

变成一颗豆子。

像这样的东西是行不通的:

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

有人能帮我一下吗?

389553 次浏览

这里有一个方法:

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

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

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

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

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

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

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

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

我试图弄清楚如何用util:列表做到这一点,但由于转换错误无法让它工作。

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


List<TypeForList> myList;


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


}

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

@Autowired
private List<Stage> stages;

spring会自动将它们注入到这个列表中。如果你需要保持秩序(上解不能做到这一点),你可以这样做:

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


@Autowired
private Stage2 stage2;


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

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

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


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

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

这是如何在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>
 <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>

然后定义这些bean (test1,test2)

你只需将id<list>标签内的bean中移除。是这样的:

<property name="listStaff">
<list>
<bean class="com.test.entity.Staff">
<constructor-arg name="name" value = "Jonh"/>
<constructor-arg name="age" value = "30"/>
</bean>
<bean class="com.test.entity.Staff">
<constructor-arg name="name" value = "Jam"/>
<constructor-arg name="age" value = "21"/>
</bean>
</list>
</property>

注入字符串列表。

假设您有一个接受字符串列表的nations模型类,如下所示。

public class Countries {
private List<String> countries;


public List<String> getCountries() {
return countries;
}


public void setCountries(List<String> countries) {
this.countries = countries;
}


}

下面的xml定义定义一个bean并注入国家列表。

<bean id="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
<property name="countries">
<list>
<value>Iceland</value>
<value>India</value>
<value>Sri Lanka</value>
<value>Russia</value>
</list>
</property>
</bean>

参考链接

注入pojo列表

假设你有如下的模型类。

public class Country {
private String name;
private String capital;
.....
.....
}


public class Countries {
private List<Country> favoriteCountries;


public List<Country> getFavoriteCountries() {
return favoriteCountries;
}


public void setFavoriteCountries(List<Country> favoriteCountries) {
this.favoriteCountries = favoriteCountries;
}


}

Bean定义。

 <bean id="india" class="com.sample.pojo.Country">
<property name="name" value="India" />
<property name="capital" value="New Delhi" />
</bean>


<bean id="russia" class="com.sample.pojo.Country">
<property name="name" value="Russia" />
<property name="capital" value="Moscow" />
</bean>




<bean id="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
<property name="favoriteCountries">
<list>
<ref bean="india" />
<ref bean="russia" />
</list>
</property>
</bean>

参考链接

使用util:list中的list-class属性创建任何特定类型的独立列表。例如,如果你想创建类型为ArrayList的列表:

<util:list id="namesList" list-class="java.util.ArrayList" value-type="java.lang.String">
<value>Abhay</value>
<value>ankit</value>
<value>Akshansh</value>
<value>Db</value>
</util:list>

或者如果你想创建一个LinkedList类型的列表,那么:

<util:list id="namesList" list-class="java.util.LinkedList" value-type="java.lang.String">
<value>Abhay</value>
<value>ankit</value>
<value>Akshansh</value>
<value>Db</value>
</util:list>