You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Blueprint XML中将ArrayList配置为Bean?

问题:OSGi Blueprint中正确配置ArrayList对象的方法

我的XML文件头部配置如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0">

尝试1:使用constructor-arg初始化ArrayList

配置代码:

<bean id="someBean" class="com.someClass"/>
<bean id="someBean1" class="com.someClass"/>

<bean id="messageFilterList" class="java.util.ArrayList">
 <constructor-arg>
  <list value-type="com.someClass">
   <ref bean="someBean"/>
  </list>
 </constructor-arg>
</bean>

触发错误:

Fault:
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element '{"http://www.osgi.org/xmlns/blueprint/v1.0.0":constructor-arg}'. One of '{"http://www.osgi.org/xmlns/blueprint/v1.0.0":description, "http://www.osgi.org/xmlns/blueprint/v1.0.0":argument, "http://www.osgi.org/xmlns/blueprint/v1.0.0":property, WC[##other:"http://www.osgi.org/xmlns/blueprint/v1.0.0"]}' is expected.

后续尝试将constructor-arg替换为argument或property,以及用factory-method=add初始化,均触发相同的SAXParseException错误。

尝试2:使用util:list

配置代码:

<util:list>
       <ref bean="idOfBeanI'veTriedToGet"/>
</util:list>

问题:Camel项目直接停止,Karaf日志仅显示路由停止,无额外错误信息。


正确配置方式

方式1:用Blueprint的<argument>替代<constructor-arg>

Blueprint规范中构造参数需用<argument>标签(而非Spring的<constructor-arg>),修正后配置:

<bean id="someBean" class="com.someClass"/>
<bean id="someBean1" class="com.someClass"/>

<bean id="messageFilterList" class="java.util.ArrayList">
    <argument>
        <list value-type="com.someClass">
            <ref bean="someBean"/>
            <ref bean="someBean1"/>
        </list>
    </argument>
</bean>

方式2:使用util:list并指定ID和类型

若需将list作为独立bean供其他组件引用,需为util:list添加id和value-type:

<util:list id="messageFilterList" value-type="com.someClass">
    <ref bean="someBean"/>
    <ref bean="someBean1"/>
</util:list>

该方式默认创建ArrayList实例,无需显式指定class。

关键注意事项

  • Blueprint与Spring XML配置存在差异,构造参数必须用<argument>标签。
  • 使用util:list时,未指定id的list仅能作为内部参数使用,无法全局引用。
  • 确保com.someClass已正确打包并在OSGi容器中可用,避免类加载问题导致项目异常停止。

内容的提问来源于stack exchange,提问作者Maciej Kajetan Chrzanowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 18:42:29