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在实际代码中没有错别字。 –