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

I'm just trying to learn one case of using an object as a value in a spring map. Here is 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>

I am trying to create a map in which the value will be a new instance of the ProductLineMetadata object with different parameters set through the constructor argument. I do not want to create a separate bean entry for each key with the required constructor values. Is there a better way to do this by specifying the parameters inside the card declaration itself?

Any pointer would be highly appreciated.

thank

+3
source share
2 answers

Do 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>
+5
source

testLine - , beans, , Spring ( 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> 
+1

Source: https://habr.com/ru/post/1770632/


All Articles