如何实现"Setter Injection with Map"

How to achieve "Setter Injection with Map"

我是 Spring 的新手,正在努力学习 Spring 我正在尝试使用 Map 实现 Setter 注入我不知道如何实现这个我遵循了一些教程但是在编写代码之后我遇到了语法错误并且我不知道如何在 eclipse 中解决这个问题我没有收到任何建议。

请建议我如何实现。

我有三个文件

Question.java Class
applicationContext.xml
TeatMain.java

Question.java

package com.varun.si.collection.map;

import java.security.KeyStore.Entry;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Question {
    private int id;
    private String name;
    private Map<String,String> answers;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


    public Map<String, String> getAnswers() {
        return answers;
    }
    public void setAnswers(Map<String, String> answers) {
        this.answers = answers;
    }
    public void display()
    {
        System.out.println("Id :"+id+"\nName :"+name);
        System.out.println("Answers are....");


        Set<Entry<String, String>> set=answers.entrySet();  
        Iterator<Entry<String, String>> itr=set.iterator();

        while(itr.hasNext()){  
            Entry<String,String> entry=itr.next();  
            System.out.println("Answer:"+entry.getKey()+" Posted By:"+entry.getValue());  
        }
    }
}

我在这些行中遇到错误

Set<Entry<String, String>> set=answers.entrySet();  

Entry<String,String> entry=itr.next();  

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

<bean id="question" class="com.varun.si.collection.map.Question">
<property name="id" value="100"></property>
<property name="name" value="varun"></property>
<property name="answers">
<map>
<entry key=" Tera kya hoga re kaliya" value="Gabbar"></entry>
<entry key="" value="Basanti"></entry>
<entry key="Basanti in kutto ke samne mat nachna" value="Veru"></entry>
<entry key="Bhag dhano bhag aaj teri basanti ki izzat ka sawal hai" value="Basanti"></entry>
<entry key="Ye dosti hum nahi chorenge" value="Jay"></entry>
<entry key="ye hath mujhe de de gabbar" value="Thakur"></entry>

</map>

</property>
</bean>



</beans>

TestMain.java

package com.varun.si.collection.map;
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  

public class TestMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Resource resource=new ClassPathResource("com/varun/si/collection/map/applicationContext.xml");
        BeanFactory factory=new XmlBeanFactory(resource);
        Question question=(Question)factory.getBean("question");
        question.display();


    }

}

Question.java 中更改以下导入:

import java.security.KeyStore.Entry;

对此:

import java.util.Map.Entry;

您正在使用一个 class 而不是另一个。

编辑:

第一个问题:有必要使用Entry,因为它是遍历[=​​15=].

的最佳方式

关于你的第二个问题, 错误是说:

  1. 在xml中声明的bean是com.varun.si.collection.map.Question
  2. com.varun.si.collection.Question中使用的class在TestMain中使用

当您考虑发布的代码时,没有对 com.varun.si.collection.Question 的引用,所以我想还有其他配置。

建议你放一个:

import com.varun.si.collection.map.Question;

TestMain.java 并最终删除导入 com.varun.si.collection.Question 如果你有它