2015-10-10 78 views
0

我试图键/值加入到使用Spring是一个HashMap,如下图所示: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; 
    } 

可有人建议这可能是造成这个异常?

+0

也许这应该是'value =。 。 .'而不是'value-ref =。 。 .'。 value-ref是否指向另一个bean的引用? – CollinD

+0

我不确定,我正在跟着一本关于Spring的书。让我给出一个镜头虽然 – ryekayo

+0

好电话的人!谢谢你!!,你可以添加作为答案 – ryekayo

回答

1

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." /> 

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

无法解析参考豆“规划相互作用的对象来解决软件问题的目的,一个系统的过程中,一种软件设计方法。“同时用键[TypedStringValue:value [面向对象设计],目标类型[null]]设置bean属性'sourceMap';嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为“为了解决软件问题而计划交互对象的系统的过程的bean”,这是一种软件设计方法。被定义为