在Spring中将字符串数组注入bean

Que*_*ueg 16 java spring

<?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>
Run Code Online (Sandbox Code Playgroud)

这是我目前的XML.如果Test只拿一份清单 - 一切都会好的.

问题是Test需要一个字符串数组.

怎么在春天做?

Pau*_*nis 18

你应该使用:

<constructor-arg>
    <array>
        <value>aa</value>
        <value>bb</value>
        <value>cc</value>
    </array>
</constructor-arg>
Run Code Online (Sandbox Code Playgroud)

数组可以包含多个内部bean,ref,collection或value元素.即使在被定义为例如值为Object类型的映射的值时,此配置元素也将始终生成数组.

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

<array value-type="java.lang.String">
  <value>aa</value>
  <value>bb</value>
  <value>cc</value>
</array>
Run Code Online (Sandbox Code Playgroud)

值类型

嵌套值的默认Java类型.必须是完全限定的类名.