【问题标题】:How to achieve "Setter Injection with Map"如何实现“带 Map 的 Setter 注入”
【发布时间】:2015-03-27 09:22:30
【问题描述】:

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


    }

}

【问题讨论】:

  • 您遇到了什么错误?
  • @Erik Dolor KeyStore.Entry 类型不是通用的;它不能用参数 参数化

标签: java spring dictionary dependency-injection


【解决方案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 中使用

当您考虑发布的代码时,没有提到com.varun.si.collection.Question,所以我想有另一个或其他配置。

建议你写一个:

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

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

【讨论】:

  • 你能解释一下这行发生了什么吗 Set> set=answers.entrySet();为什么我们需要进入???
  • 现在我收到此错误 log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)。 log4j:WARN 请正确初始化 log4j 系统。线程“主”java.lang.ClassCastException 中的异常:com.varun.si.collection.Question 无法在 com.varun.si.collection.map.TestMain.main 处转换为 com.varun.si.collection.map.Question (TestMain.java:14)
  • 您在 TestMain 中使用的类似乎与 applicationContext.xml 中声明的 bean 不同,但您的代码似乎是正确的。你必须对课程提问吗?
  • 我已经发布了上面的所有内容...我想这是造成问题 com.varun.si.collection.Question cannot be cast to com.varun.si.collection.map.Question at跨度>
  • 我明白了。我没有指定正确的绝对路径 TestMain.java 谢谢
猜你喜欢
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2012-04-25
  • 1970-01-01
相关资源
最近更新 更多