2013-07-28 78 views
0

spring.xml讀取屬性文件在Spring 3.2

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="meassageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="resource\message"> 
    </property> 
    </bean> 

</beans> 

Main.java類文件

public class Main { 

    public static void main(String[] args) { 

     ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml"); 

     System.out.println(context.getMessage("emp", null, Locale.US)); 

    } 

} 

我的屬性文件在src /資源文件夾。文件名是mesaage_en_US.properties。 我也嘗試過不同的文件名,如message.property,message_en.property和不同的語言環境,如Locale.English,Locale.UK,但沒有運氣。 我將屬性文件移動到src文件夾,但得到相同的異常。 我收到以下異常。

Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'emp' for locale 'en_US'. 
    at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:65) 
    at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1234) 
    at org.beans.Main.main(Main.java:14) 

請幫忙。

message_en_US.properties

emp=Hello Employee. 
+0

聽起來像你的'ApplicationContext'不能自動檢測你的'MessageSource'。該bean的名稱中有一個錯字 - 它是如何在你的實際代碼中? – superEb

+0

類路徑屬性應該用正斜槓分隔,所以請嘗試資源/消息。 – samlewis

+0

@superEb在實際代碼中沒有錯別字。 –

回答

5

我喜歡用一個PropertySourcesPlaceholderConfigurerHere's a great tutorial讓你開始。

基本上,你需要添加:

<context:property-placeholder location="classpath:foo.properties" /> 

你的Spring XML配置文件,其中「foo.properties」是類路徑中的資源的絕對路徑。

然後你可以將它們注入這樣的領域:

@Value("${jdbc.url}") 
private String jdbcUrl; 

其中「jdbc.url」是在你的屬性文件引用名。

當然,@Value不會在你的static void main裏面工作,但是我真的懷疑static void main是你想要使用你的屬性的地方。你應該從Spring Bean中訪問它們。

+0

正確,但它的測試應用程序。 –

+0

那麼,我會斷言在製作Spring測試應用程序時,應該使它成爲Spring應該使用的方式。所以你應該測試你可以從Spring組件訪問屬性,而不是直接從應用程序上下文訪問。 [這個問題](http://stackoverflow.com/questions/10822951/how-do-i-get-a-property-value-from-an-applicationcontext-object-not-using-an-a)有一個答案這就解釋了爲什麼在運行時無法從上下文訪問屬性佔位符。 – CorayThan

+0

我也嘗試通過在不同的bean中實現MessageSourceAware接口。這也不起作用。 –

0

更改爲

<property name="basename" value="message" /> 

在同一個文件夾中message_en_US.properties爲您spring.xml

編輯定義MessageSource當你在你的名稱的拼寫錯誤。它的名字應該完全是messageSource。由於額外ameassageSourceApplicationContext未能加載它。

+0

OP正在請求readind該文件。 – erencan

+0

@Ravi無法正常工作 –

+0

@JitendraShukla你可以嘗試將道具文件移動到'/ src'中嗎? –

0

我認爲這是從this問題重複。基本上,它與你的包和代碼中指定的語言環境之間的不匹配有關。

+0

我嘗試過不同的語言環境,沒有幫助。 –

0

而不是從ApllicationContext得到消息我從MeassageSource本身得到消息。我改變了我的spring.xml這樣

<bean id="employee" class="org.bean.Employee" > 
     <property name="id" value="1"/> 
     <property name="name" value=""/> 
     <property name="dept" value=""/> 
     <property name="messages" ref="messageSource"/>  
     </bean> 

現在我從Employee類調​​用messages.getMessage(this.messages.getMessage("emp", null, Locale.UK))。它的工作。

+0

請檢查我的更新。您錯誤地將bean命名爲* meassageSource *(注意額外的「a」)。 –

+0

@RaviThapliyal正如我所說的在實際代碼中沒有錯字 –

0

我已經複製了你的錯誤,發現了問題。它與Spring沒有找到捆綁包有關。我想你應該在發生異常之前發出警告,併發出以下消息:

WARNING: ResourceBundle [resource\message] not found for MessageSource: Can't find bundle for base name resource\message, locale en_US 

這已經是提示。該問題與您的項目結構以及在指定setBasename屬性時如何搜索捆綁包有關。請看看this

無論如何,我認爲你應該把你的捆綁在更加標準的位置src/main/resources中。如果按照這個約定,你爲messageSource豆應該被定義是這樣的:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="message" /> 
</bean> 

用這種方法你例子應該產生所需的線路:

你好員工。

+0

正確,但如果您從'ApplicationContext'獲得消息,那麼它將不起作用。 –

+0

@JitendraShukla對不起,但它工作。我在測試項目中複製了你的代碼,輸出是沒有錯誤的預期輸出。我從ApplicationContext獲取消息。 –