在Spring中将字符串数组注入bean [英] Inject array of strings to a bean in Spring

查看:822
本文介绍了在Spring中将字符串数组注入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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://            www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="test" class="com.Test">
        <constructor-arg>
            <list>
                <value>aa</value>
                <value>bb</value>
                <value>cc</value>
            </list>
        </constructor-arg>
    </bean>
</beans>

这是我目前的XML。
如果只有测试拿了一个List - 一切都会好的。

This is my current XML. If only Test took a List - everything would be fine.

问题在于测试获取一个字符串数组。

The problem is that Test takes an array of strings.

如何在Spring中执行此操作?

How to do it in Spring?

推荐答案

你应该使用:

<constructor-arg>
    <array>
        <value>aa</value>
        <value>bb</value>
        <value>cc</value>
    </array>
</constructor-arg>




数组可以包含多个内部bean,ref,collection或value
元素。这个配置元素总是会产生一个数组,即使被定义为
,例如作为值为
对象的地图的值。

An array can contain multiple inner bean, ref, collection, or value elements. This configuration element will always result in an array, even when being defined e.g. as a value for a map with value type Object.

您还可以指定要传递的值类型:

Also you can specify a value type which you will be passing:

<array value-type="java.lang.String">
  <value>aa</value>
  <value>bb</value>
  <value>cc</value>
</array>

value-type


嵌套值的默认Java类型。必须是完全合格的
类名。

The default Java type for nested values. Must be a fully qualified class name.

这篇关于在Spring中将字符串数组注入bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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