Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring中定义列表bean?_Java_Spring - Fatal编程技术网

Java 如何在Spring中定义列表bean?

Java 如何在Spring中定义列表bean?,java,spring,Java,Spring,我正在使用Spring在我的应用程序中定义阶段。它被配置为将必要的类(这里称为Configurator)与阶段一起注入。 现在我需要另一个类中的stage列表,名为LoginBean。Configurator不提供对其阶段列表的访问 我无法更改类配置程序 我的想法: 定义一个名为Stages的新bean,并将其注入Configurator和LoginBean。 我的问题是,我不知道如何转换此属性: ... ... ... 变成豆子 像这样的事情是行不通的: 有人能帮我吗?这里有一个方法:

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

我无法更改类
配置程序

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


...
...
...
变成豆子

像这样的事情是行不通的:


有人能帮我吗?

这里有一个方法:


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


福
酒吧


值类型是要使用的泛型类型,是可选的。您还可以使用属性
list class

指定列表实现类,我想您可能正在寻找


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

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

Stacker给出了一个很好的答案,我将更进一步,使其更具动态性,并使用Spring 3 EL表达式

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

#{springDAOBean.getGenericListFoo()}
我试图弄清楚如何使用util:list实现这一点,但由于转换错误,无法使其工作


<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>
在某种程度上:

class SomeClass {

    List<TypeForList> myList;

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

}
class-SomeClass{
列出我的清单;
@必需的
公共void setMyList(列表myList){
this.myList=myList;
}
}

另一个选项是使用JavaConfig。假设所有阶段都已注册为SpringBean,您只需:

@Autowired
private List<Stage> stages;

作为Jakub答案的补充,如果您计划使用JavaConfig,也可以通过这种方式自动连线:

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

  @Autowired
  private Stage2 stage2;

  @Bean
  public List<Stage> stages() {
    return Lists.newArrayList(stage1, stage2);
  }
}
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);
    }
}
import com.google.common.collect.list;
导入java.util.List;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.Bean;
@配置
公共类MyConfiguration{
@豆子
公开列表阶段(最终阶段1阶段1、最终阶段2阶段2){
返回列表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(test1、test2):)

您只需从
标记内的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>

插入字符串列表。

假设您有一个Countries模型类,它采用如下所示的字符串列表

public class Countries {
    private List<String> countries;

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

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

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

}
公共类国家{
私人名单国家;
国家/地区公共列表(){
返回国;
}   
国家/地区(列表国家/地区){
这个国家=国家;
}
}
下面的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 Countries {
    private List<String> countries;

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

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

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

}
公共类国家{
私有字符串名称;
私人资本;
.....
.....
}
公营国家{
私人名单优惠国家;
公共列表getFavoriteCountries(){
返回最惠国;
}
public void setFavoriteCountries(列出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>


参考。

显然,列表的内容可以是值、引用和bean等。答案很好,它更像“spring式”简单:
嗨,我得到了这个cvc复杂类型。2.4.c:匹配的通配符很严格,但找不到元素“value”的声明。我已经添加了名称空间和模式位置,但我刚刚注意到,
@Autwired
在注入以这种方式创建的列表时没有帮助。然而,
@Resource
起作用。i、 e.
@Resource List我的列表
这是一个很好的提示,但我无法让它工作。斯塔克的安瓦尔成功了+1作为提示。+1-我不知道你能做那件事(尽管我能看出它应该会起作用)。建议:我认为您应该能够在
中嵌入
StageClass
bean声明,避免使用
元素。您还可以使用util:list为您提供一个数组列表。是否可以将这些bean定义嵌入到“”?@Sefler是,这些定义应该是相同的,因此存在一个陷阱:如果使用@Autowired,请确保您的pojo也是“ArrayList”类型,而不仅仅是“List”,否则您可能会得到完全不同的内容。这太棒了。。。用于使用“dictionary”java类,而不是从字典中复制魔术字符串。。非常感谢。