如何在Spring中定义List bean? [英] How to define a List bean in Spring?

查看:386
本文介绍了如何在Spring中定义List bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring在我的应用程序中定义阶段。它被配置为必要的类(这里称为 Configurator )被注入阶段。

现在我需要另一个类中的阶段列表,名为 LoginBean 配置程序无法访问他的阶段列表。

I'm using Spring to define stages in my application. It's configured that the necessary class (here called Configurator) is injected with the stages.
Now I need the List of Stages in another class, named LoginBean. The Configurator doesn't offer access to his List of Stages.

我无法更改班级配置器

我的想法:

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

My Idea:
Define a new bean called Stages and inject it to Configurator and LoginBean. My problem with this idea is that I don't know how to transform this property:

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

进入一个bean。

类似的东西这不起作用:

Something like this does not work:

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

任何人都可以帮我吗?

推荐答案

导入spring util命名空间。然后你可以按如下方式定义一个列表bean:

Import the spring util namespace. Then you can define a list bean as follows:

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

价值类型是要使用的泛型类型,并且是可选的。您还可以使用 list-class 属性指定列表实现类。

The value-type is the generics type to be used, and is optional. You can also specify the list implementation class using the attribute list-class.

这篇关于如何在Spring中定义List bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆