java - Spring中的HashMap

标签 java spring

我正在尝试使用 Spring 将键/值添加到 HashMap 中,如下所示:

<bean id="genPhrase" class="com.WheelOfFortune.Client.generatePhrase">
    <property name="WordsAndPhrasesLevelOne">
        <util:map map-class="java.util.HashMap">
            <entry key="Object Oriented Design" value-ref="Process of planning a system of interacting objects for the purpose of solving a software problem, It is one approach to software design." />
            <entry key="Hardware devices" value-ref="Collection of physical elements that comprise a computer system." />
            <entry key="Operating System" value-ref="A set of programs that manage computer hardware resources and provide common services for application software." />
            <entry key="while statement" value-ref="A control flow statement that allows code to be executed repeatedly based on a given boolean condition." />
            <entry key="printf statement" value-ref="Refers to a control parameter used by a class of functions typically associated with some types of programming languages." />
            <entry key="Internet Service Provider" value-ref="An organization that provides access to the Internet." />
            <entry key="round robin" value-ref="Used to describe a document signed by multiple parties in a circle to make it more difficult to determine the order in which it was signed, thus preventing a ringleader from being identified." />
            <entry key="Internet Protocol" value-ref="Principal communications protocol used for relaying datagrams (also known as network packets) across an internetwork using the Internet Protocol Suite." />
        </util:map>

不太确定我做错了什么,但是当我运行代码时,我得到:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genPhrase' defined in file [/home/ryan/workspace/WheelOfFortuneSpring/generatePhrase.xml]: Cannot create inner bean 'util:map#a6d006c' of type [org.springframework.beans.factory.config.MapFactoryBean] while setting bean property 'WordsAndPhrasesLevelOne'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'util:map#a6d006c': Cannot resolve reference to bean 'Process of planning a system of interacting objects for the purpose of solving a software problem, It is one approach to software design.' while setting bean property 'sourceMap' with key [TypedStringValue: value [Object Oriented Design], target type [null]]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'Process of planning a system of interacting objects for the purpose of solving a software problem, It is one approach to software design.' is defined

最后是我的代码:

public class generatePhrase implements generatePhraseInterface {
    public void readPhrase() {
        for (String key : WordsAndPhrasesLevelOne.keySet())
        {
            System.out.print(key + " : ");
        }
    }
    private Map<String,String> WordsAndPhrasesLevelOne;

    public void setWordsAndPhrasesLevelOne(Map<String,String> WordsAndPhrasesLevelOne)
    {
        this.WordsAndPhrasesLevelOne = WordsAndPhrasesLevelOne;
    }

有人可以建议什么可能导致此异常吗?

最佳答案

value-ref 属性用于引用另一个 bean。相反,请使用 value=。 。 . 在您的条目中,如下所示。

        <entry key="Object Oriented Design" value="Process of planning a system of interacting objects for the purpose of solving a software problem, It is one approach to software design." />

这里的赠品是错误消息的最后一点:

Cannot resolve reference to bean 'Process of planning a system of interacting objects for the purpose of solving a software problem, It is one approach to software design.' while setting bean property 'sourceMap' with key [TypedStringValue: value [Object Oriented Design], target type [null]]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'Process of planning a system of interacting objects for the purpose of solving a software problem, It is one approach to software design.' is defined

关于java - Spring中的HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33056744/

相关文章:

java - 代表数字的链表

java - 在Spring MVC错误处理中获取HTTP错误代码

java - 即使指定了列名也是错误的

spring - 在gradle 2.12和2.13中使用spring-boot的奇怪错误

java - spring:即时添加 xml 上下文?

java - java spring中的通用集合依赖

java - 在 JFrame 上绘制 Canvas

java.sql.SQLException : ORA-00942: table or view does not exist with JPA entityManager. createQuery()

Java:循环 sleep 的替代方法?

java - Java 中静态变量的延迟初始化 - 执行?