I'm just trying to explore one use case of using object as a value in a spring map. Here's my example
<util:map id="someSourceMap" map-class="java.util.HashMap">
<entry key="source1" value="testLine"/>
<entry key="source2" value="testLine2"/>
</util:map>
<bean id="testLine1" class="com.test.ProductLineMetadata" scope="prototype">
<constructor-arg value="PRODUCT_LINE_1"></constructor-arg>
<constructor-arg value="TYPE_1"></constructor-arg>
</bean>
<bean id="testLine2" class开发者_运维百科="com.test.ProductLineMetadata"scope="prototype">
<constructor-arg value="PRODUCT_LINE_2"></constructor-arg>
<constructor-arg value="TYPE_2"></constructor-arg>
</bean>
What I'm trying to achieve is to create a map in which the value will be a new instance of ProductLineMetadata object with different parameters set through constructor argument. I don't want to create a separate bean entry for each key with the desired constructor values. Is there a better way of doing this by somehow specifying the parameters inside the map declaration itself?
Any pointer will be highly appreciated.
Thanks
You mean something like this?
<util:map id="someSourceMap" map-class="java.util.HashMap">
<entry key="source1">
<bean class="com.test.ProductLineMetadata">
<constructor-arg value="PRODUCT_LINE_1"/>
<constructor-arg value="TYPE_1"/>
</bean>
</entry>
<entry key="source2">
<bean class="com.test.ProductLineMetadata">
<constructor-arg value="PRODUCT_LINE_2"/>
<constructor-arg value="TYPE_2"/>
</bean>
</entry>
</util:map>
If your testLine
s are just test data rather than regular beans you may use more lightweight approach to declare them, for example, Spring Expression Language (since Spring 3):
<util:map id="someSourceMap" map-class="java.util.HashMap">
<entry key="source1"
value="#{new com.test.ProductLineMetadata('PRODUCT_LINE_1', 'TYPE_1')}"/>
<entry key="source2"
value="#{new com.test.ProductLineMetadata('PRODUCT_LINE_2', 'TYPE_2')}"/>
</util:map>
精彩评论