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;
}
可有人建議這可能是造成這個異常?
也許這應該是'value =。 。 .'而不是'value-ref =。 。 .'。 value-ref是否指向另一個bean的引用? – CollinD
我不確定,我正在跟着一本關於Spring的書。讓我給出一個鏡頭雖然 – ryekayo
好電話的人!謝謝你!!,你可以添加作爲答案 – ryekayo