2011-01-12 53 views
0

我有加載我的屬性但未覆蓋MessageSource標記的spring Java配置對象。獲取Spring PropertyPlaceholderConfigurer以覆蓋MessageSource值的問題

@Configuration 
@SuppressWarnings("unused") 
public class PropertyConfiguration { 

public static final String PROPERTY_OVERRIDE_URL = "d2.config.location"; 

@Bean 
public UrlResource propertyOverrideUrl() { 
    String propertyOverrideUrl = System.getProperty(PROPERTY_OVERRIDE_URL); 

    UrlResource overrideUrl = null; 
    // just add a bogus url as to not get a malformed URL 
    try{ 
     overrideUrl = new UrlResource(
       (propertyOverrideUrl == null || "".equals(propertyOverrideUrl)? "file:///FILENOTFOUND" : propertyOverrideUrl) 
     ); 
    } catch (MalformedURLException e){ 
     // Set the URL to a dummy value so it will not break. 
     try{ 
      overrideUrl = new UrlResource("file:///FILENOTFOUND"); 
     } catch (MalformedURLException me){ 
      // this is a valid URL, but will not be found at runtime. 
     } 
    } 
    return overrideUrl; 
} 

@Bean 
@DependsOn("propertyOverrideUrl") 
@Lazy(false) 
public ContextAwarePropertyPlaceholderConfigurer propertyPlaceholderConfigurer() 
throws IOException{ 
    return new ContextAwarePropertyPlaceholderConfigurer(){{ 
     setLocations(new Resource[]{ 
      new ClassPathResource("application_prompts.properties"), 
      new ClassPathResource("application_webservice.properties"), 
      new ClassPathResource("application_externalApps.properties"), 
      new ClassPathResource("application_log4j.properties"), 
      new ClassPathResource("application_fileupload.properties"), 
      //Must be last to override all other resources 
      propertyOverrideUrl() 
     }); 
     setIgnoreResourceNotFound(true); 
    }}; 
} 

當我在javaconfig酒店內運行單元測試它是好的:

@Test 
public void testOverrides__Found() throws Exception { 
    setOverrideUrlSystemProperty("/target/test/resources/override-test.properties"); 
    context = SpringContextConfigurationTestHelper.createContext(); 
    context.refresh(); 

    String recordedPaymentConfirmationPath = 
      (String)context.getBean("recordedPaymentConfirmationPath"); 
    assertThat(recordedPaymentConfirmationPath, is("test/dir/recordings/")); 

    String promptServerUrl = 
      (String)context.getBean("promptServerUrl"); 
    assertThat(promptServerUrl, is("http://test.url.com")); 
} 

但是,當我嘗試的MessageSource的價值,它仍然有舊值:

@Test 
public void testOverrides__XYZ() throws Exception { 
    setOverrideUrlSystemProperty("/target/test/resources/override-test.properties"); 
    context = SpringContextConfigurationTestHelper.createContext(); 
    context.refresh(); 

    String promptServerUrl = (String)context.getMessage("uivr.prompt.server.url", 
        new Object[] {}, Locale.US); 

    assertThat(promptServerUrl, is("http://test.url.com")); 
} 

輸出:

[junit] Testcase: testOverrides__XYZ took 0.984 sec 
[junit]  FAILED 
[junit] 
[junit] Expected: is "http://test.url.com" 
[junit]  got: "http://24.40.46.66:9010/agent-ivr-prompts/" 
[junit] 
[junit] junit.framework.AssertionFailedError: 
[junit] Expected: is "http://test.url.com" 
[junit]  got: "http://24.40.46.66:9010/agent-ivr-prompts/" 
[junit] 
[junit]  at  com.comcast.ivr.agent.configuration.PropertyOverrideConfigurationTest.testOverrides__XYZ(PropertyOverrideConfigurationTest.java:114) 

有人可以幫我找到一種方法來覆蓋的MessageSource因爲這是在我們的Velocity模板使用:

#set($baseUrl = "#springMessage('uivr.prompt.server.url')") 

我加了一些記錄:

@Override 
    protected void loadProperties(Properties props) throws IOException { 
    super.loadProperties(props); 
    super.mergeProperties(); 
    if(logger.isDebugEnabled()){ 
     System.out.println("--------------------"); 
     for (Map.Entry entry : props.entrySet()) { 
      System.out.println(entry.getKey() + ":" + entry.getValue()); 
      logger.info(entry.getKey() + ":" + entry.getValue()); 
     } 
     System.out.println("--------------------"); 
    } 
} 

和預期的打印值。

... 
[junit] uivr.prompt.server.url:http://test.url.com 
... 

回答

0

有關注釋一對夫婦便箋上您propertyPlaceholderConfigurer()@Bean方法應用於:

  • @Lazy(false)是不必要的,因爲這已經是默認的。你可以省略這個。
  • @DependsOn("propertyOverrideUrl")是不必要的,因爲依賴已經從propertyPlaceholderConfigurer()

到您的實際問題中調用propertyOverrideUrl()成立,這是一個有點很難回答,因爲我不知道什麼ContextAwareProperyPlaceholderConfigurer呢(我假設它是一個自定義組件,因爲它不是核心Spring框架的一部分)。

除此之外,可能只是存在一個誤解。 Spring的PropertyPlaceholderConfigurer(PPC)和朋友通過後處理bean定義來替代${...}佔位符,但不以任何方式與MessageSource對象交互。所以,你所描述的行爲是,我相信,如預期的那樣:當你詢問bean時(因爲它已經被PPC後處理),你會看到'正確的'URL,但是當你詢問消息時你會看到'不正確的'url來源(因爲它與PPC後期處理完全無關)。

希望這會有所幫助。