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

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

问题描述

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

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.

我无法更改 Configurator 类.

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

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>

变成豆子.

这样的事情是行不通的:

Something like this does not work:

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

有人可以帮我解决这个问题吗?

Can anybody help me with this?

推荐答案

导入 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天全站免登陆