package com.h3.common; import java.util.map; public class customer { private map maps; //... }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerbean" class="com.h3.common.customer"> <property name="maps"> <bean class="org.springframework.beans.factory.config.mapfactorybean"> <property name="targetmapclass"> <value>java.util.hashmap</value> </property> <property name="sourcemap"> <map> <entry key="key1" value="one" /> <entry key="key2" value="two" /> <entry key="key3" value="three" /> </map> </property> </bean> </property> </bean> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="customerbean" class="com.h3.common.customer"> <property name="maps"> <util:map map-class="java.util.hashmap"> <entry key="key1" value="1" /> <entry key="key2" value="2" /> <entry key="key3" value="3" /> </util:map> </property> </bean> </beans>
caused by: org.xml.sax.saxparseexception: the prefix "util" for element "util:map" is not bound.
执行结果...
package com.h3.common; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class app { public static void main( string[] args ) { applicationcontext context = new classpathxmlapplicationcontext("springbeans.xml"); customer cust = (customer)context.getbean("customerbean"); system.out.println(cust); } }
输出结果:
customer [maps={key2=two, key1=one, key3=three}]