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