繁体   English   中英

如何在 Spring 中定义 List bean?

[英]How to define a List bean in Spring?

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

我无法更改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>

value-type 是要使用的泛型类型,并且是可选的。 您还可以使用属性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>

另一种选择是使用 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注释。 然后列表将包含按升序注释值排序的 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 {

    List<TypeForList> myList;

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

}

Stacker 提出了一个很好的答案,我会更进一步使其更具动态性并使用 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>元素作为其值,并为 bean 提供一个id属性。 然后,每次在其他 bean 声明中使用声明的id作为ref或类似内容时,都会实例化列表的新副本。 您还可以指定要使用的List类。

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

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

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

您只需从<list>标签内的 bean 中删除id 像这样:

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

注入字符串列表。

假设您有一个采用如下字符串列表的国家模型类。

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

参考链接

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM