Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question on a specific use case on spring <util:map>

Tags:

spring

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

like image 721
Shamik Avatar asked Dec 30 '25 12:12

Shamik


1 Answers

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>
like image 63
skaffman Avatar answered Jan 01 '26 21:01

skaffman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!