Spring简写为bean属性?

问题描述 投票:2回答:3

我搜索了很多但没有找到关于此的示例或声明。

是否可以使用Spring(此时为4.3)XML <util:map>速记来在property中分配bean

具体来说,我想简化/缩短这个:

<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
    <property name="attributes">
        <map>
            <entry key="properties" value-ref="properties"/>
        </map>
    </property>
</bean>
spring
3个回答
1
投票

Xml-wise看起来像它将要获得的简洁! DTD不支持任何其他内容。

因为你需要refs,所以内联也不会更短。


1
投票

当然。这是一个例子:

<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
    <property name="attributes">
        <util:map>
            <entry key="key1" value="strValue" />   <!-- value is string -->
            <entry key="key2" value="1234" value-type="java.lang.Integer" /> <!-- Use value-type to explicitly specify the type -->
            <entry key="key3" value-ref="fooBean"/> <!-- Use value-ref to reference to other bean -->
        </util:map>
    </property>
</bean>

1
投票

最短的我发现这是2-3线,取决于格式。假设您已经定义了必要的命名空间:

<util:map id="attributes">
    <entry key="properties" value-ref="properties"/>
</util:map>
<bean class="org.springframework.web.context.support.ServletContextAttributeExporter"
      p:attributes-ref="attributes"/>

但可以说不值得额外的间接和(IMO)较低的可读性。

© www.soinside.com 2019 - 2024. All rights reserved.