2011-09-27 50 views
2

我applicationContext.xml的是路徑:的MessageSource和PropertyPlaceholderConfigurer以及無法加載但與消息類路徑*

src/main/resources/META-INF/spring 

和財產文件路徑:

src/main/resources/messages 

,我加載網頁Spring上下文。 xml如下:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:META-INF/spring/applicationContext.xml</param-value> 
</context-param> 

當我配置MessageSource和PropertyPlaceholderConfigurer如下:

<bean id="propertyPlaceholderConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath:messages/apps.properties</value>   
     </list> 
    </property> 
</bean> 

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basenames"> 
     <list> 
      <value>classpath:messages/ValidationMessages</value> 
      <value>classpath:messages/app</value> 
     </list> 
    </property> 
    <property name="defaultEncoding" value="UTF-8"/> 
</bean> 

他們都沒有工作,當我改變classpathclasspath* 任何想法,爲什麼它僅適用?

+0

你是什麼意思它不起作用,當你啓動你的服務器時是否有某種錯誤信息? – user273895

+0

你屬於同一個'jar'嗎?你有沒有嘗試'classpath:/ messages/apps.properties'。注意'messages'前面的正斜槓 – tolitius

+1

@Jword:在ClassPathResource.class中(在構造函數中)設置一個斷點並以調試模式啓動你的servlet容器。然後,當Spring爲這些無法找到的文件創建Resource對象時,請檢查正在使用哪個ClassLoader,哪個Path和哪個Class用於解析。 @tolitius:當使用'classpath:'方案時,Spring無論如何剝離主導斜線。請參閱ClassPathResource(String,ClassLoader)構造函數。 – mhaller

回答

3

Spring documentation

4.7.2.2類路徑*:前綴

[...]路徑字符串可能使用特殊的classpath*:前綴:[...]

這種特殊prefix [...]指定必須獲取與給定名稱匹配的所有類路徑資源,,然後合併以形成最終的應用程序上下文定義。

你確定沒有其他messages/apps.properties文件在你的CLASSPATH巧合地優先和重寫你的文件?此描述建議您使用*時可能會合並多個相同的命名文件。

SomeClass.class.getClassLoader().getResources("/messages/apps.properties"); 

可以通過調用檢查這個?

+0

當我試着上面的代碼,沒有方法getResources。 –

+0

對不起,代碼現在已修復。 –

+0

感謝您的反饋,但url必須以斜線開頭,並且枚舉結果只包含target/classes/messages中的一個文件,那麼您怎麼看? –

2

看看這個優秀article的類路徑v CLASSPTH *的問候春天資源加載不同,我做了你的問題,一些測試在我的測試中,它的工作我是否使用類路徑或類路徑*

我的代碼清單這裏說一下測試,我沒有

  • 我創建了一個這樣的目錄結構(META-INF /春在src /主/資源)放置的context.xml
  • 我在這裏列出完整的context.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="propertyPlaceholderConfigurer" 
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="locations"> 
         <list> 
          <value>classpath:messages/apps.properties</value> 
         </list> 
        </property> 
        </bean> 
    
        <bean class="prasanna.service.TestBean"> 
         <property name="appName" value="${appname}"></property> 
        </bean> 
    
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basenames"> 
        <list> 
         <value>classpath:messages/ValidationMessages</value> 
          <value>classpath:messages/apps</value> 
         </list> 
        </property> 
        <property name="defaultEncoding" value="UTF-8"/> 
    </bean> 
        </beans> 
    
  • 清單apps.properties

    appname=spring mvc app 
    
  • 清單ValidationMessages。性能

    error.name=Invalid name 
    
  • testBean就相當簡單

    public class TestBean 
        { 
         private String appName; 
    
         public String getAppName() { 
          return appName; 
         } 
    
         public void setAppName(String appName) { 
          this.appName = appName; 
         } 
    
        } 
    
  • 我用一個簡單的Java類加載屬性文件並閱讀他們

    public class LoadContext 
    { 
        public static void main(String[] args) 
        { 
         ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:META-INF/spring/context.xml"}); 
         ReloadableResourceBundleMessageSource msgs = ctx.getBean(ReloadableResourceBundleMessageSource.class); 
    
           TestBean testBean = ctx.getBean(TestBean.class); 
        Assert.assertTrue(testBean.getAppName().equals("spring mvc app")); 
    
         String msg = msgs.getMessage("appname", new Object[]{new DefaultMessageSourceResolvable("appname")}, null); 
         System.out.println(" "+ msg); 
    
         String msg2 = msgs.getMessage("error.name", new Object[]{new DefaultMessageSourceResolvable("error.name")}, null); 
         System.out.println(" "+ msg2); 
        } 
    
    } 
    
相關問題