我搜索了很多但没有找到关于此的示例或声明。
是否可以使用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>
Xml-wise看起来像它将要获得的简洁! DTD不支持任何其他内容。
因为你需要refs,所以内联也不会更短。
当然。这是一个例子:
<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>
最短的我发现这是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)较低的可读性。