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.
聽起來像你的'ApplicationContext'不能自動檢測你的'MessageSource'。該bean的名稱中有一個錯字 - 它是如何在你的實際代碼中? – superEb
類路徑屬性應該用正斜槓分隔,所以請嘗試資源/消息。 – samlewis
@superEb在實際代碼中沒有錯別字。 –