Varun
Varun

Reputation: 4452

How to achieve "Setter Injection with Map"

I am new with Spring and trying to learn Spring I am trying to implement Setter Injection with Map I did not have any idea how to achieve this I followed some tutorial but after writing the code I am getting an syntax error and I do not know how to fix this even in eclipse I am not getting any suggestion.

Please suggest me how to achieve this.

I have three file

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());  
        }
    }
}

I am getting error in these line

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();


    }

}

Upvotes: 1

Views: 523

Answers (1)

Michael
Michael

Reputation: 3326

Change the following import in Question.java :

import java.security.KeyStore.Entry;

to this:

import java.util.Map.Entry;

You are using one class instead of another.

EDIT:

To your first question: it's necessary to use Entry because it's the best way to iterator over a Set.

To your second question, the error is saying that:

  1. The bean declared in the xml is com.varun.si.collection.map.Question
  2. The class used in com.varun.si.collection.Question is used in TestMain

As you consider the posted code, there's no reference to a com.varun.si.collection.Question so I suppose there's another or other configuration.

I suggest you to put a:

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

to TestMain.java and eventually remove import com.varun.si.collection.Question if you have it

Upvotes: 1

Related Questions