2015-10-10 40 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」,這是一種軟件設計方法。被定義爲