我想从属性文件中检索值,但是ApplicationContext找不到该文件,我不知道为什么。看我的代码和屏幕截图:
spring_part22.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="point1" class="org.dxc.java.technology.part22.Point">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
<context:component-scan base-package="org.dxc.java.technology.part22" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>mymessage_en_US.properties</value>
</list>
</property>
</bean>
</beans>
mymessage.properties的位置:
Drive.java
public class Drive {
static ApplicationContext context =
new ClassPathXmlApplicationContext("ConfigurationFiles/spring_part22.xml");
public static void main(String[] args) {
test1();
}
static void test1() {
System.out.println(context.getMessage("greeting", null, "Default Greeting", null));
}
}
当我运行main函数时,我得到以下输出:
Apr 15, 2017 11:42:39 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@497470ed: startup date [Sat Apr 15 11:42:39 CST 2017]; root of context hierarchy
Apr 15, 2017 11:42:39 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [ConfigurationFiles/spring_part22.xml]
Apr 15, 2017 11:42:40 AM org.springframework.context.support.ResourceBundleMessageSource getResourceBundle
WARNING: ResourceBundle [mymessage_en_US.properties] not found for MessageSource: Can't find bundle for base name mymessage_en_US.properties, locale en_US
Default Greeting
我不知道为什么springframework找不到该属性文件,任何人都可以帮助我或告诉我为什么
答案 0 :(得分:1)
而不是
<value>mymessage_en_US.properties</value>
使用此
<value>mymessage</value>