上下文与上下文 - 设置 ignore-unresolvable="true" - PropertyPlaceholderConfigurer

Context vs context - setting ignore-unresolvable="true" - PropertyPlaceholderConfigurer

问题

我正在尝试设置 ignore-unresolvable="true"

我找到了答案 from the question how to define not mandatory property in spring?

他们展示的例子是:

<context:property-placeholder ignore-unresolvable="true" ... />

但是,在我继承的项目中,我们有一个名为 project.xml 的项目文件,其中包含带有 Context 标记的 Resource 定义。

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource />
<ResourceLink />
<Resource />
</Context>

注意:资源已被删除

当我编辑 Context 标签以添加 ignore-resolvable 时,一切都中断了,甚至我的 DataSource 资源都没有被读取。有人有什么想法吗?

我尝试了以下方法:

<Context:property-placeholder ignore-unresolvable="true">

可能相关:

spring PropertyPlaceholderConfigurer and context:property-placeholder

事实证明,在特定项目中,使用了基于 class 的配置而不是 XML。我发现以下 class 我在 returns PropertySourcesPlaceholderConfigurer 的方法中添加了 setIgnoreUnresolvablePlaceholders(false):

@Configuration
@ComponentScan
@EnableWebMvc
@EnableAsync
@EnableScheduling
@PropertySource(value = {"classpath:appProp.properties"})
@Import({ExternalizeConfiguration.class, AppApplication.class,
        AppPersistenceApplication.class, ConnectBoIntegrationApplication.class})
public class AppWebApplication extends WebMvcConfigurerAdapter {

...Other Code...

/**
     * Bean required for Value annotation
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer test = new PropertySourcesPlaceholderConfigurer();
        test.setIgnoreUnresolvablePlaceholders(false);
        return test;
    }

}

所以我的理解是,将此方法注释为 @Bean 会导致该方法在自动连接 PropertySourcesPlaceholderConfigurer 类型的对象时执行。通过这种方式,我们可以控制使用哪个实例以及在其上设置哪些参数。