2017-04-15 75 views
1

我想從屬性文件中檢索值,但ApplicationContext無法找到該文件,我不知道爲什麼。看到我的代碼和屏幕截圖:未找到MessageSource的資源包

spring_part22.xml:

mymessage.properties的
<?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> 

地點:

enter image description here

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)); 

    } 
} 

當我運行主要功能,我得到如下輸出:

Apr 15, 2017 11:42:39 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]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的無法找到屬性文件,任何人都可以幫助我,或告訴我,爲什麼

回答

1

而不是

<value>mymessage_en_US.properties</value> 

使用本

<value>mymessage</value>