如何实现" Setter Injection with Map"

时间:2015-03-27 09:22:30

标签: java spring dictionary dependency-injection

我是Spring的新手并且正在努力学习Spring 我正在尝试使用Map实现Setter Injection我不知道如何实现这一点我遵循了一些教程但是在编写代码之后我得到了语法错误而且我不知道如何修复这个甚至在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();


    }

}

1 个答案:

答案 0 :(得分:1)

Question.java中更改以下导入:

import java.security.KeyStore.Entry;

到此:

import java.util.Map.Entry;

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

修改

关于您的第一个问题:使用Entry是必要的,因为它是Set上迭代器的最佳方式。

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

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

    在您考虑发布的代码时,没有对com.varun.si.collection.Question的引用,因此我认为还有其他或其他配置。

    建议你放一个:

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

    TestMain.java ,如果有的话,最终删除导入com.varun.si.collection.Question