2011-01-12 6 views
0

私のプロパティを読み込むが、MessageSourceのトークンを上書きしないSpring Javaの設定オブジェクトがあります。問題SpringのPropertyPlaceholderConfigurerを取得してMessageSourceの値を無効にする

@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の値をしようとすると、それはまだ古い値があります:私はjavaconfigプロパティにユニットテストを実行すると

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

それは結構です

@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) 
@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("--------------------"); 
    } 
} 

そして期待通りの値が印刷されています

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

は、私はいくつかのログを追加しました:は、誰かがそれが私たちのVelocityテンプレートで使用されているものですので、私はMessageSourceを無効にする方法を見つける助けてください。あなたの propertyPlaceholderConfigurer() @Bean方法で使用されるアノテーションに関する

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

答えて

0

カップルサイドノート:それはすでにデフォルトであるようLazy(false) @

  • は、不要です。これを省略することができます。
  • @DependsOn("propertyOverrideUrl")は、依存関係がすでにあなたの実際の質問にオンpropertyPlaceholderConfigurer()

内からpropertyOverrideUrl()を呼び出すことによって確立されているので、私はContextAwareProperyPlaceholderConfigurerは、(I」が何をするかわからないんだから、それは答えるために少し厳しいですが、不要ですそれはカスタムSpring Frameworkの一部ではないため、カスタムコンポーネントだとします)。

それ以外にも、誤解が起こっている可能性があります。 SpringのPropertyPlaceholderConfigurer(PPC)とフレンドは、${...}プレースホルダを置き換えるためにBean定義を後処理して動作しますが、何らかの形でMessageSourceオブジェクトと対話しません。それで、あなたが描いている振る舞いは、私が期待している通りだと信じています。あなたのPPCで後処理されているので、ビーンズに質問するときに「正しい」URLが表示されますが、ソース(PPC後処理とは全く関係がないため)。

これが役に立ちます。

関連する問題