Posts Tagged ‘Hashtable’

Mapas en el XML de Spring (usar un Hashtable en lugar de un HashMap)

Sábado, septiembre 4th, 2010

Necesitaba usar un mapa sincronizado, así que tuve que investigar como usar un Hashtable en el XML de Spring:

<util:map id="categories" map-class="java.util.Hashtable">
  <entry key="c1" value="category01"/>
  <entry key="c2" value="category02"/>
  <entry key="c3" value="category03"/>
</util:map>

<bean id="item" class="com.coresware.Item">
  <property name="categories" ref="categories"/>
</bean>

Otra cosa interesante es como agregar constantes declaradas en clases java al XML de Spring:

<bean id="..." class="...">
  <property name="isolation">
    <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
  </property>
</bean>

Y podemos juntar todo en la creación de un bean:

<bean id="Context" class="javax.naming.InitialContext">
    <constructor-arg>
        <util:map id="jmsProperties" map-class="java.util.Hashtable">
            <entry>
                <key><util:constant static-field="javax.naming.Context.INITIAL_CONTEXT_FACTORY"/></key>
                <value>org.apache.activemq.jndi.ActiveMQInitialContextFactory</value>
            </entry>
            <entry>
                <key><util:constant static-field="javax.naming.Context.PROVIDER_URL"/></key>
                <value>tcp://localhost:61616</value>
            </entry>
        </util:map>
    </constructor-arg>
</bean>

Y podemos crear mapas con listas como elementos. Para este ejemplo necesitamos:

private HashMap> mapTest;

Y lo configuramos así:

	<util:list id="one">
		<value>String one</value>
		<value>String two</value>
	</util:list>

	<util:list id="two">
		<value>String three</value>
		<value>String four</value>
	</util:list>

	<util:map id="map">
		<entry key="one" value-ref="one" />
		<entry key="two" value-ref="two" />
	</util:map>

La información la obtuve de:

http://www.unicon.net/node/601

http://forum.springsource.org/showthread.php?t=58341

http://forum.springsource.org/archive/index.php/t-45064.html