我正在尝试使用util名称空间注入HashMap对象,但无法获取该对象的HashMap类(正在打印LinkedHashMap类名称。) 无法弄清楚为什么会这样。
在此测试中,我在Spring jar下方使用:
1. spring-beans-5.0.7.RELEASE.jar 2. spring-context-5.0.7.RELEASE.jar 3. spring-core-5.0.7.RELEASE.jar 4. spring-expression-5.0.7.RELEASE.jar 5. commons-logging-1.1.1.jar
Bean类:
package com.vitp.pkg1;
import java.util.HashMap;
public class ActualCollections {
private HashMap<String,Integer> student;
public void setStudent(HashMap<String, Integer> student) {
this.student = student;
}
public void displayData()
{
System.out.println("Map data(Student<name,rollnumber>) : ---default is LinkedHashMap And actual is :: "+student.getClass().getName());
for(String key : student.keySet())
{
System.out.println(key+"::"+student.get(key));
}
}
}
用于测试输出的驱动程序类:
package com.vitp.start;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.vitp.pkg1.ActualCollections;
import com.vitp.pkg1.DefaultCollections;
public class ActualCollectionObjectInjectionTest {
public static void main(String... s)
{
ApplicationContext beans = new ClassPathXmlApplicationContext("/com/vitp/resources/particular_collection.xml");
ActualCollections pc = (ActualCollections) beans.getBean("pc");
pc.displayData();
}
}
xml配置文件:
<!--In this case we need to make use of util namespace so will use xsd instate of dtd.-->
<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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="pc" class="com.vitp.pkg1.ActualCollections">
<!--Set Map attribute with setter injection-->
<property name="student">
<util:map map-class="java.util.HashMap">
<!-- demonstrating various ways on values for entry-->
<entry key="Sachin" value="1"/>
<!--way2-->
<entry key="Rahul">
<value>2</value>
</entry>
<!--way3-->
<entry key="Vinayak" value-ref="three"/>
<!--way4-->
<entry key="Salman">
<ref bean="four"/>
</entry>
</util:map>
</property>
</bean>
<bean id="three" class="java.lang.Integer">
<constructor-arg index="0">
<value>3</value>
</constructor-arg>
</bean>
<bean id="four" class="java.lang.Integer">
<constructor-arg index="0">
<value>4</value>
</constructor-arg>
</bean>
</beans>
结果是:
Sep 09, 2018 7:17:03 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@27f8302d: startup date [Sun Sep 09 19:17:03 IST 2018]; root of context hierarchy
Sep 09, 2018 7:17:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/vitp/resources/particular_collection.xml]
Map data(Student<name,rollnumber>) : ---default is LinkedHashMap And actual is :: java.util.LinkedHashMap
Rahul::2
Vinayak::3
Sachin::1
Salman::4
我希望它应该是HashMap类。
答案 0 :(得分:0)
您缺少Map的id属性。如果您将id用作地图,则可以使用。 工作代码:
<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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="pc" class="com.vitp.pkg1.ActualCollections">
<property name="student" ref="someId"/>
</bean>
<util:map id="someId" map-class="java.util.HashMap">
<!-- demonstrating various ways on values for entry-->
<entry key="Sachin" value="1"/>
<!--way2-->
<entry key="Rahul">
<value>2</value>
</entry>
<!--way3-->
<entry key="Vinayak" value-ref="three"/>
<!--way4-->
<entry key="Salman">
<ref bean="four"/>
</entry>
</util:map>
<bean id="three" class="java.lang.Integer">
<constructor-arg index="0">
<value>3</value>
</constructor-arg>
</bean>
<bean id="four" class="java.lang.Integer">
<constructor-arg index="0">
<value>4</value>
</constructor-arg>
</bean>
</beans>