2017-05-01 62 views
0

我想從我的消息屬性得到的消息自動裝配文件爲messageSource沒有被春天

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
<property name="basenames"> 
<list> 
<value>mymessage</value> 
</list> 
</property> 
</bean> 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.MessageSource; 

public class Test{ 

@Autowired 
    private MessageSource messageSource; 
    public MessageSource getMessageSource() { 
     return messageSource; 
    } 
    public void setMessageSource(MessageSource messageSource) { 
     this.messageSource = messageSource; 
    } 

public void draw() 
    { 

    System.out.println(this.messageSource.getMessage("name",null,"default",null));`enter code here` 
    }} 

在我得到空指針異常

但是當我已經改變了代碼並執行測試類中的MessageSourceAware然後工作正常。甚至在我刪除@autowired註釋和bean id定義本身之後。

+0

但始終默認值 – Prayer

回答

0

這裏我聲明在調度方面 「的servlet-context.xml的」:

<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <beans:property name="basename" value="classpath:messages" /> 
    <beans:property name="defaultEncoding" value="UTF-8"/> 
    <beans:property name="fileEncodings" value="UTF-8"/> 
</beans:bean> 

更改類名從class="org.springframework.context.support.ResourceBundleMessageSourceclass="org.springframework.context.support.ReloadableResourceBundleMessageSource

爲什麼你的基本名稱屬性是空的?