繁体   English   中英

使Spring PropertyPlaceholderConfigurer覆盖MessageSource值时出现问题

[英]Issue getting Spring PropertyPlaceholderConfigurer to override MessageSource values

我有一个春天的Java配置对象,可以加载我的属性,但不覆盖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的方法,因为这是我们的速度模板中使用的方法:

    #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
...

有关在propertyPlaceholderConfigurer() @Bean方法上使用的注释的@Bean

  • @ Lazy(false)是不必要的,因为它已经是默认值。 您可以忽略这个。
  • @DependsOn("propertyOverrideUrl")是不必要的,因为依赖已经通过调用建立propertyOverrideUrl()从内propertyPlaceholderConfigurer()

关于您的实际问题,要回答这个问题有点困难,因为我不确定ContextAwareProperyPlaceholderConfigurer功能(我假设它是自定义组件,因为它不属于Spring核心框架)。

除此之外,可能只是存在误会。 Spring的PropertyPlaceholderConfigurer (PPC)和朋友通过后期处理Bean定义来操作以替换${...}占位符,但不以任何方式与MessageSource对象进行交互。 因此,我相信您所描述的行为符合预期:在查询bean时,您会看到“正确的” URL(因为它已由您的PPC进行了后处理),但是在查询消息时,您看到的是“不正确”的url。来源(因为它与PPC后处理完全无关)。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM